html - How to send radio button value with AJAX and recover it with PHP -
i've been trying send value of radio button selected user , recover value php, problem can't recover value. here's code:
html
<form name="submission" action=""> <input type="radio" name="ex1" id="ex1_a" value="1"> <input type="radio" name="ex1" id="ex1_b" value="2"> <input type="radio" name="ex1" id="ex1_c" value="3"> <button class="buttons" type="submit"> submit </button> </form>
jquery script w/ ajax
$(function() { $(".buttons").click(function() { // validate , process form here var radio_button_value; if ($("input[name='ex1']:checked").length > 0){ radio_button_value = $('input:radio[name=ex1]:checked').val(); } else{ alert("no button selected, try again!"); return false; } $.ajax({ type: "post", url: "save.php", data: radio_button_value, success: function() { alert("form submitted: "+ radio_button_value); } }); return false; }); });
php
<?php $selected_button = $_post['ex1']; echo "test"; echo $selected_button; ?>
the ajax part seems work since alert displayed, don't know if sending value correctly or if php wrong, echo "test" displayed, echo $selected_button never appears. appreciate help.
in ajax function have specify parameter name value:
$.ajax({ type: "post", url: "save.php", data: {"ex1":radio_button_value}, success: function() { alert("form submitted: "+ radio_button_value); } });
Comments
Post a Comment