我很熟悉如何让ajax去PHP页面执行一系列事情然后返回json数据.但是,是否可以调用驻留在给定页面中的特定函数?
@H_502_1@基本上我想要的是减少项目中的文件数量.所以我可以在一个页面中放置很多常用函数,然后只调用我想要的函数.
对于ajax请求
@H_502_1@1.在您的网页中包含Jquery库.
例如:
例如:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>@H_502_1@2.单击按钮调用功能
<button type="button" onclick="create()">Click Me</button>@H_502_1@3.点击按钮,在javascript中调用create function.
<script> function create () { $.ajax({ url:"test.PHP",//the page containing PHP script type: "post",//request type,dataType: 'json',data: {registration: "success",name: "xyz",email: "abc@gmail.com"} success:function(result){ console.log(result.abc); } }); } <script>@H_502_1@在服务器端test.PHP文件中,应该读取动作POST参数和相应的值并在PHP中执行操作并以json格式返回,例如:
$regstration = $_POST['registration']; $name= $_POST['name']; $email= $_POST['email']; if ($registration == "success"){ // some action goes here under PHP echo json_encode(array("abc"=>'successfuly registered')); }