tags:

views:

408

answers:

3

Hi!

I use jquery to post data to mysql.

 **In settings.php i have this short JS code:**

 $("form#submit").submit(function() {
    var fname     = $('#fname').attr('value');
    var lname     = $('#lname').attr('value'); 
 $.ajax({
  type: "POST",
  url: "settings.php",
  data: "fname="+ fname +"& lname="+ lname,
  success: function(){
   $('form#submit').hide();
   $('div.success').fadeIn();
  }
 });
return false;
});


**And this short php:**


 if (isset($_POST['fname'])){
        $fname        = htmlspecialchars(trim($_POST['fname']));
           DO SOMETHING....
 }

This is the code where the FNAME comes from: (after hit ADD image-button then posted the fname value..)

echo "......
      <form id='submit$i' method='post'><input type='hidden' name='fname' id='fname' class='text' value='$fm_linkaz'></div><input name='add'type='image' id='add' value='$fm_linkaz' src='s.png'/></form>..........";

This is work well. But i need a SELECT element, so i changed the code to:

 ......

   echo "<select name=dropdown_list id='**ONAME**'><option value''>test</option>";
   for($i=0; $i < count($myarray); $i++){
   echo "<option value='$myarray[$i]'>$myarray[$i]</option>";}echo "</select>";

   ......</form>";

This is work well, but i dont know how can i modify the JS code, to post the selected value too.
Thank u for your help.

A: 

To get a selected value for use

jQuery('#**ONAME**').val();

Although I'm not sure if **ONAME** is valid ID, try removing the **

michal kralik
A: 

To get the value use the

$('select[name=dropdown_list]').val();

You could also use and ID, but I think you have some invalid chars in it and I doubt this will work:

$('select#**ONAME**').val();

Anyway .val() is what you are looking for. I also suggest using val() instead of attr('value').

RaYell
no stars..i hit BOLD button and so on...whatever..I try what u suggest, but i have problem with post data, because:After i hit the button, i use this short code to, test if ONAME is posted:if (isset($_POST['oname'])){ echo "Ok"; } But no echo, so something wrong with this code. The drop down menu is work, only no posted any value.. echo "....something...<form .... echo "<select name=dropdown_list id='oname'><option value''>test</option>"; for($i=0; $i < count($myarray); $i++){ echo "<option value='$myarray[$i]'>$myarray[$i]</option>"; }echo "</select>";...........</form>
A: 

First of all the javascript code needs a few updates:

$('#fname').val() is better than $('#fname').attr('value') -- .val() will work on selects/checkboxes as well - where .attr('value') won't be reliable.

Second: the data parameter to your $.ajax() call can take a json object (which it will convert to the form string)

$.ajax({
  type: "POST",
  url: "settings.php",
  data: {
    'fname': $('#fname').val(),
    'lname': $('#lname').val(),
    'oname': $('#oname').val()
  },
  success: function(){
    $('form#submit').hide();
    $('div.success').fadeIn();
  }
});

There is a plugin that makes this much easier:

$("form").ajaxForm({
   success: function(){
     $('form#submit').hide();
     $('div.success').fadeIn();
   }
});

UPDATED:

Also - the <select> element was named "dropdown_list" - perhaps you wanted it to be submitting data as "oname" instead. Form elements use the "name" property to submit, the id property only makes css/js selectors easier to code.

gnarf
i did changed as you said..but, after i hit the button, i use this to, test if ONAME is posted...if (isset($_POST['oname'])){ echo "Ok";} But no echo, so something wrong with this code. The drop down menu is work, only no posted any value..echo "....something...<form ....echo "<select name=dropdown_list id='oname'><option value''>test</option>";for($i=0; $i < count($myarray); $i++){ echo "<option value='$myarray[$i]'>$myarray[$i]</option>";}echo "</select>";...........</form>
Your field "name" is 'dropdown_list' - not 'oname' is that perhaps the problem? Check $_POST['dropdown_list'] ?
gnarf
Also - I noticed in another comment that you had <select id='oname'> not <select id='**oname**'> - updated the code to select $("#oname") instead of $("select[@name=dropdown_list]"); - Seems <select name='oname' id='oname'> in the php will probably fix your problem though.
gnarf
u've right..:) with $_POST['dropdown_list'] the check is ok..i get the selected value from SELECT element..but the ajax part of code still not work...After check the POST then:$fname= htmlspecialchars(trim($_POST['fname']));$oname= htmlspecialchars(trim($_POST['oname']));But i get undefinied index oname error...So something still wrong with ajax part...
No - $_POST['oname'] isn't set in the ajax request - just change the name of your select to 'oname' and alot your problems will probably go away - you'll need to change it from 'dropdown_list' to 'oname' in the ajax submit too (the answer was updated to include that change)
gnarf
ohh ..ic . what a lame am i...Thank you very much, you r the best;)
Sorry Gnarf! One more question pls!Something wrong with the success: function(), because everything ok, but the div did not show up...could you suggest something?
Make sure the div is class success, and a part of the DOM? Try doing $('div.success').fadeIn() without the ajax - make sure it still works?
gnarf
Without ajax, work. Its ok so div is class success, but the "part of the DOM" thing..i dont understand..how can i check this?
If the success function isn't being called, you will probably want to make error:function(XHR, textStatus) { alert([XHR.status, textStatus, XHR.responseText].join("\n")); } a part of your ajax request - this should let you see more information if the ajax request fails. If $('div.success').fadeIn() works without the ajax request - I would assume the success() function isn't being called - try adding some alerts and see...
gnarf
the error function did not show any information..i dont know what is the problem..im tryin'. Thanky you.