0

inserting values into database more then one with ajax and php, using this code inserting only one value i dont know why please help me out.

PHP coding

$content=$_POST['content'];
$text=$_POST['text'];
mysql_query("insert into ajax(ajax,text) values ('$content','$text')");

ajax coding

$(function() {
    $(".comment_button").click(function() {

        var test = $("#content").val();
        var text = $("#text").val();

        var dataString = 'content='+ test;
        var datastring = 'text='+ text;

        if(test=='') {
            alert("Please Enter Some Text");        
        } else {
            $("#flash").show();
            $("#flash").html('<img src="loading.gif" align="absmiddle">&nbsp;<span class="loading">Wait..!</span>');            
            $.ajax({
                type: "POST",
                url: "demo_insert.php",
                data: dataString + datastring,
                cache: false,
                success: function(html) {
                    //document.getElementById('content').value='';
                    $("#flash").hide();
                    alert("Values Inserted Successfully");
                    //document.write("values inserted Successfully..."); // to show message as string           
                }
            });
        }

        return false;
    });
});
Baptiste Pernet
  • 3,318
  • 22
  • 47
Mani
  • 37
  • 5
  • At least try that: `var datastring = '&text='+ text;` or read some basic tutos how to send data to server using $.ajax – A. Wolff Oct 13 '14 at 16:03
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 13 '14 at 16:13

1 Answers1

1
var dataString = 'content='+ test;
var datastring = 'text='+ text;

In the above code you are re-declaring the variable which overrides the first variable's data

The best way for you to do this is

$.ajax({
  type: "POST",
  url: "demo_insert.php",
  data: {content:test, text: text},
  cache: false,
  success: function(html){
 //document.getElementById('content').value='';
 $("#flash").hide();
 alert("Values Inserted Successfully");
 //document.write("values inserted Successfully..."); // to show message as string
  }
});
Dharam
  • 423
  • 2
  • 10
  • Dear @Dharam thanks for your kind attention, my query is solved with your guide line.... but i have one more question..if i use var dataString = 'content='+ test; var string = 'text='+ text; then how i will use the code...plz guide me..thx – Mani Oct 13 '14 at 16:14
  • Javascript is case sensitive so these are 2 different vars. he's just missing the ampersand if he wanted a query string format – cyberwombat Oct 13 '14 at 16:14
  • You can use the following `Var dataString = 'content='+test; dataString += '&text='+text; //Note when you use var - it redeclares the variable, and note that in this line we are concatenating the earlier declared variable. Wrote this without testing, but this should work.` Also If all these data are coming from a form you can also use $('#formid').serialize(); and pass this directly – Dharam Oct 14 '14 at 06:48