2

Suggestions from people on my previous question don't seem to be helpful. So, I thought of posting it again in complete code here: mysql_depricated.php:

<script>
 function fun()
 {
 var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }

   var name = document.getElementById("name").value;
   var url = "mysql_depricated2.php";
   var param = "name=" + name;

   ajaxRequest.open("POST", url, true);

//Send the proper header information along with the request
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", param.length);
ajaxRequest.setRequestHeader("Connection", "close");

    ajaxRequest.send(param); 
 }
</script>

 <input type="text" id="name">
 <input type="button" value="ajax&" onclick="fun()">

mysql_depricated2:

<?php
      $host="localhost"; // Host name
      $username="root"; // Mysql username
      $password=""; // Mysql password
      $db_name="example"; // Database name

   // Connect to server and select databse.
   $con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
   mysql_select_db("$db_name")or die("cannot select DB");


 $content = $_POST['name'];
 $content=mysql_real_escape_string($content);

 $sql = "insert into e values('$content')";
 mysql_query($sql);
?>

It does not work if I try to insert a string containing & symbol. Any solution please?

Nelson
  • 49,283
  • 8
  • 68
  • 81
Parveez Ahmed
  • 1,325
  • 4
  • 17
  • 28
  • 1
    Duplicate of your own question: [& can't be inserted in MYSQL through PHP](http://stackoverflow.com/questions/19753471/cant-be-inserted-in-mysql-through-php). If you want to provide further information then **edit** the existing question, don't ask it again. – Quentin Nov 04 '13 at 08:28

1 Answers1

4

Replace your following line:

ajaxRequest.send(param);

for this one:

ajaxRequest.send( encodeURIComponent(param) );

So you encode the & special character as its url-encoded form which is %26.

Nelson
  • 49,283
  • 8
  • 68
  • 81
  • What should I do to decode this so that it can be successfully inserted in database? @Nelson – Parveez Ahmed Nov 04 '13 at 02:24
  • You don't need to do nothing special to decode it, PHP will do that automatically for you. – Nelson Nov 04 '13 at 08:26
  • if I use encodeURIComponent and echo $content it only displays an empty string – Parveez Ahmed Nov 04 '13 at 10:17
  • 1
    Try to use the encodeURIComponent when you assign `name` ie. `var param = "name=" + encodeURIComponent(name);` otherwise it should be a problem with your ajax code, as encodeURIComponent works well as you can see in http://jsfiddle.net/Q2Z5h/ – Nelson Nov 04 '13 at 10:55
  • Thanks @nelson for your valuable help. I tried to echo the results and they were quite satisfactory. That is for example, if I input #124 then PHP echoes #124 or for input &127 PHP prints exactly &127, but if I input say { then PHP just inputs {.I don't know why..... again confused – Parveez Ahmed Nov 04 '13 at 15:17