简体   繁体   English

在JavaScript中获取来自PHP的回复

[英]Getting response from Php in javascript

I have a php script which forms part of a login process, I pass the data from the login page to the process.php page via ajax, now this works, but what i am trying to do is get a response back in the form of a 1 or a 0 and then generate an action based on the result. 我有一个php脚本,它构成登录过程的一部分,我通过ajax将数据从登录页面传递到process.php页面,现在可以正常工作,但是我想做的就是以的形式返回响应1或0,然后根据结果生成操作。

here is my code. 这是我的代码。

 (document).ready(function(){
$('form.login').submit(function () {
var user = $(this).find("[name='user']").val();
var pass = $(this).find("[name='pass']").val();
var sublogin = $(this).find("[name='sublogin']").val();

// ...
    $.ajax({
type: "POST",
url: "http://www.server.co.uk/process.php",
data: {
    user : user,
    pass : pass,
    sublogin : sublogin,
},
 success: function(response){                       
        if(response == "1")
                    {
                        text = "Logged In";
                        /*
                            login successfull
                        */
                    }
                    // Login failed
                    else
                    {
                        text = "Error";
                    }


        //alert(response);
    }
});

And the process.php code 和process.php代码

  function procLogin(){
  global $session, $form;
  /* Login attempt */
  $retval = $session->login($_POST['user'], $_POST['pass'], isset($_POST['remember']));

  /* Login successful */
  if($retval){ 
  echo '1';

  }
  /* Login failed */
  else{
    echo '0';
  }

} }

I think my problem is the way I am trying to pass the information back from the php page to the login page. 我认为我的问题是我试图将信息从php页面传递回登录页面的方式。

UPDATE: I can get a response back, using a response container in the javascript, i can echo the correct result for example 1 if the system logs in or 0 if it fails. 更新:我可以使用javascript中的响应容器来获取响应,如果系统登录,我可以回显正确的结果,例如1或失败时返回0。 What i can't seem to work out is how to take the responding result 1 or 0 and have it activate the if else statement. 我似乎无法解决的是如何将响应结果1或0并使其激活if else语句。 Sorry probably wasn't clear enough earlier. 抱歉,可能之前还不清楚。

do one alert in the result of the ajax. 对ajax的结果发出警报。 And see if it's ok with your PHP. 并查看您的PHP是否可以。

But I guess that is not going is because when accessing the php script is inside a function[procLogin()] that is never called. 但是我想那是不可行的,因为访问php脚本时是一个从未调用过的function [procLogin()]内部。

remove the function and let loose code 删除函数并放开代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM