943,172 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1621
  • PHP RSS
Sep 2nd, 2010
0

Retrieve PHP Variable with jQuery

Expand Post »
Hi,
I'm somewhat new to jQuery/JavaScript, so any help would be greatly appreciated.
I have a few textboxes that need to be populated when a user clicks a hot spot on an image map. Each hot spot has an alt tag representing the ID number of the object I need to retrieve from a database.
To retrieve the info from the DB, I have a small PHP script that is called with jQuery's .post() function, something to the effect of:
JavaScript Syntax (Toggle Plain Text)
  1. $.post($('#myForm').attr('action'),formInput, function(data){ ... });
My question is how do I use jQuery to get and manipulate the data that the PHP function retrieves from the DB?

Thanks in advance for any assistance.
- EF
Reputation Points: 40
Solved Threads: 31
Junior Poster
EvolutionFallen is offline Offline
198 posts
since Aug 2009
Sep 2nd, 2010
0
Re: Retrieve PHP Variable with jQuery
Reputation Points: 26
Solved Threads: 31
Posting Whiz
MooGeek is offline Offline
349 posts
since Mar 2009
Sep 2nd, 2010
0
Re: Retrieve PHP Variable with jQuery
or atleast try this one
http://lmgtfy.com/?q=jquery+php+ajax
Reputation Points: 26
Solved Threads: 31
Posting Whiz
MooGeek is offline Offline
349 posts
since Mar 2009
Sep 2nd, 2010
0
Re: Retrieve PHP Variable with jQuery
I understand that I can use success: function(html) { $('#response').html(html); to print any code that was echo'ed by the php script. But what I want to do is return an array from the script and manipulate it to output its contents in specific places. How could I do that?
Reputation Points: 40
Solved Threads: 31
Junior Poster
EvolutionFallen is offline Offline
198 posts
since Aug 2009
Sep 2nd, 2010
1
Re: Retrieve PHP Variable with jQuery
hmmm. okay you just have to use your imagination.

first, there is a split function in JS: that is http://stackoverflow.com/questions/2...g-using-jquery

so, you can just put delimiter on the echo'd data coming from the PHP script.

for example

PHP Syntax (Toggle Plain Text)
  1. var str = html; //assuming the data is hello||john||world
  2. var arr = str.split("");
  3. //now doing this will give you 3 separate data
  4. document.write(arr[0]);
  5. document.write(arr[1]);
  6. document.write(arr[2]);

Hope this helps
Cheers!
Reputation Points: 26
Solved Threads: 31
Posting Whiz
MooGeek is offline Offline
349 posts
since Mar 2009
Sep 2nd, 2010
0
Re: Retrieve PHP Variable with jQuery
Thanks, this is the idea that was starting to form in my head. I originally wanted to return an associative array because it would be easier for me to manipulate, but I think I'll just echo a string with a delimiter and split() it. Glad to see I was headed in the right direction. Thanks!
Reputation Points: 40
Solved Threads: 31
Junior Poster
EvolutionFallen is offline Offline
198 posts
since Aug 2009
Sep 2nd, 2010
0
Re: Retrieve PHP Variable with jQuery
oh! I've forgotten!!

the function should be

var arr = str.split("||");
not var arr = str.split("");
Reputation Points: 26
Solved Threads: 31
Posting Whiz
MooGeek is offline Offline
349 posts
since Mar 2009
Sep 2nd, 2010
0
Re: Retrieve PHP Variable with jQuery
btw it's impossible to retrieve an Array Variable from a PHP script to JQuery AJAX. it is always a raw data.

Cheers!
Reputation Points: 26
Solved Threads: 31
Posting Whiz
MooGeek is offline Offline
349 posts
since Mar 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Is It Possible To Create An "adult Content Filter" In Php?
Next Thread in PHP Forum Timeline: Need wordpress tutorial





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC