在AJAX请求成功完成时运行一个PHP函数


Run a PHP function on success of AJAX request completion

我知道在JS中,您可以使用AJAX函数的成功作为触发器来启动其他事件…

所以我刚刚使用AJAX加载了一个新页面。通过这个,我还传递了一些参数,这是我传递的:

xmlhttp.open("GET","register-form.php?one="+wordChoiceOne+"&two="+wordChoiceTwo+"&three="+wordChoiceThree,true);

现在PHP中有了变量$one, $two, $three

我想在输出到页面之前使用这些(连接,资本化,分配等)完成几个任务。然而,我不希望它被绑定到点击按钮或任何东西,我希望它被绑定到 AJAX加载的成功。

我现在如何在PHP中启动这些操作?

伪代码:

function setUsername() // RUN ON AJAX SUCCESS{
     // capitalise first letter of one two and three
     // concatenate together
     // Store in variable $username
}

ECHO USERNAME OUT

埃塔。我已经编写了AJAX请求,它工作得很好。加载完成后,我要执行这个PHP函数,将该函数的触发器绑定到AJAX加载成功:

<?php
$_GET['one'], $_GET['two'] and $_GET['three'];
// Capitalise first letter
$one = ucfirst($one); 
$two = ucfirst($two); 
$three = ucfirst($three); 
$username = $one . $two . $three;
?>

EDIT: OK,所以看起来我无法解释自己,或者我还不明白这个功能需要什么。但这是我需要用简单的英语发生的事情,带有链接。

我正在建立一个注册系统。而不是让人们输入自己的用户名,我有一个用户名生成器。你可以在这里看到:http://marmiteontoast.co.uk/fyp/login-register/register-username-builder.php

Javascript第一步

你把瓷砖拖到盒子里,点击出现的按钮。当你三个都有了,你就按下绿色的大按钮。到目前为止,我们都在使用jQuery,并且存储了以下三个变量:

wordChoiceOne -您选择的第一个单词wordChoiceTwo——你选择的第二个单词wordChoiceThree -您选择的第三个单词

这些被存储为JS变量。

第二步PHP时间

现在我有了这三个存储变量,我希望离开JS,开始使用PHP。我已经建立了一个成功的工作注册,但它使用username作为输入,而不是帮助你"构建"自己的。

我的研究导致了这样的理解:由于客户端到服务器端切换,我可以成功地将这些变量传递到PHP的唯一方法是通过AJAX请求。这对我来说似乎很方便,因为我希望我已经构建的注册表单被异步加载到页面中。因此,我询问了一些关于如何通过AJAX请求"共享"这些jQuery值的建议,并得到了以下帮助,通过URL传递它们:

xmlhttp.open("GET","register-form.php?one="+wordChoiceOne+"&two="+wordChoiceTwo+"&three="+wordChoiceThree,true);

我的理解(也许我错了?请让我知道并解释,如果我是…)是这分配已经存在的变量wordChoiceTwo等PHP变量$ 2。这样对吗?

现在我有了PHP变量$ 1 $ 2 $ 3…

运行PHP

也许这是因为我更习惯于与JS工作,但与JS你必须做一些(按钮点击等)能够制定一个功能。我知道一个这样的做某事是一个成功的AJAXC请求,但这不是PHP。我的问题是…这是我唯一的问题,通过AJAX加载后如何开始运行PHP函数?**我想在这个页面上运行一个名为"setUsername"的php函数。但我不希望用户必须按下一个按钮,使这个开始,我希望它是绑定到AJAX完成的成功或类似的东西,因为我理解这可能是JS只有。

这是我已经准备好的AJAX调用:

function saveUsername(wordChoiceOne, wordChoiceTwo, wordChoiceThree){
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("login-register-wrapper").innerHTML=xmlhttp.responseText;
    }
  }

xmlhttp.open("GET","register-form.php?one="+wordChoiceOne+"&two="+wordChoiceTwo+"&three="+wordChoiceThree,true);
xmlhttp.send();
}

添加json标题类型和echo

$_GET['one'], $_GET['two'] and $_GET['three'];
// Capitalise first letter
$one = ucfirst($one); 
$two = ucfirst($two); 
$three = ucfirst($three); 
$username = $one . $two . $three;
header('Content-type: application/json');
echo json_encode(array('username'=>$username)); // <---

,然后在你的js中,绑定一个函数到成功调用:

xmlhttp.onreadystatechange=function() {
   if (xmlhttp.readyState==4 && xmlhttp.status==200) {
     //xmlhttp.responseText;
   }
 }

编辑,因为似乎有一些混淆:

仅仅因为ajax调用到达php脚本并不意味着它被执行。请求自然会等待答复。这个回复可以包含数据,如果你的php回显它。

edit 2这样你就可以理解它的原理了,这里有一个我认为你想要的过度简化:

文件"ajax。":

<div id="name"></div>
<script>
    function myName(first, last) {
        var req = new XMLHttpRequest();
        req.open('GET', 'ajax.php?first='+first+'&last='+last);
        req.onreadystatechange = function() {
            if(req.readyState==4 && req.status==200) {
                data = JSON.parse(req.responseText);
                document.getElementById('name').innerHTML = data.username;
            }
        }
        req.send();
    }
    myName('john', 'doe')
</script>
文件"ajax.php":

$first = $_GET['first'];
$last = $_GET['last'];
header('Content-type: application/json');
echo json_encode(array('username'=>ucwords($first) . ' ' . ucwords($last)));

这就是AJAX的一般用法