0

I am having the hardest time of my life for not understanding the basics of the POST REDIRECT GET pattern in forms that submit to themselves. The main problem is that when the user goes back or refreshes the page, I get duplicate entries in the database So basically I have a page that contains two forms, each one submits to itself. I have some code implemented regarding the PRG pattern but it doesn't seem to work. I'll post a brief example where I'll try to explain what I am doing.

    <?php
    function saveUser1($UserName_1)
    {
    include 'db_conn.php';
    //MySQL code etc...    
        if($result) return 1; //registro correcto
        else return -2; //error
        header('Location: samepage.php' , true, 303);
    exit();
    }

    function saveUser2($UserName_2)
    {
    include 'db_conn.php';
    //MySQL code etc...    
        if($result) return 1; //registro correcto
        else return -2; //error
        header('Location: samepage.php' , true, 303);
    exit();
    }

    $error1 = 0;
    $error2 = 0;

    if(isset($_POST['userForm1']))
    {
        $error1 = saveUser1(clean_form($_POST['txtUserName_1']);
    }
    if(isset($_POST['userForm2']))
    {
        $error2 = saveUser2(clean_form($_POST['txtUserName_2']);
    }
    ?>

Now the HTML

    <form action="" name="userForm1" method="POST">
    <label for="data">Some Data</label>
    <input type="text" value="some test data to post" name="txtUserName_1" id="txtUserName_1" /><br />
    <input type="submit" name="userForm1" id="userForm1"/>
    </form>

    <form action="" name="userForm2" method="POST">
    <label for="data">Some Data</label>
    <input type="text" value="some test data to post" name="txtUserName_2" id="txtUserName_2" /><br />
    <input type="submit" name="userForm2" id="userForm2"/>
    </form>

I just created this code in example of what I am trying to accomplish, but I haven't had any luck with the PGR pattern. Could you guys tell me where the error is? Or redirect me (no kidding) to some good tutorial regarding this subject? I have been looking to a lot of questions / answers, blogs but I can't find anything really solid (from my point of view). Thanks in advance.

4

3 回答 3

0

而不是检查表单名称本身,而是检查表单中的唯一字段。例如 If(isset($_POST[txtUserName_1'']))

表单名称本身不会存在于帖子中。

要查看发布的内容,请尝试:

print_r($_POST); 出口;

于 2013-01-22T19:37:16.367 回答
0

也许您必须将发布操作设置为同一页面。
并且您的表单不应与您的提交按钮具有相同的名称(不确定)。

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >    
<label for="data">Some Data</label>
<input type="text" value="some test data to post" name="data" id="data" /><br />
<input type="submit" name="submit1" id="userForm1"/>
</form>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="form2" method="POST">
<label for="data">Some Data</label>
<input type="text" value="some test data to post" name="data" id="data" /><br />
<input type="submit" name="submit2" id="userForm2"/>
</form>

对于 php:

if(isset($_POST['submit1']))
{
    $error1 = saveUser1(clean_form($_POST['txtUserName_1']);
}
if(isset($_POST['submit2']))
{
    $error1 = saveUser1(clean_form($_POST['txtUserName_2']);
}

您可以添加一个隐藏字段以检查其是否执行:

<input type="hidden" name="executed" value="0"/>

然后你可以在执行 mysql 查询时将其设置为 0

function saveUser1($UserName_1)
{
if($_POST['executed'] == 0)
{
include 'db_conn.php';
//MySQL code etc...    
    if($result) $_POST['executed'] = 1; //registro correcto
    header('Location: samepage.php' , true, 303);
exit();
}
}
于 2013-01-22T19:47:12.687 回答
0

如果您想尝试,下面是示例代码。

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    Name: <input type="text" name="name">
    Email: <input type="text" name="email">
    Password: <input type="password" name="password">
    <input type="submit" value="submit" name="send">
</form>

PHP代码和common.php是数据库连接文件

<?php
   require_once "common.php";

   if(isset($_REQUEST['send']))
   {
      $name = $_POST['name'];
      $email = $_POST['email'];
      $password = $_POST['password'];

      $check = "SELECT * FROM user WHERE name = '".$name."' AND email = '".$email."' AND password = '".$password."'";
      $check_result = mysql_query($check) or die(mysql_error());

      if(mysql_num_rows($check_result) > 0)
      {
        header('Location : post.php');
      }
      else
      { 
        $sql = "INSERT INTO user (name,email,password) VALUES ('$name','$email','$password')";
        $result = mysql_query($sql) or die(mysql_error());
      } 
   }    
?>
于 2013-01-23T10:57:29.370 回答