insert record using jquery dint work...

i am trying to insert in database using jquery ajax, but it is not working …here is codes…

in mainphp.file:


in out.js:

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

var test1 = $("#content").val();
var data1 = ‘content=’+ test1;
var testy=$("#content2").val(),
var data2= ‘content2=’+ test2;
if(test==’’)
{
}
else
{
$("#flash").show();
$("#flash").fadeIn(400).html(’ Loading Comment…’);

$.ajax({
type: “POST”,
url: “insert.php”,
//data: { data1,data2},

data : “{ ‘data1’:’” +$(’#content1’).val()+ “’, ‘data2’: '” + $(’#content2’).val()+ “’}”,

cache: false,
success: function(html){
$("#display").after(html);
document.getElementById(‘content’).value=’’;
document.getElementById(‘content’).focus();
$("#flash").hide();
}
});
} return false;
});
});

in insert.php file

<?php include('db.php'); if(isset($_POST['content1'])||isset($_POST['content2'])) { $content1=$_POST['content1']; $content2=$_POST['content2']; mysql_query("insert into student(studentName,Email) values ('$content','$email)"); $sql_in= mysql_query("SELECT studentName,id FROM student order by id desc"); $q=mysql_fetch_array($sql_in); echo "".$q['studentName'] .""; } ?>

actually my main concern is this i made it complecated earlier…i dont knw how to edit your thread so im posting here my main doubt…

i have syntax problem while transferring more than 2 values from jquery to php file …but code dint work…when i try to transfer only 1 variable it works but
not with more than 1 variable…here is the codes that im using…

var data1=$("#textbox1").val(); //textbox with name textbox1 in html page
var data2=$("#textbox1").val(); //textbox with name textbox2 in html page
$.ajax({
type: “POST”,
url: “insert.php”,
data: { data1,data2}, //i stuck in this line
cache: false,
success: function(html){
//some codes here
},
});

in insert.php file:

if(isset($_POST[‘data1’])||isset($_POST[‘data2’]))
{
echo $_POST[‘data1’];
echo $_POST[‘data2’];
}

Hi,

Here is the problem:

data: { data1,data2}, //i stuck in this line

Solution:

data: “data1=”+data1+"&data2="+data2; //

:slight_smile:

dint worked…i also tried
1)var datasent = ‘contet=’+ data1+’&txt1=’+data2;
data : datasent,
2)var datasent = ‘contet:’+ data1+’&txt1:’+data2;
data : datasent,
3)data:{ contet : $("#textbox1").val() , txt1=$("#textbox1").val()}, /

If your fields in html form have id’s “content1” and “content2” all you need is this:

$.ajax({ type: "POST", url: "insert.php", data: "data1=" + $('#content1').val() + "&data2=" + $('#content2').val(), cache: false, success: function(html){ //some codes here }, });

And be careful with variable names - in your last post you spell it contet and #textbox1, while your field ids are content1 and content2

@phphelp: only one record is updating…n thx for ur help.

What do you mean - “only one record is updating” ?
Let’s separate javascript code and php code. You’ve asked for correct syntax for jQuery .ajax method - code is provided. To test if it is working correctly, do this small change:

$.ajax({ type: "POST", url: "insert.php", data: "data1=" + $('#content1').val() + "&data2=" + $('#content2').val(), cache: false, success: function(html){ alert(html); }, });

Now, place this php code to your insert.php file (only this code, remove anything else):
[php]<?php
echo 'data1 = '.$_POST[‘data1’]."\n";
echo 'data2 = '.$_POST[‘data2’];
?>[/php]

That’s all. Now combine this code with this jQuery ajax example, and check - you should receive javascript message displaying values of 2 variables posted by ajax method to your php script.

Sponsor our Newsletter | Privacy Policy | Terms of Service