【发布时间】:2016-10-23 12:37:49
【问题描述】:
我有一个带有一个输入字段的表单,用于通过 AJAX 提交 student id,我正在从 AJAX 请求中获取一些 JSON 结果。现在需要将 AJAX 请求返回的数据分配给表单下方输入字段(电话)中的ng-init。请帮助我如何将此值分配给ng-init。谢谢!
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;
}
?>
【问题讨论】:
-
需要修改访问php的代码并获取值
-
@Sajeetharan 请告诉我应该对我的代码进行哪些更改?
-
这个提交全错了,根本不应该涉及jQuery。
ng-init的想法也是错误的。建议大家学习一些angular form教程,了解ng-model和ng-submit -
@charlietfl 我知道这是错的,你能纠正我的错误吗?
-
试图解释这种方法是完全错误的。见thinking-in-angularjs-if-i-have-a-jquery-background。尽管我个人更喜欢
ng-submit,但下面提供的解决方案更符合角度方法。 Angular 还内置了您可以利用的验证
标签: php jquery angularjs ajax angularjs-ng-init