【问题标题】:Trying to redirect users based on what they select in dropdown menu, having troubles inserting option into database and it’s not redirecting尝试根据用户在下拉菜单中选择的内容重定向用户,在将选项插入数据库时​​遇到问题并且它没有重定向
【发布时间】:2018-08-11 19:37:51
【问题描述】:

过去一周我一直在尝试解决这个问题,但没有成功。我想做的是基于用户从下拉菜单中选择的角色,他们将被重定向到网站的不同视图。目前,该站点没有基于用户重定向,我认为 register_handler.php 上第 65-78 行的代码会处理好用户选择的国家/地区也不会存储在 phpmyadmin 中。我已经添加/删除或更改了我能想到的所有变量(包括 register_handler 上的第 156 行,已包含下图),但我无法弄清楚它是否可以工作。我知道我犯的错误很可能与变量有关,但不知道该怎么做。此外,我尝试添加的代码来自本指南(http://form.guide/php-form/php-form-select.html/comment-page-1#comments)。 首先十分感谢! 已包含两个相关页面的代码。
不好意思,第一次发帖。。。

Users table

Register screen

register_handler.php(添加:``,)用于名为 formCountry 的表中的新列。
Country selection form

我在 register.php 中添加的代码

<?php  
require 'config/config.php';
require 'includes/form_handlers/register_handler.php';
require 'includes/form_handlers/login_handler.php';
?>


<html>
<head>
    <title>Welcome to Countries!</title>
    <link rel="stylesheet" type="text/css" href="assets/css/register_style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="assets/js/register.js"></script>
</head>
<body>

    <?php  

    if(isset($_POST['register_button'])) {
        echo '
        <script>

        $(document).ready(function() {
            $("#first").hide();
            $("#second").show();
        });

        </script>

        ';
    }


    ?>

    <div class="wrapper">

        <div class="login_box">

            <div class="login_header">
                <h1>Countries!</h1>
                Login or sign up below!
            </div>
            <br>
            <div id="first">

                <form action="register.php" method="POST">
                    <input type="email" name="log_email" placeholder="Email Address" value="<?php 
                    if(isset($_SESSION['log_email'])) {
                        echo $_SESSION['log_email'];
                    } 
                    ?>" required>
                    <br>
                    <input type="password" name="log_password" placeholder="Password">
                    <br>
                    <?php if(in_array("Email or password was incorrect<br>", $error_array)) echo  "Email or password was incorrect<br>"; ?>
                    <input type="submit" name="login_button" value="Login">
                    <br>
                    <a href="#" id="signup" class="signup">Need an account? Register here!</a>
                </form>
            </div>
            <div id="second">
                <form action="register.php" method="POST">
                    <label for='formCountry'>Select your country of residence</label><br>
                    <select>
                        <option value="">Select a country</option>
                        <option value="US">United States</option>
                        <option value="UK">United Kingdom</option>
                        <option value="France">France</option>
                        <option value="Mexico">Mexico</option>
                        <option value="Russia">Russia</option>
                        <option value="Japan">Japan</option>
                    </select><br>
                    <input type="text" name="reg_fname" placeholder="First Name" value="<?php 
                    if(isset($_SESSION['reg_fname'])) {
                        echo $_SESSION['reg_fname'];
                    } 
                    ?>" required>
                    <br>
                    <?php if(in_array("Your first name must be between 2 and 25 characters<br>", $error_array)) echo "Your first name must be between 2 and 25 characters<br>"; ?>




                    <input type="text" name="reg_lname" placeholder="Last Name" value="<?php 
                    if(isset($_SESSION['reg_lname'])) {
                        echo $_SESSION['reg_lname'];
                    } 
                    ?>" required>
                    <br>
                    <?php if(in_array("Your last name must be between 2 and 25 characters<br>", $error_array)) echo "Your last name must be between 2 and 25 characters<br>"; ?>

                    <input type="email" name="reg_email" placeholder="Email" value="<?php 
                    if(isset($_SESSION['reg_email'])) {
                        echo $_SESSION['reg_email'];
                    } 
                    ?>" required>
                    <br>

                    <input type="email" name="reg_email2" placeholder="Confirm Email" value="<?php 
                    if(isset($_SESSION['reg_email2'])) {
                        echo $_SESSION['reg_email2'];
                    } 
                    ?>" required>
                    <br>
                    <?php if(in_array("Email already in use<br>", $error_array)) echo "Email already in use<br>"; 
                    else if(in_array("Invalid email format<br>", $error_array)) echo "Invalid email format<br>";
                    else if(in_array("Emails don't match<br>", $error_array)) echo "Emails don't match<br>"; ?>


                    <input type="password" name="reg_password" placeholder="Password" required>
                    <br>
                    <input type="password" name="reg_password2" placeholder="Confirm Password" required>
                    <br>
                    <?php if(in_array("Your passwords do not match<br>", $error_array)) echo "Your passwords do not match<br>"; 
                    else if(in_array("Your password can only contain english characters or numbers<br>", $error_array)) echo "Your password can only contain english characters or numbers<br>";
                    else if(in_array("Your password must be betwen 5 and 30 characters<br>", $error_array)) echo "Your password must be betwen 5 and 30 characters<br>"; ?>


                    <input type="submit" name="register_button" value="Register">
                    <br>

                    <?php if(in_array("<span style='color: #14C800;'>You're all set! Go ahead and login!</span><br>", $error_array)) echo "<span style='color: #14C800;'>You're all set! Go ahead and login!</span><br>"; ?>
                    <a href="#" id="signin" class="signin">Already have an account? Sign in here!</a>
                </form>
            </div>

        </div>

    </div>


</body>
</html>


我添加到 register_handler.php 中的代码

    <?php
//Declaring variables to prevent errors
$fname = ""; //First name
$lname = ""; //Last name
$em = ""; //email
$em2 = ""; //email 2
$password = ""; //password
$password2 = ""; //password 2
$date = ""; //Sign up date 
$error_array = array(); //Holds error messages

if(isset($_POST['register_button'])){

    //Registration form values

    //First name
    $fname = strip_tags($_POST['reg_fname']); //Remove html tags
    $fname = str_replace(' ', '', $fname); //remove spaces
    $fname = ucfirst(strtolower($fname)); //Uppercase first letter
    $_SESSION['reg_fname'] = $fname; //Stores first name into session variable

    //Last name
    $lname = strip_tags($_POST['reg_lname']); //Remove html tags
    $lname = str_replace(' ', '', $lname); //remove spaces
    $lname = ucfirst(strtolower($lname)); //Uppercase first letter
    $_SESSION['reg_lname'] = $lname; //Stores last name into session variable

    //email
    $em = strip_tags($_POST['reg_email']); //Remove html tags
    $em = str_replace(' ', '', $em); //remove spaces
    $em = ucfirst(strtolower($em)); //Uppercase first letter
    $_SESSION['reg_email'] = $em; //Stores email into session variable

    //email 2
    $em2 = strip_tags($_POST['reg_email2']); //Remove html tags
    $em2 = str_replace(' ', '', $em2); //remove spaces
    $em2 = ucfirst(strtolower($em2)); //Uppercase first letter
    $_SESSION['reg_email2'] = $em2; //Stores email2 into session variable

    //Password
    $password = strip_tags($_POST['reg_password']); //Remove html tags
    $password2 = strip_tags($_POST['reg_password2']); //Remove html tags

    $date = date("Y-m-d"); //Current date


if(isset($_POST['formCountry']))
{
    $varCountry = $_POST['formCountry'];
    $errorMessage = "";

    if(empty($varCountry))
    {
        $errorMessage = "<li>You forgot to select a country!</li>";
    }

    if($errorMessage != "") 
    {
        echo("<p>There was an error with your form:</p>\n");
        echo("<ul>" . $errorMessage . "</ul>\n");
    }
    else 
    {

        $redir = "us.php";
        switch($varCountry)
        {
            case "US": $redir = "us.php"; break;
            case "UK": $redir = "uk.php"; break;
            case "France": $redir = "france.php"; break;
            case "Mexico": $redir = "mexico.php"; break;
            case "Russia": $redir = "russia.php"; break;
            case "Japan": $redir = "japan.php"; break;
            default: echo("Error!"); exit(); break;
        }
        echo "<script>location.href='$redir'</script>"; 

        exit();
    }
}

    if($em == $em2) {
        //Check if email is in valid format 
        if(filter_var($em, FILTER_VALIDATE_EMAIL)) {

            $em = filter_var($em, FILTER_VALIDATE_EMAIL);

            //Check if email already exists 
            $e_check = mysqli_query($con, "SELECT email FROM Users WHERE email='$em'");

            //Count the number of rows returned
            $num_rows = mysqli_num_rows($e_check);

            if($num_rows > 0) {
                array_push($error_array, "Email already in use<br>");
            }

        }
        else {
            array_push($error_array, "Invalid email format<br>");
        }


    }
    else {
        array_push($error_array, "Emails don't match<br>");
    }


    if(strlen($fname) > 25 || strlen($fname) < 2) {
        array_push($error_array, "Your first name must be between 2 and 25 characters<br>");
    }

    if(strlen($lname) > 25 || strlen($lname) < 2) {
        array_push($error_array,  "Your last name must be between 2 and 25 characters<br>");
    }

    if($password != $password2) {
        array_push($error_array,  "Your passwords do not match<br>");
    }
    else {
        if(preg_match('/[^A-Za-z0-9]/', $password)) {
            array_push($error_array, "Your password can only contain english characters or numbers<br>");
        }
    }

    if(strlen($password > 30 || strlen($password) < 5)) {
        array_push($error_array, "Your password must be betwen 5 and 30 characters<br>");
    }


    if(empty($error_array)) {
        $password = md5($password); //Encrypt password before sending to database

        //Generate username by concatenating first name and last name
        $username = strtolower($fname . "_" . $lname);
        $check_username_query = mysqli_query($con, "SELECT username FROM Users WHERE username='$username'");


        $i = 0; 
        //if username exists add number to username
        while(mysqli_num_rows($check_username_query) != 0) {
            $i++; //Add 1 to i
            $username = $username . "_" . $i;
            $check_username_query = mysqli_query($con, "SELECT username FROM Users WHERE username='$username'");
        }

        //Profile picture assignment
        $rand = rand(1, 2); //Random number between 1 and 2

        if($rand == 1)
            $profile_pic = "assets/images/profile_pics/defaults/head_deep_blue.png";
        else if($rand == 2)
            $profile_pic = "assets/images/profile_pics/defaults/head_emerald.png";


        $query = mysqli_query($con, "INSERT INTO Users VALUES ('', '', '$fname', '$lname', '$username', '$em', '$password', '$date', '$profile_pic', '0', '0', 'no', ',')");

        array_push($error_array, "<span style='color: #14C800;'>You're all set! Go ahead and login!</span><br>");

        //Clear session variables 
        $_SESSION['reg_fname'] = "";
        $_SESSION['reg_lname'] = "";
        $_SESSION['reg_email'] = "";
        $_SESSION['reg_email2'] = "";
    }
}
?>

【问题讨论】:

    标签: php html mysqli


    【解决方案1】:

    在 register.php 的 &lt;select&gt; 标记中,您必须添加一个等于“formCountry”的属性名称。

    <select name="formCountry">
        <!-- Options -->
    </select>
    

    这是因为在您的服务器上您正在寻找名为“formCountry”($_POST['formCountry']) 的数据,因此您还需要在 HTML(客户端)中为该数据定义一个名称,因为如果您不这样做字段永远不会在服务器上传递,因为它没有名称。

    在此处阅读有关名称属性的更多信息:https://www.w3schools.com/tags/att_select_name.asp

    【讨论】:

      猜你喜欢
      • 2020-02-09
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 2012-12-02
      • 2016-12-22
      • 1970-01-01
      • 2022-12-15
      相关资源
      最近更新 更多