We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,973 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to Post Multiple Data with Ajax(Jquery)?

Hi all..
I got this code that dealing with ajax..Basically the ajax code is to get the data from the html element and insert it to the database.

this is ajax and the html code :

<script type="text/javascript">
$(document).ready(function(){
 $("form#post_reply").submit(function() {
 
 var discuss_text = $('#discuss_text').attr('value');
 var test5 = $('#test5').attr('value');
	
 $.ajax({
	type: "POST",
	url: "reply.php",
	data: "discuss_text="+ discuss_text,
	success: function(){
	          $("ul.reply_text").prepend("<li style='display:none' class='stbody'>   <div class='stimg'></div>
<div class='sttext'>"+discuss_text+
"<div class='below'>
<div class='sttime'>2 seconds ago</div>
<div class='like'>| like |</div>
<div class='dislike'>dislike |</div>
</div></div></li>");
   $("ul.reply_text li:first").fadeIn();
	       }
		});	
	return false;
	});	
});
</script>
<form name='disscuss_text' id='post_reply' id='post_reply'>
   <input type="hidden" name="test5" id="test5" value="<?php echo $row['id']; ?>">
   <textarea rows='3' cols='87' id='discuss_text' name='discuss_text'></textarea>
   <input type='submit' value='Post' name='Help' id='post-button' />
</form>

and this is the reply.php file :

<?php
// Change this parameter with your connection's info.
$db_host="localhost";
$db_name="*******";
$username="******";
$password="******";
$db_con=mysql_connect($db_host,$username,$password);
$connection_string=mysql_select_db($db_name);
// Connection
mysql_connect($db_host,$username,$password);
mysql_select_db($db_name);
?>
<?php
if(isset($_POST['discuss_text'])){
	/* Connection to Database */

	/* Remove HTML tag to prevent query injection */
	$message = strip_tags($_POST['discuss_text']);
	
	$sql = 'INSERT INTO reply (message) VALUES("'.$message.'")';

	mysql_query($sql);
	
	echo $message;
	} else { }
?>

So, if you notice,there is this code above :

<input type="hidden" name="test5" id="test5" value="<?php echo $row['id']; ?>">

$row value is what i am trying to insert into the 'reply' database into the post_id table..
So how to add one or more data to this line of code "data: "discuss_text="+ discuss_text," ??
so that i could be able to insert more than just one data at a time into the database by using ajax..


ask me if unclear bout this question,
Thanks for helping :)

2
Contributors
1
Reply
1 Day
Discussion Span
2 Years Ago
Last Updated
6
Views
Question
Answered
samsons17
Posting Whiz in Training
246 posts since Oct 2009
Reputation Points: 6
Solved Threads: 1
Skill Endorsements: 0

Hi all..
I got this code that dealing with ajax..Basically the ajax code is to get the data from the html element and insert it to the database.

this is ajax and the html code :

<script type="text/javascript">
$(document).ready(function(){
 $("form#post_reply").submit(function() {
 
 var discuss_text = $('#discuss_text').attr('value');
 var test5 = $('#test5').attr('value');
	
 $.ajax({
	type: "POST",
	url: "reply.php",
	data: "discuss_text="+ discuss_text,
	success: function(){
	          $("ul.reply_text").prepend("<li style='display:none' class='stbody'>   <div class='stimg'></div>
<div class='sttext'>"+discuss_text+
"<div class='below'>
<div class='sttime'>2 seconds ago</div>
<div class='like'>| like |</div>
<div class='dislike'>dislike |</div>
</div></div></li>");
   $("ul.reply_text li:first").fadeIn();
	       }
		});	
	return false;
	});	
});
</script>
<form name='disscuss_text' id='post_reply' id='post_reply'>
   <input type="hidden" name="test5" id="test5" value="<?php echo $row['id']; ?>">
   <textarea rows='3' cols='87' id='discuss_text' name='discuss_text'></textarea>
   <input type='submit' value='Post' name='Help' id='post-button' />
</form>

and this is the reply.php file :

<?php
// Change this parameter with your connection's info.
$db_host="localhost";
$db_name="*******";
$username="******";
$password="******";
$db_con=mysql_connect($db_host,$username,$password);
$connection_string=mysql_select_db($db_name);
// Connection
mysql_connect($db_host,$username,$password);
mysql_select_db($db_name);
?>
<?php
if(isset($_POST['discuss_text'])){
	/* Connection to Database */

	/* Remove HTML tag to prevent query injection */
	$message = strip_tags($_POST['discuss_text']);
	
	$sql = 'INSERT INTO reply (message) VALUES("'.$message.'")';

	mysql_query($sql);
	
	echo $message;
	} else { }
?>

So, if you notice,there is this code above :

<input type="hidden" name="test5" id="test5" value="<?php echo $row['id']; ?>">

$row value is what i am trying to insert into the 'reply' database into the post_id table..
So how to add one or more data to this line of code "data: "discuss_text="+ discuss_text," ??
so that i could be able to insert more than just one data at a time into the database by using ajax..


ask me if unclear bout this question,
Thanks for helping :)

Yes, you can pass multiple variables through jqueyr ajax

<script type="text/javascript">
function resultPublish()
{
	 var myindex  = ....
	 var selValue = ....
	 
	  $.ajax({
		url: "yourPage.php",	
		type: "GET",		
		data: "data1="+myindex+"&data2="+selValue,	
		cache: false,
		success: function (html) {	
		
			...do something	
		}		
	});
}
</script>

In "yourPage.php" you can access "data1" and "data2" like following

echo $_GET['data1'];
echo $_GET['data2'];

Hope it will help you

six_sic6
Light Poster
34 posts since Feb 2011
Reputation Points: 10
Solved Threads: 5
Skill Endorsements: 0
Question Answered as of 2 Years Ago by six_sic6

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0565 seconds using 2.7MB