2012-03-15 158 views
1

我正在开发一个网站/数据库解决方案,它使管理员可以选择登录到网页并对数据库执行操作。Ajax从数据库读取

当前他们可以添加/删除/运行预定义的查询,但是为了编辑表中的记录,我希望他们指定主键(ID),然后让ajax读取与该关联的值记录并允许他们改变。

这是什么最好的方法呢?

回答

3

显示它stratton建议你必须在前端使用jquery/html,在后端使用php/mysql,这样逻辑就可以处理你发送的数据,并检索将被发回给用户的结果。 ex。 有一个表格,将发送的ID搜索:

<form> 
<input type="text" id="data" name="data"/> 
<--send button--> 
</form> 

我建议再使用jquery通过AJAX发送的形式是这样的:

$(sendbutton).click(function(e){ 
e.preventDefault(); 
s.ajax({ 
url: 'urlofyouphppage', 
type: 'post', 
data : {data: $("#data").val(), action: 'search_thing'} 
success: function(data){ 
//the response from the php page to insert somewhere. 
//beware that if you use json you will have to decode via parseJSON. 
} 
}); 
}); 

在你的php端,你将不得不解析后,然后执行操作

<?php 
if($_POST['action'] == 'search_thing'){ 
$id = $_POST['data']; 
//query for searching 
//format the result data 
echo $result; //or json_encode($result); if you want to use json 
} 
?> 

这样你就可以通过每段时间设定不同的动作创建多个逻辑的步骤,和前面回答可以是包含全日的HTML代码e“下一步”执行。

1

使Ajax请求(使用jQuery)的PHP脚本,从数据库中读取并输出在你喜欢的任何格式,返回值(HTML,JSON),然后在前端使用JavaScript