Have an account? Sign in
Login  Register  Facebook
data option in jQuery.ajax() and mysql_query?
I modify a php comment system. I want add it after every article witch are query from database.

this is the php part
<?php
...
while($result = mysql_fetch_array($resultset))
{
$article_title = $result['article_title'];
...
?>
<form id="postform" class="postform">
<input type="hidden" name="title" id="title" value="<?=$article_title;?>" />
<input type="text" name="content" id="content" />
<input type="button" value="Submit" class="Submit" />
</form>
...
<?php
}
?>
this is the ajax part.
$(function($) {
$(document).ready(function(){
$(".Submit").click(function(){
var id = $(this).attr("id");
var name = $(this).attr("name");
var dataString = 'id='+ id ;
var parent = $(this);
var anyBlank = 0;
if(anyBlank == "0")
{
var title    = $("#title").val();
var content   = $("#content").val();
$.ajax({
type: "POST",
url: "ajax_post.php",
data: "title="+title+"&content="+content,
success: function(date_added){
if(date_added != 0)
{
structure = '<div class="comment_date_added">'+date_added+'</div><div id="comment_text"><div id="comment_content">'+content+'</div>';				  	
$("#post_comment").prepend(structure);
}
});
});
ajax_post.php
echo $title;
echo $content;


How to modify the ajax data part that each article's comment can send each data to the ajax_post.php? thanks a lot.
Started: September 22, 2011 Latest Activity: September 22, 2011 ajax php
1 Answer
The "data" property of a $.ajax request is actually a json object. jQuery deals with serializing and getting the data to the server automatically.
You want to do: data: { title: title, content: content }
On the PHP end:
$title = $_POST['title']
$content = $_POST['content']

Posted: MacOS
In: September 22, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer