Rohitashv Singhal
Rohitashv Singhal

Reputation: 4557

unable to send complete data into database using ajax

i m using the following javascript to send the data into database

var x=document.getElementById("sortable").innerHTML;
    alert(x);
    var http=new XMLHttpRequest();
        http.open("POST", "5.php?field="+x, true);
        http.onreadystatechange = function() 
        {
            if(http.readyState == 4 && http.status == 200) 
            {
                alert(http.responseText);

            }
        }

        http.send();

on php side i m using the following functions:

$l=$_GET['field'];
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("formdem", $con);
$sql="INSERT INTO rohit(content)VALUES('$l')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "<br/>&nbsp&nbsp&nbsp Your Form is now ready to be filled.....";
$rs= mysql_query("Select * from rohit where content='$l'");
if(mysql_num_rows($rs)!=0)
    {
    while($row = mysql_fetch_array($rs)) {

      echo "<br/>Your unique id is ".$row['formid'];


      }
      }

but this code is not sending the complete data into database. what may be the reasons. in database i have taken field as longtext. thnx

Upvotes: 0

Views: 87

Answers (1)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382102

As your innerHTML is some HTML, it can't be simply added to make an URL. You can't do this :

http.open("POST", "5.php?field="+x, true);

You have to urlEncode x before :

http.open("POST", "5.php?field="+encodeURIComponent(x), true);

Upvotes: 1

Related Questions