3
votes

I am currently using Ajax to submit an input field without a page refresh or button click. The function works well with a text input field But it doesnt work with posting the value of a select box and then php echoing the result. I check with the firebug tool and nothing is being posted by Ajax/js function.

How can I submit the value of a select box so I can then echo with the php? EXAMPLE

JS

<script>
$(document).ready(function() {
        var timer = null; 
        var dataString;   
          function submitForm(){
                         $.ajax({ type: "POST",
                         url: "index.php",
                         data: dataString,
                         success: function(result){
                         $('#item_input').text( $('#resultval', result).html()); 
                             }
                            });
                            return false;
           }
             $('#item_name').on('keyup', function() {
             clearTimeout(timer);
             timer = setTimeout(submitForm, 050);
             var name = $("#item_name").val();
             dataString = 'name='+ name;
          });
 }); 
</script>

PHP

<?php
      if ($_POST)
                {
                  $item_name     = $_POST['name'];
                  echo ('<div id="item_input"><span id="resultval">'.$item_name.'</span></div>');
                }
?>

HTML

<html>
<form method="post" id="form" name="form">  
<select name="item_name" value="<? $item_name ?>" size="4" id="item_name">
     <option value="">Item1</option>
     <option value="">Item2</option>
     <option value="">Item3</option>
     <option value="">Item4</option>
</select>
</form>
<div id="item_input"></div>
</html>
4

4 Answers

4
votes

select tags does not trigger keyup event , you should use change instead, try the following:

 $('#item_name').on('change', function() {
     clearTimeout(timer);
     var name = $(this).val();
     dataString = 'name='+ name;
     timer = setTimeout(submitForm, 050);
 });

 $('#item_input').html(result); 
1
votes

It seems that your js is right problem is in your html part. you not provided the select list values. pls provide values to select list options.

$(document).ready(function() {
     var timer = null; 
     var dataString;   
     function submitForm(){
       $.ajax({ type: "POST",
                url: "index.php",
                data: dataString,
                success: function(result){

                //$('#item_input').html( $('#resultval', result).html()); 
                //$('#special').text(result); 
                //$('#item_input').html( $('#resultval').html() + '<p>' + result + '</p>'); 
                $('#item_input').html(result); 

                }

       });
       return false;
     }


    $('#item_name').on('change', function() {
     clearTimeout(timer);
     var name = $(this).val();
     dataString = 'name='+ name;
     timer = setTimeout(submitForm, 050);
    });      

}); 

it should be like this or whatever values you want to post

<select name="item_name" value="" size="4" id="item_name">
    <option value="item1">Item1</option>
    <option value="item2">Item2</option>
    <option value="item3">Item3</option>
    <option value="item4">Item4</option>
</select>
0
votes

Trigger submitForm() with an onchange event so that every time the value of <select> changes, it submits.

0
votes

KeyUp is for input boxes and others that use the keyboard. Select boxes you can either use onClick or onChange, preferrably onChange:

$('#item_name').change(function() {
             clearTimeout(timer);
             timer = setTimeout(submitForm, 050);
             var name = $("#item_name").val();
             dataString = 'name='+ name;
}

This will work for you.

Good Luck!