tags:

views:

50

answers:

3

How can I post data using Jquery from selected data to the PHP?

HTML code:

<select id="test" title="test" class="test">
  <option value="1">1
  <option value="2">2
  <option value="3">3
  <option value="4">4
  <option value="5">5
 </select>

Jquery:

$('.18').click(function(event) {
  var test_s = $('#test').val();
  $('.opc_mfont').load("index.php?x=18&fid=<?=$fid;?>&mid=<?=$mid;?>&data=' + test_s +'");
  event.stopPropagation();
});

All the data was successfully post to PHP and MySql query except ' + test_s + ' at the and of jquery load().

I was tested that data received from select fields with "alert(test_s);" and the alert message show up with correct data on it. But I can't send "var test_s" to PHP using jquery load(). How can I do that?

+2  A: 

read

from jQuery .load():

The POST method is used if data is provided as an object; otherwise, GET is assumed.

read here: http://api.jquery.com/load/

code

untested, probably something like this:

$('.opc_mfont').load("index.php?x=18&fid=<?=$fid;?>&mid=<?=$mid;?>", {'data':test_s});
Adam Kiss
This is the solution for my problem. Thanks Adam.
Sergio
+2  A: 

You mixed up single and double quotes:

$('.18').click(function(event) {
  var test_s = $('#test').val();
  $('.opc_mfont').load('index.php?x=18&fid=<?=$fid;?>&mid=<?=$mid;?>&data=' + test_s);
  event.stopPropagation();
});
middus
+3  A: 

Your problem is your string:

"index.php?x=18&fid=<?=$fid;?>&mid=<?=$mid;?>&data=' + test_s +'"

That's not string concatenation - you're mixing your string delimiters. You want:

"index.php?x=18&fid=<?=$fid;?>&mid=<?=$mid;?>&data=" + test_s
beamrider9