0
votes

I have tried a lot but I have not been able to find out what is wrong with this function to save two values into database. It has been working fine for another function to save one value. It behaves very strange here. Sometimes send 'parent' value & sometimes stop sending it but never send msg value. Here is function. It works fine for one input i.e. parent but problems start with the addition of 2nd input.

<script> 
function ADDLISITEM(form)
{ 
var parent = form.txtInput.value;
    var msg    = form.msgInput.value;
    form.txtInput.value = "";
    form.msgInput.value = "";
    var url = "send_mysql.php"
var request = null;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
request=new XMLHttpRequest();
 }
else
{// code for IE6, IE5
request=new ActiveXObject("Microsoft.XMLHTTP");
}
request.open("POST", url, true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.setRequestHeader("Connection", "close");
request.onreadystatechange = function(){
    if (request.readyState == 4) {
        if (request.status == 200) {
            //alert('POST');
    } else {
        alert(request.status); // fails here
    }
    }
}
request.send("parent=" + encodeURIComponent(parent).replace(/%20/g, '+')+"&msg=" + 
    encodeURIComponent(msg).replace(/%20/g, '+'));
}
</script>

This is send.php

$username = "babar";
$password = "k4541616";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect
to MySQL");
$selected = mysql_select_db("spec",$dbh) or die("Could not select first_test");

  //die(var_export($_POST,TRUE));

  $parent = $_POST['parent'];
  $msg = $_POST['msg'];
  $name   = 'Akhtar Nutt';
  //$parent2 = json_decode($parent);
  $msg_ID = '2q7b2sfwwe';
  //$msg2    = json_decode($msg);
  $query  =  "INSERT INTO msg2_Qualities(id,name,msg,msg_id,parent) VALUES  
  ('','$name','$msg','$msg_ID','$parent')";
    if(!mysql_query($query, $dbh))
        {die('error:' .mysql_error())
      ;}

?>

1

1 Answers

2
votes

Alter

request.send("parent=" + encodeURIComponent(parent).replace(/%20/g, '+')+"msg=" + encodeURIComponent(msg).replace(/%20/g, '+'));

to:

request.send("parent=" + encodeURIComponent(parent).replace(/%20/g, '+')+"&msg=" + encodeURIComponent(msg).replace(/%20/g, '+'));

You're missing the argument separator & in your query string...

You also might want to refrain from using values in $_REQUEST as they aren't reliable. If your script expects data from a POST then retrieve these values from $_POST.