php - $_POST empty, sending form values with Ajax -


html

<form action='insert.php' method='post'>     <p><b>client:</b><input type='text' name='idclient'/>      <p><b>total:</b><br /><input type='text' name='total'/>       <p><input type='submit' value='save' id="btnsave"/>     <input type='hidden' value='1' name='submitted' />  </form>  

php (insert.php)

<?php     echo file_get_contents('php://input');     include_once "connect.php";       if ($db_found){         if (isset($_post['submitted'])) {             foreach($_post $key => $value) {                  $_post[$key] = mysql_real_escape_string($value);                             }               $sql = "insert `mytable` ( `idclient` ,  `total` ,  ) " .                         "values(  {$_post['idclient']} , {$_post['total']}  ) ";             mysql_query($sql) or die(mysql_error());          }            }     mysql_close($db_handle); ?> 

this works ok, when try call insert using ajax $_post function empty , cannot access values form.

this ajax code , function call:

<form action="javascript:save()" method='post'> 

ajax

function save() {                              xmlhttp = getxmlhttp(); // returns new xmlhttprequest or activexobject   xmlhttp.onreadystatechange = function(){      if(xmlhttp.readystate == 4) {        document.getelementbyid("btnsave").value = "saved";       document.getelementbyid("result").innerhtml = xmlhttp.responsetext;     }     else{       document.getelementbyid("btnsave").value = "saving...";     }   }    xmlhttp.open("post", "insert.php", true);        xmlhttp.send(null);  // need create , pass parameter string here?              } 

doing echo file_get_contents('php://input'); indeed $_post empty , parameters values not passed along.

i concatenate params value in url this

xmlhttp.open("post", "insert.php?idclient=123&total=43", true);    

but, there way use $_post , take advantage of it?

you need set content-type application/x-www-form-urlencoded values show in $_post.

for example xmlhttp.setrequestheader("content-type", "application/x-www-form-urlencoded"); (if using xmlhttprequest object).


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -