AJAX表单数据不是从PHP包含的页面发送的

时间:2013-05-14 18:09:31

标签: php javascript html ajax forms

我正在构建一个注册表单,并希望将其包含在主页面上的div中。主页面还包括“登录”表单。

两个表单都包含在页面中,使用适当的div中的php include命令,即:

<?php include_once("login.php")?>    
<?php include_once("signup.php")?>

现在当我在第二个表单上使用ajax,即signup.php表单时,它会从第一个表单中的字段发回信息。电子邮件和密码字段都无济于事。

有谁知道如何隔离每个表单中的信息,以便我可以分别从每个表单发送数据?以下是表格和Javascript的所有相关代码。

以下是第一个表单(login.php)的代码:

  <form name="loginform" id="loginform" onsubmit="return false;">
    <input style="margin-bottom:5px;" id="email" type="text" class="searchbox" onblur="checkusername()" maxlength="35" value="Email">
    <span id="emailstatus"></span>
    <input id="password" class="searchbox" type="password" onfocus="emptyElement('status')"  maxlength="88" value="Password">
    <p><a href="#" id="loginbtn" onclick="login()">Log In / <a href="#" onclick="forgotpass()">Forgot Password</a></p>
    <span id="status"></span>
  </form>

和它的ajax:

function login(){
//isolate the variables from the form
var p = _("password").value;
var e = _("email").value;
var status = _("status");
if(e != "Email" && p != "Password"){
    _("loginbtn").style.display = "none";
    status.innerHTML = 'please wait ...';

//start the Ajax Request

    var ajax = ajaxObj("POST", "login.php");
    ajax.onreadystatechange = function() {
        if(ajaxReturn(ajax) == true) {
            if(ajax.responseText == "login_failed"){
                status.innerHTML = 'There was a problem. Please check your email and password and try again. ';
                _("loginbtn").style.display = "block";
            }  else {
                window.location = "user.php?id="+ajax.responseText; 

            }
        }
    }

//data being sent by the ajax call

    ajax.send("e="+e+"&p="+p);
} else {
    status.innerHTML = "You're missing something..";
}
}

function emptyElement(x){
//The _(x) function is a shortcut for getElementById
    _(x).innerHTML = "";
}

//This jQuery used to pre populate the email and password fields with "email" and "password"
$('input .seachbox').each(function(){
    $(this)
    .data('default', $(this).val())
    .focus(function(){
        if ($(this).val() == $(this).data('default')||''){
        $(this).val() = ''
        }
    })
    .blur(function(){
    var default_val = $(this).data('default');
    if($(this).val() == ''){
        $(this).val($(this).data('default'))
    }
    }); 
});

和第二种形式(signup.php):

     <form name="signupform" id="signupform" onsubmit="return false;">
      <div>Email Address:</div>
    <input id="emails" type="text" class="searchbox" onfocus="emptyElement('status')" onkeyup="restrict('email')" maxlength="88">
    <div>Create Password:</div>
    <input id="pass1" type="password" class="searchbox" onfocus="emptyElement('status')" maxlength="16">
    <div>Confirm Password:</div>
    <input id="pass2" type="password" class="searchbox" onfocus="emptyElement('status')" maxlength="16">
      </form>           
<br> <a href="#" alt="Signup" onclick="signup()">Create Account</a><p>
<div class="statuserror"><span id="status" >This is a permanent error</span></div>
    </div>      

和它的ajax:

function signup(){
var e = _("email").value;
var p1 = _("pass1").value;
var p2 = _("pass2").value;
var status = _("status");
if( e == "" || p1 == "" || p2 == ""){
    status.innerHTML = "Fill out all of the form data";
} else if(p1 != p2){
    status.innerHTML = "Your password fields do not match";
} else {
    status.innerHTML = 'please wait ...';
    var ajax = ajaxObj("POST", "signup.php");
    ajax.onreadystatechange = function() {
        if(ajaxReturn(ajax) == true) {
            if(ajax.responseText != "OK"){
                status.innerHTML = ajax.responseText;
                _("signupbtn").style.display = "block";
            } else {
                _("p1").innerHTML = "Please check your email inbox and junk mail box at <u>"+e+"</u> in a moment to complete the sign up process by activating your account. You will not be able to do anything on the site until you successfully activate your account.";
            }
        }
    }
    ajax.send("e="+e+"&p="+p1);
}

This is a screenshot with the Firebug output - the response from the POST should appear where the red text is on the page.

编辑 - 解决方案: - 有 - 在3天后发现解决方案如此愚蠢我必须分享它。基本上我将状态div的id从“status”更改为“status1”,发现现在一切正常。我认为这可能是因为在登录页面上我还有一个名为status的div,因此浏览器不确定将哪个div放入其中并且什么都不做。这也解释了为什么signup.php页面在浏览器中单独运行时有效,但是当login.php与主页中一起包含时不能。

故事的道德 - 确认你的ID是完全不同的! 感谢大家的帮助。

0 个答案:

没有答案