HTML代码
<div class="ui grid">
<div class="eight wide column">
<form method="POST" class="ui form">
<input type="hidden" name="act" value="{% if act == 'edit' %}edit{% else %}add{% endif %}">
<input type="hidden" name="id">
<div class="field">
<label>First Name</label>
<input name="firstName" type="text" id="firstName" placeholder="First Name" value="{% if act == 'edit' %}{% endif %}">
</div>
<div class="field">
<label>Last Name</label>
<input name="lastName" type="text" id="lastName" value="{% if act == 'edit' %}{% endif %}" placeholder="Last Name">
</div>
<button class="ui button" type="submit">
{% if act == "edit" %}
<i class="save icon"></i>
Edit
{% else %}
<i class="save icon"></i>
Add
{% endif %}
</button>
</form>
</div>
<div class="eight wide column">
<table class="ui celled table">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr id="user-{{ user.test_id }}">
<td>{{ user.test_first }}</td>
<td>{{ user.test_last }}</td>
<td>
<div href="#" class="ui compact icon button update user" data-user-id="{{ user.test_id }}">
<i class="edit icon"></i>
</div>
<div href="#" class="ui red compact icon button delete user" data-user-id="{{ user.test_id }}">
<i class="remove user icon"></i>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
的Ajax
$('.button.update.user').click(function(){
var userID = $(this).data('user-id');
$.get(
site_url + userID,
function (users) {
$('#firstName').text(user.test_first);
$('#lastName').text(user.last_name);
}
),
$.ajax({
dataType: "json",
data:
'act=edit' +
'&userID=' + userID,
type: 'post',
});
})
PHP
class Application_Controllers_Test_Module extends Application_Controllers_ModelResource {
public function indexAction() {
// $data['test'] = $this->getConfig()->read($this->getDispatcher()->getServiceKey());
$testModel = Application_App::getModel("test");
$act = $this->getRequest()->getParam("act");
$userFirst = $this->getRequest()->getParam("firstName");
$userLast = $this->getRequest()->getParam("lastName");
$userID = $this->getRequest()->getParam("userID");
if($act == 'add') {
$result = $testModel->AddingTestUsers($userFirst, $userLast);
Application_App::redirect('test');
}
if($act == 'getUser') {
$data["users"] = $testModel->selectUserByID($userID);
echo json_encode($teacher);
}
if($act == 'edit') {
$result = $testModel->UpdateTestUsers($userFirst, $userLast, $userID);
}
if($act == 'delete') {
$result = $testModel->DeleteTestUsers($userID);
die(json_encode($result));
}
$data["users"] = $testModel->selectTestUsers();
return $data;
}
}
## MySQL ##
public function selectUserByID($userID) {
return $this->getAdapter()->fetch("SELECT * FROM test WHERE 'test_id'=?", array($userID));
}
我是Ajax的初级。我的公司使用他们自己的php框架,看起来像somhow laravel,他们使用twig模板。问题我应该通过ajax更新表单。我有一个带按钮编辑的表格。点击按钮后,信息应出现在表单输入中。只有一个小问题是我不知道如何将Ajax连接到我的php脚本
答案 0 :(得分:2)
要将AJAX连接到PHP脚本,您必须在AJAX调用中指定URL以及方法:
$.ajax({
method: 'post',
url: 'foo.php',
dataType: "json",
data:
'act=edit' +
'&userID=' + userID,
});