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;
    });
});
2

1 Answer 1

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
  }
});
Sign up to request clarification or add additional context in comments.

3 Comments

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
Javascript is case sensitive so these are 2 different vars. he's just missing the ampersand if he wanted a query string format
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

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.