Using jQuery $.ajax to post json encoded data to a php script: success event is doing nothing -
i making ajax call using jquery post data json php file, nothing happening on success
. code below :
ajax section
$.ajax({ url:"mydata.php", datatype: 'json', method:"post", data:'country', success:function(j){ var $country = $("#country"); $.each(j, function () { $country.append($('<option></option>').attr("value", this.country_id).text(this.country_name)); }); } });
php section
if(isset($_request['country'])){ $conn=new mysqli("localhost","root","","newdb"); $myquery="select * country"; $result=$conn->query($myquery); while($country=$result->fetch_assoc()){ echo json_encode($country); } }
html section
<label >country:</label> <select class="form-control" id="country" > <option>---select---</option> </select>
you need collect countries in array echo json.
$countries = []; if(isset($_request['country'])){ $conn=new mysqli("localhost","root","","newdb"); $myquery="select * country"; $result=$conn->query($myquery); while($country=$result->fetch_assoc()){ $countries[] = $country; } echo json_encode($countries); }
also in js send country parameter.
... method:"post", data: {country : true}, ...
Comments
Post a Comment