SitePoint Sponsor

User Tag List

Results 1 to 1 of 1

Threaded View

  1. #1
    SitePoint Addict
    Join Date
    Apr 2003
    Posts
    332
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Undefined index in Ajax Script

    The problem has been identified.

    Hi,

    My simple ajax script is needed to post form input into a mysql table. When the javascript breakpoint stops the program at the send() function, the POST headers reflect the information just as I want it to be sent. OK so far. However, when the script completes, the php process file returns:
    Code:
    <b>Notice</b>:  Undefined index:  fname in <b>c:\apache\htdocs\andytest\formpost2.php</b> on line <b>7</b>
    <b>Notice</b>:  Undefined index:  lname in <b>c:\apache\htdocs\andytest\formpost2.php</b> on line <b>7</b>
    .

    The file containing the input form is
    Code:
    <head>
    <title>Form In</title>
    <script type="text/javascript">
    var request = null;
    try {
      request = new XMLHttpRequest();
    } catch (trymicrosoft) {
      try {
        request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (othermicrosoft) {
        try {
          request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (failed) {
          request = null;
        }
      }
    }
    
    if (request == null)
      alert("Error creating request object!");
    function formpost(){
    var fname = document.getElementById('fname');
    var lastname = document.getElementById('lastname');
    var url="formpost2.php";
    request.open("POST",url,true);
    var passData="";
    passData='fname='+escape(fname)+'&amp;lastname='+escape(lastname);
    request.setRequestHeader("Content-Type" , "application/x-www.form-urlencoded");
    request.send(passData);
    }
    </script>
    <style type="text/css">
    body{font:bold 100&#37; "Trebuchet MS", sans-serif;
    		color: #666; background: #ccc;
    }
    legend{font-size: 150%;}
    form{width:500px;margin:0 auto; color:#666; background: #fff; padding:3em;}
    label{width:100px; margin-left:1em;}
    .input{width:25em; margin-left: 110px;}
    .button{width:15em; margin-left: 175px;}
    </style>
    </head>
    <body>
    <form method="post" action="">
    <fieldset><legend>Customer Name</legend>
    <label for = "fname">First Name</label><input type="text" class="input" size="25" name="fname" id="fname" /><br />
    <label for = "lastname">Last Name</label><input type="text" class="input" size="25" name="lastname" id="lastname" />
    <input type="button" class="button" name="postbutton" value="Post This Record" id="postbutton" onclick="formpost()" />
    </fieldset>
    </form>
    <div id="current"></div>
    </body>
    </html>
    The php process file is
    Code:
    <?php
    $domain="localhost";
    $user="root";
    $password="andrew";
    $conn = mysql_connect( $domain, $user, $password );
    $rs=mysql_select_db("ajax", $conn) or die('Could not connect: ' .mysql_error());
    $sql="INSERT INTO formpost (`firstname`,`lastname`) VALUES ({$_POST['fname']},{$_POST['lastname']})";
    $rs=mysql_query($sql,$conn);
    ?>
    The table is simply:
    db: ajax
    table: formpost
    field: firstname varchar;
    field: lastname varchar;

    The php is outputting to the mysql table but the fields are null. Each time I run the script, another row is added to the table but the fields are null.

    Would appreciate some help with this.

    Andy
    Last edited by 1andyw; Jul 23, 2007 at 10:10. Reason: Resolved


Bookmarks

Posting Permissions

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