php - Fill textarea with database value from selected option -
i have select element has values of sequences in database:
<option value="1">one</option> <option value="2">two</option> i have php code puts table data array
<?php $return_arr = array(); $sql="select * tickets_standardresponses "; $rs=mysql_query($sql,$conn) or die(mysql_error()); while($result=mysql_fetch_array($rs)) { $return_arr[] = array('sequence' => $result["sequence"], 'response' => $result["response"]); } $data = json_encode($return_arr); ?> <script type="text/javascript"> $(document).ready(function(){ var data = <?php echo $data; ?>; $('#standard_response').on('change',function() { }); }); </script> how can fill textarea based on selected option
for example, if selected option's value = 1 want put response data array sequence = 1
i not want use get/post requests using ajax
var data = <?php echo $data; ?>; $('#response').on('change', function() { var sequence = $(this).val(); //check each object in data array $.each(data, function( index, obj ) { if (obj.sequence === sequence) { //the "result" textarea $('#result').text(obj.response); } }); }); you have loop through data array , check each objects sequence , see if matches value of selected option.
note: make sure sequence parameter string or convert option value integer.
Comments
Post a Comment