Print Select Option Using Ajax, Jquery and a PHP Echo -
i have looked throughout stackoverflow , seen numerous examples of none give answer how echo. have select dropdown on page called testing.php:
<form id="projects" action="project-add.php" method="post"> <select name="territory" id="territory"> <option value="australia">australia</option> <option value="argentina">argentina</option> </select> // fair few more form fields here <input type="submit" value="submit project"> </form>
i need whatever selected here available echo php variable later down form without need press button.
i have following script in :
<script type='text/javascript'>//<![cdata[ $(window).load(function(){ $('#territory').change(function(e) { e.preventdefault(); var selectedoption = $(this).find('option:selected'); $('#territory option').removeattr('selected'); $(selectedoption).attr('selected','selected'); var selectedoptionvalue = $(selectedoption).val(); var selectedoptiontext = $(selectedoption).text(); $.ajax({ url: 'testing.php', type: 'post', data: {data : selectedoptiontext}, datatype: 'text', success: function(data){ } }); });//]]> }); </script>
i print:
<? echo $_post['data']; ?>
but isn't working. appreciated.
thanks in advance
1. need able trigger change
<select name="territory" id="territory"> <option value="">please select</option> <option value="australia">australia</option> <option value="argentina">argentina</option> </select>
2. call php selected option
$(function() { // on page load $('#territory').on("change",function(e) { $("#someoutputcontainerid").empty(); // clear container var selectedoptionvalue = $(this).val(); if (selectedoptionvalue) { // in case select "please select" $.ajax({ url: 'testing.php', type: 'post', data: {data : selectedoptionvalue }, datatype: 'text', success: function(data){ $("#someoutputcontainerid").html(data); } }); } }); });
the selected value available in testing.php $_post["data"]
, echoed in testing.php show in container
Comments
Post a Comment