php - Value from selected dropdown option cannot be retrieved using $_POST -
i can't seem selected option value select.php value_selected.php file.the console.log(data) displayed nothing. why that?
select.php
<!doctype html> <html> <head></head> <body> <script src="js/jquery-3.2.0.min.js"></script> <form method="post" action=""> <label "sel_opt">select value: </label> <select name="sel_opt" id="sel_opt"> <option value="1">1</option> <option value="2">2</option> </select> <input name="submit" type="submit" id="submit" value="submit"> </form> <div id="result"></div> <script> $(document).ready(function(){ $("#submit").click(function(){ $.ajax({ url:"http://localhost/value_selected.php", type:"post", success:function(data) { console.log(data); $('#result').html(data); } }); }); }); </script> </body> </html> value_selected.php
<?php $output = ""; if(isset($_post["submit"])) { if(isset($_post["sel_opt"])) { $val = $_post["sel_opt"]; if ($val == 1) { $output = "<p>value 1 selected</p>"; } else { $output = "<p>value 2 selected</p>"; } echo $output; } ?>
add data: ajax config object form values.
<script> $(document).ready(function(){ $("#submit").click(function(){ $.ajax({ url:"http://localhost/value_selected.php", type:"post", data: $('form').serialize(), success:function(data) { console.log(data); $('#result').html(data); } }); }); }); </script>
Comments
Post a Comment