I've a form with one input field to submit the student id via AJAX and I'm fetching some JSON result from the AJAX request. Now the returned data from AJAX request need to be assigned to ng-init in the input field (phone) below the form. Please help me how to assigning this value to ng-init. Thank you!
HTML
<div ng-app="">
<form id="std_form">
<input type="text" name="sid" id="sid">
<input type="submit">
</form>
<input type="text" name="phone" ng-model="phone" ng-init="" id="phone">
Ph: {{phone}}
</div>
jQuery
$('#std_form').submit(function(e){
e.preventDefault();
var val = $('#sid').val();
var dataString = "sid=" + val;
$.ajax({
type: "POST",
url: "post_process.php",
data: dataString,
dataType: 'json',
success: function(data){
$('#phone').val(data.phone);
}
});
});
post_process.php
<?php
if(ISSET($_POST['sid'])) {
$sid = $con->real_escape_string($_POST['sid']);
$sql = "SELECT phone FROM std_detials WHERE sid = $sid";
$result = $con->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$phone = $row['phone'];
}
$json = array('phone' => $phone);
$json = json_encode($json);
echo $json;
}
?>