?
Solved

Correct way to send multiple data for $.post()

Posted on 2009-05-16
3
Medium Priority
?
661 Views
Last Modified: 2012-05-07
I need to send some data to a php script. Some of the data is coming from a textarea and some of it is coming from a variable (advice_ID) defined earlier in the javascript. Below is the code that I am using trying to send the data.

when I leave out the "+ {ID : advice_ID}" the code inputs form_data fine but I need to send that advice_ID.

when I leave in "+ {ID : advice_ID}" the code adds "[object Object]" to the end of the form_data.

don't know if that helps you diagnose the problem.
$('#comment_form').submit(function() {
var form_data = $(this).serialize();
			$.post('../advice/php/post_comment.php', form_data + {ID : advice_ID}, comment_callback);
							function comment_callback(data) {
					}
			return false;
								   
});

Open in new window

0
Comment
Question by:Sucao
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 12

Expert Comment

by:alien109
ID: 24406357
Hard to know for sure, without seeing what advice_ID is getting set to and how, but I'm guessing that advice_ID is not a string. Can you show how/where that variable is getting set?
0
 

Author Comment

by:Sucao
ID: 24406593
Yea sure alien109. It's getting set earlier from a $.getJSON().

also, usually it gets set as something like '47' or some other number. not sure if that counts as being set as a string or just as a number.

sorry for my newbie ways on this one.
$.getJSON('../advice/php/get_advice_start.php', {ID : top.window.location.hash.substring(1)}, process_advice);
			function process_advice(data) {
advice_ID = data.id;
}

Open in new window

0
 

Accepted Solution

by:
Sucao earned 0 total points
ID: 24415122
This is what Alien109 posted in a different post based on the same question.


the result of a the serialize method is a string. To add your ID parameter, you'd need to do this:

$.post('../advice/php/post_comment.php', form_data + "&ID=" + advice_ID, comment_callback);


0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question