SitePoint Sponsor

User Tag List

Results 1 to 1 of 1

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Apr 2003
    0 Post(s)
    0 Thread(s)

    Undefined index in Ajax Script

    The problem has been identified.


    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:
    <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
    <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";"POST",url,true);
    var passData="";
    request.setRequestHeader("Content-Type" , "application/x-www.form-urlencoded");
    <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;}
    <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()" />
    <div id="current"></div>
    The php process file is
    $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']})";
    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.

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


Posting Permissions

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