Skip to content Skip to sidebar Skip to footer

Read Data From HTML Select Tag Before Sending To PHP

I need to send a option to a PHP script, but the instead of a user clicking on a submit button, they simply click a type link. For instance: So the user will select 1, then clic

Solution 1:

You Have to use <form> tag to make this possible or use ajax submit tu submit your form with <form> you can do in this way

<form action="next.php" method="POST">
 <select name="test">
   <option value="1">1</option>
 </select>
 <!-- Then after this don't use anchor tag <a> use input submit instead-->
<input type="submit" name="submit" value="Next"/>
</form>

Or you can also submit it with <button> :

<form action="next.php" method="POST">
 <select name="test">
   <option value="1">1</option>
 </select>
 <!-- Then after this don't use anchor tag <a> use button instead-->
<button type="submit">Next</button>
</form>

as you mentioned in question you want to get the value of select before submit to php you can use jQuery ajax to do this look below

<form id="my_form" action="">
 <select id="my_tag" name="test">
   <option value="1">1</option>
 </select>
<button type="submit">Next</button>
</form>

Ajax:

$('#my_form').submit(function(e){
   if($('#my_tag').val() !== '') {
    var select = $('#my_tag').val();
    $.ajax({
         type: 'Get', // Could be GET/POST
         url: 'php.php?select='+select, //targeting url with value from select tag
         success: function(data){
         alert(data); // alerting the data which php file returned.
         }
     });
    } else {
       alert('error'); // if select tag is empty we will show an error.
    }
e.preventDefault();
});

Post a Comment for "Read Data From HTML Select Tag Before Sending To PHP"