简体   繁体   English

单击 PHP 中的按钮后如何重定向到 URL

[英]How do I redirect to a URL after clicking a button in PHP

This is my php code which I called registerprocess.php This code occurs after I click a submit button on another code called userregistration.php这是我的 php 代码,我称之为 registerprocess.php 在我单击另一个名为 userregistration.php 的代码上的提交按钮后,会出现此代码

I wanted registerprocess.php to redirect the page to the userregistration.php if the user registering is using an email that already exists in the database.如果用户注册使用的是数据库中已经存在的电子邮件,我希望 registerprocess.php 将页面重定向到 userregistration.php。

And if the user registers with an email that is not in the database, I would want it to redirect to mainpage.php如果用户使用不在数据库中的电子邮件注册,我希望它重定向到 mainpage.php

I tried using window.location.href to redirect my page but it did not work.我尝试使用 window.location.href 重定向我的页面,但没有用。 Is there another way I could redirect my page?还有另一种方法可以重定向我的页面吗?

<?php
    session_start();
    $dbhost = "localhost";
    $dbname = "registration";
    $dbuser = "root";
    $dbpass = "password";

    $conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

    if ($conn->connect_error) {
        die("Connect failed: " . $conn->connect_error);
    }
    $UserId = mysqli_real_escape_string($conn, $_POST['UserId']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $password = mysqli_real_escape_string($conn . $_POST['password_1']);
    $password = mysqli_real_escape_string($conn . $_POST['password_2']);
    $duplicate = mysqli_query($conn, "SELECT * FROM users WHERE email='$email' ");
    if (mysqli_num_rows($duplicate) > 0) {
        $url = $_SESSION['url'];
        echo("<script language='javascript'>
            window.alert('Email already been taken')
            window.location.href='https://easymoney.com/userregistration.php' ;
        </script>");
    } else {
        $sql = "INSERT INTO users (email, password, UserId) VALUES ('$email', '$password','$UserId' )";
        $result = mysqli_query($conn, $sql);

        if ($result) {
            $url = $_SESSION['url'];
            echo("<script language='javascript'>
                window.alert('successful')
                window.location.href='https://easymoney.com/mainpage.php';
            </script>");
        } else {
            $url = $_SESSION['url'];
            echo("<script language='javascript'>
                window.alert('error')
                window.location.href='$url';
            </script>");
        }
    }
?>

已经有预定义的函数可以在 php 中重定向。

header("Location: $url");

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

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