0

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 Answer 1

2

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.

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

5 Comments

I tried both but it make no difference, Yes, that was missing I add it but it also does not make an difference.
OK. Then let send.php do a "die(var_export($_POST,TRUE))" right at the start. What's the result when sending data via XHR then?
@ goreSplatter, can you explain a bit. I put die(var_export($_POST,TRUE)); right in the start of send_mysql.php i.e after database connection. Whats do you mean by 'sending data via XHR '?
@ goreSplatter, OK, I dont't know what was wrong but it started working eventually though I tried this way before, I removed json encode/decode & send it as a string. thanX for your '&' correction as that was also an issue.
XHR is the XMLHttpRequest object. In onreadystatechange you can check the response of your script. The output of var_export which would have been captured could have given you more clues. Anyways... Thanks for accepting and happy coding.

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.