0

I'm trying to submit Multiple Form Items from HTML using Java-script into mySQL via AJAX request. I can get one field to update but not the other 2. I've commented out the other code I believed would aid in this but was not working.

 HTML part: 
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<head>

<script type="text/javascript">

function insert()  {

      if (window.XMLHttpRequest) {
      xmlhttp = new XMLHttpRequest();
   } else {
       xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
   }
   xmlhttp.onreadystatechange = function(){
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    document.getElementById('message').innerHTML = xmlhttp.responseText;
  }
  }

 parameters = 'fname='+document.getElementById('fname').value;


   xmlhttp.open("POST", "update.php", true);
   xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
   xmlhttp.send(parameters);

} ;

 </script>

 </head>

 <body>

          First Name: <input class="work" type="text" id="fname" ><br>
          Middle Name: <input class="work" id="mname" type="text"><br>
          Last Name: <input class="work" id="lname" type="text"><br>
          <input type="button" value="Submit" onclick="insert();">

          <div id="message"></div>

  </body>
  </html>



 PHP part:
<?php
       //require 'connect.midasproject.php';
   $conn_error = 'could not connect.';

        $dbhost = "localhost";
        $dbname = "mastergolddb";
        $dbuser = "root";
        $dbpass = "";

 if (!@mysql_connect("$dbhost", "$dbuser", "$dbpass")||!@mysql_select_db ("$dbname")) {
   die($conn_error);
 } else {
   echo 'connected.';
   }

$fname = $_POST['fname'];
//$mname = $_POST['mname'] ;
//$lname = $_POST['lname'];

 if (!empty($fname))  {

$query = "INSERT INTO `customers` VALUES ('','$fname','joe','blow')";
    if  ($query_run = mysql_query($query)) {
       echo 'data inserted.' ;
    } else {
  echo 'Query failed.';
    }
   }

?>
5
  • You didn't send the other fields, you only sent the fname field. Commented Sep 20, 2012 at 20:52
  • And you need to use those fields again in your SQL query, '{$fname}',{$mname}', etc Commented Sep 20, 2012 at 20:56
  • How do I pass the other values? Commented Sep 20, 2012 at 22:16
  • I've tried multiple combinations of adding the other mname and lname inputs fields to the 'parameters' but code fails. When I create variables $mname = $_POST['mname'] ; $lname = $_POST['lname']; the also code fails. Commented Sep 20, 2012 at 22:54
  • I've used them in my SQL query, I put in Joe and blow as constants to show code that works but when I pass in the $mname and $lname it doesnt work. Commented Sep 20, 2012 at 22:56

1 Answer 1

1

you need to pass all 3 values to the ajax function. you seem to be passing just fname here parameters = 'fname='+document.getElementById('fname').value;

Sign up to request clarification or add additional context in comments.

1 Comment

How do I pass the other values? BTW- the HTML code of this didnt post.. I'm trying how to edit the question to append with that now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.