Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Newbie Javascriptor needs help

    I am trying to collect form data and write it onto the document using a new object to store the data and then its method to write the data to the document. I can get the form data into variables and call it to a prompt but after that the program isnt working. I think it is at the point right after the first alert where I am trying to insert the variables into the newly created object parameters. I need someone to look over it and tell me what I'm doing wrong.

    Thank you soo much...

    -Michael

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Form Input and Display</title>
    <script>

    function writeCustomer(form) {
    var a = document.bucky.firstName.value;
    var b = document.bucky.lastName.value;
    var c = document.bucky.address.value;
    var d = document.bucky.address2.value;
    var e = document.bucky.number.value;
    var f = document.bucky.email.value;
    alert(a + b + c + d + e + f)
    //Program is fine untill after the Alert at this point
    var bucky = new customer(a, b, c, d, e, f)
    //Program fails at this point

    }
    function customer(a, b, c, d, e, f) {
    this.a = a;
    this.b = b;
    this.c = c;
    this.d = d;
    this.e = e;
    this.f = f;
    this.enscribe = enscribe;
    }
    function enscribe(){
    alert(this.a)
    document.write(this.a)
    document.write(this.b)
    document.write(this.c)
    document.write(this.d)
    document.write(this.e)
    document.write(this.f)
    }
    document.write(bucky.enscribe())
    </script>

    </head>
    <body>
    <h1>Customer Information</h1>
    <script>//This form works just fine</script>
    <h2>Please type in your information</h2>
    <form name="bucky" method="get">
    First Name: <input type="text" name="firstName" value="" />;
    Last Name: <input type="text" name="lastName" value=""/>;
    Address Line: <input type="text" name="address" value=""/>;
    Second Address Line: <input type="text" name="address2" value=""/>;
    Phone Number: <input type="text" name="number" value=""/>;
    Email Address: <input type="text" name="email" value=""/>;
    <input type="submit" value="Submit" value="click" onclick="writeCustomer(this.form)"/>;
    </form>



    </body>
    </html>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    You can *NOT* use document.write after a page is loaded.

    If you do, the document.write will *WIPE OUT* all content on your page (except what it wrote), including even the JS code that did the writing!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Another problem: You are using a submit button.

    So even if your call to writeCustomer() works, the <form> will *STILL* be SUBMITTED!

    Meaning that the browser will send the form data back to the web server and the web server will send back AN ENTIRELY NEW PAGE.

    Simple solution to that problem: change type="submit" to type="button"
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Other problems: You have a variable with the same name as your <form>. (And, by the by, <form>s should no longer be named; they should get id's instead.)

    Actually, that's not a problem, only because the variable is LOCAL to the writeCustomer() function and so is *INVISIBLE* outside of that function.

    But then that means that assigning to the variable bucky is doing nothing useful, since indeed it goes away as soon as the writeCustomer function is finished.

    And then, to top it off, you are calling document.write(bucky.enscribe()) *BEFORE* you have created the bucky variable. Again, not that it would really matter, since that variable is invisible outside the function you create it in.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •