如何从PHP函数运行JavaScript AJAX请求?

时间:2014-03-22 19:19:37

标签: javascript php html ajax

目前我的PHP if statement的成功只是重新加载页面:

header('Location: index.php');

已添加:此处链接到此网站http://marmiteontoast.co.uk/fyp/login-register/index.php

但是我想运行JavaScript基本AJAX请求而不是将页面register-success.php加载到div中:

function loadSuccess()
{var xmlhttp;
if (window.XMLHttpRequest)
  {xmlhttp=new XMLHttpRequest();}
else
  {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-success.php",true);
xmlhttp.send();}

但是我不确定JavaScript和PHP的这种组合以及如何实现它

1)是否有某种PHP函数可以包装一段JS以使其实现?

2)有没有办法在PHP中进行这样的简单AJAX调用?还是模仿它的东西?

这是验证我的表单的函数。如果表单有效,我需要使用AJAX重新加载并调用register-success文件

<?php
    session_start();
    require 'functions.php';

    if(isset($_POST['sign-up'])){
        // username
        if (isset($_POST['username'])){
            $username = mysql_real_escape_string(trim($_POST['username']));
            $_SESSION['status']['register']['username'] = $username;

            if(strlen($username) > 3){
                    if(strlen($username) < 31){
                        if(user_exists($username) === true){
                            $_SESSION['status']['register']['error'][] = 'That username is already taken. Sorry, please try again with a different username.';
                        } else{
                            // passed
                            // continue
                        }
                    } else {
                        $_SESSION['status']['register']['error'][] = 'The username is greater than 30 characters.';
                    }
                } else {
                    $_SESSION['status']['register']['error'][] = 'The username is less than 3 characters.';
                }
        } else {
            $_SESSION['status']['register']['error'][] = 'The username is not entered.';
        }

        if (isset($_POST['password'])){
            $password = mysql_real_escape_string(trim($_POST['password']));

            if(strlen($password) >= 8){
                    $password = hash_function($password);
                } else {
                    $_SESSION['status']['register']['error'][] = "Your secret password is too short. You should make a password with at least 8 letters.";
                }

        } else {
            $_SESSION['status']['register']['error'][] = "You haven't put in a password.";
        }

        // Email address
        if (!empty($_POST['email_address'])){
            $email_address = mysql_real_escape_string(trim($_POST['email_address']));
            $_SESSION['status']['register']['email_address'] = $email_address;
            if(strlen($email_address) > 10){ // email address less than 10
                    if(strlen($email_address) < 161){ // if longer than 160

                        if(email_valid($email_address) == false){ // email address invalid format
                                $_SESSION['status']['register']['error'][] = "The email address has been put in wrong. Please check and try again.";
                            }
                            else{
                                // passed min length, passed max length, passed validation
                                // Continue
                            }
                    }
                    else 
                    {
                        $_SESSION['status']['register']['error'][] = 'The email address is too long.';
                    }
                } 
                else
                {
                    $_SESSION['status']['register']['error'][] = "The email address is too short. It can't be shorter than 10 letters.";
                }
        }
        else{// passed (no email input)
        }

        if (isset($_POST['tos'])){
            $_SESSION['status']['register']['tos'] = $_POST['tos'];
            if(empty($_SESSION['status']['register']['error'])){
                if(register($email_address, $username, $password) === true){

                    // Success!!
                    $_SESSION['status']['register']['success'] = true;

                    // Sends an email

                    send_email($email_address);

                } else {
                    echo mysql_error();
                    die();
                    $_SESSION['status']['register']['error'][] = "Something went wrong. We're sorry. Please try again.";
                }
            } else {}
        } else {
            $_SESSION['status']['register']['error'][] = "You have to agree to the House Rules to be able to sign up.";
        }

        header('Location: index.php');
    } else {
        // success script with AJAX goes here
    }

    ?>

3 个答案:

答案 0 :(得分:1)

你需要在PHP Block之外编写javascript代码,所以在?&gt;之后。 Ajax调用是从客户端进行的,所以只能使用javascript。

为了简化代码,你可以使用javascript lib,比如jQuery,所以代码更简单:

$('#login-register-wrapper').load('register-success.php');

你可以使用像Kamehameha Answer这样的PHP,使用ob_get_contents()。

在这种情况下,没有来自html页面的参数,结果是相同的,来自已处理的内容。

修改

根据评论,您需要使用某些参数重定向到'index.php',例如:

header('Location: index.php?success=1');

所以,在index.php中,你可以这样写:

if (isset($_GET['success']) && $_GET['success'] == 1) {
    echo '<script>function loadSuccess()
{var xmlhttp;
if (window.XMLHttpRequest)
  {xmlhttp=new XMLHttpRequest();}
else
  {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-success.php",true);
xmlhttp.send();}</script>';
}

修改

在标签正文中,您需要调用javascript函数loadSuccess()。像这样:

<body onload="loadSuccess()">

答案 1 :(得分:0)

您只需要创建一个仅包含寄存器逻辑的额外.php文件。假设您有一个包含所有数据的表单,那么您将使用javascript创建一个提交按钮,并使用ajax请求将所有数据(例如通过get或post)发送到服务器上的.php文件。答案只是你想要包含在你的包装器中的html。然后,只需修改当前站点的dom即可显示服务器的响应

答案 2 :(得分:0)

这很简单! 执行以下操作:

  • 将此行添加到您的PHP代码中,以便它可以在PHP函数成功运行时运行:
  回声&#34;       的document.getElementById(&#39;成功&#39;)。点击();       &#34 ;;

  • 现在将此HTML行添加到您的页面中,以便加载
<a id="success" style="visibility:hidden;">Success</a>
<script>
var el = document.getElementById('success');
el.onclick = loadSuccess();
</script>