如何分离两个php表单提交功能

时间:2014-04-25 15:46:07

标签: php

我正在创建一个简单的用户更新页面,用户可以在其中更新密码和他们的电子邮件,但我想用两个单独的表单来做,因为如果我使用一个,并将其中一个字段发送为空,则会更新它在数据库中清空。 (我希望用户只能更新电子邮件或仅更新用户名)。

这是我的代码:

<html>
<body>

<?php

$con=mysqli_connect();
session_start();
if (!isset($_SESSION['ID'])){
    header('location:login.php');
}
//
?>

<?php

if(!isset($_POST['submit'])) {
    $con=mysqli_connect();
} else {
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
}

?>

<?php

$email = mysqli_real_escape_string($con,$_POST['Email']);
$password = mysqli_real_escape_string ($con,$_POST['Password']);
$ID = $_SESSION['ID'];
$emailErr =  $passwordErr="";

function test_input($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}

    if ($_SERVER["REQUEST_METHOD"] == "POST")

    {                           
        if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
        {
            $emailErr = "Please enter a valid Email Address";
        }

        else {

            $sql="UPDATE `customer`
            SET `Email`='$email'
            WHERE `ID`='$ID'";
            $result = mysqli_query($con,$sql);




echo "Update complete!";
//header("Location: userpage.html");



 if (!mysqli_query($con,$sql))
{
  die('Error: ' . mysqli_error($con));
}
}
}

?>

   <form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>  method="post"><br />

Email:<br /> <input type="text" name="Email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span><br /><br />
<input type="submit">
</form> 

<?php



if ($_SERVER["REQUEST_METHOD"] == "POST")

    {                   





                        if (!preg_match("/^[a-zA-Z0-9@_]*$/",$password))

                            {
                            $passwordErr = "Please enter a valid password"; 
                            }




    else {





$sql="UPDATE `customer`
SET `Password`='$password'
WHERE `ID`='$ID'";
$result = mysqli_query($con,$sql);

echo "Update complete!";
//header("Location: userpage.html");



 if (!mysqli_query($con,$sql))
{
  die('Error: ' . mysqli_error($con));
}
}
}

mysqli_close($con);



?>


<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>  method="post"><br />
Password:<br /> <input type="password" name="Password">
<span class="error">* <?php echo $passwordErr;?></span><br /><br />
<input type="submit">
</form> 




</body>
</html>

使用此代码,如果我更新第二个表单(这是密码),我将收到第一个错误,因为它在提交时执行。 我如何才能使它在同一页面上有两个表单,在单击提交按钮时更新表中的不同行,而不重定向到另一个页面? 谢谢

3 个答案:

答案 0 :(得分:1)

您可以为每个提交(或其他输入)指定一个唯一的名称,并检查它是否在POST之后设置

例如,为您的submit按钮命名:

<input type="submit" name="turtle">

在你的PHP中:

<?php
if(isset($_POST['turtle'])) {
    // Process the form associated with the "turtle" submit button.
} else {
    // Do the other form stuff.
}

?>

答案 1 :(得分:1)

为什么不组合成一个表格并检查用户是否输入了密码?

    <?php
    $con = mysqli_connect();
    session_start();
    if (!isset($_SESSION['ID'])){
        header('location:login.php');
    }

    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    function test_input($data) {
         $data = trim($data);
         $data = stripslashes($data);
         $data = htmlspecialchars($data);
         return $data;
    }

    if (isset($_POST['submit'])):

        $email = mysqli_real_escape_string($_POST['Email']);
        $password = mysqli_real_escape_string($_POST['Password']);
        $ID = $_SESSION['ID'];

        // Validate Email
        if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
            $errors[] = "Please enter a valid Email Address";
        }

        // Check if the password field has been filled in
        if($password) {
            // If it has then do your regex...
            if (!preg_match("/^[a-zA-Z0-9@_]*$/",$password)) {
                $errors[] = "Please enter a valid password"; 
                $update_password = false;
            } else {
                $update_password = true;
            }       
        }   

        if(count($errors) == 0) {

            // Update Email
            $sql="UPDATE `customer`
            SET `Email`='$email'
            WHERE `ID`='$ID'";
            $result = mysqli_query($con,$sql);      

            // If the test above passed then update the password
            if($update_password) {
                $sql="UPDATE `customer`
                SET `Password`='$password'
                WHERE `ID`='$ID'";
                $result = mysqli_query($con,$sql);  
            }    

            echo 'Update Complete';
            //header("Location: userpage.html");

        }

    ?>
    <?php else: ?>
    <form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>  method="post"><br />
        <?php if($errors): ?>
            <span class="error">* <?php echo explode(', ',$errors);?></span><br /><br />
        <?php endif; ?>
        Email:<br /> <input type="text" name="Email" value="<?php echo $email;?>">
        Password:<br /> <input type="password" name="Password">
        <input type="submit" name="submit">
</form>
    <?php endif; ?>

P.S我整理了你的代码,因为它给了我一个疝气。

答案 2 :(得分:-1)

使用隐藏的输入分隔请求

OR

将不同表单的单独文件用作目标...

OR

使用boneca n#39; s回答