0

我有一个将信息发送到 mysql 数据库的表单。错误消息和确认显示在表单右侧的单独 div 中,因此表单操作需要将用户发送到同一页面。我正在回显用户输入的字段,以防他们需要根据出现的错误消息编辑某些内容。但是,如果表单提交正确,我怎样才能使 php 不在表单中回显?

<?php

$submit = filter_input(INPUT_POST, 'submit');
//form data
$fullname = filter_input(INPUT_POST, 'fullname');
$email = filter_input(INPUT_POST, 'email');
$business = filter_input(INPUT_POST, 'business');
$date = date("Y-m-d");

?> 

然后在其他html代码之后....

  <div class="wrap">
      <div id="reg">

  <form action='register.php' method='POST'>
<table>
    <tr>
        <td>
        Your full name:
        </td>
        <td>
        <input type='text' name='fullname' value='<?php echo $fullname;?>'>
        }
        </td>

    </tr>
    <tr>
        <td>
        Your email:
        </td>
        <td>
        <input type='text' name='email' value='<?php echo $email;?>'>
        </td>

    </tr>

    <tr>
        <td>
        Your business:
        </td>
        <td>
        <input type='text' name='business' value='<?php echo $business;?>'>
        </td>

    </tr>






 </table>
 </br>
 <input type='submit' name='submit' value='Register'>

</form>
 </br>       

    </div>
    <?php
 if ($submit)
  {
    //open database
  $connect=mysql_connect("localhost","root","Ryweb1994");
  mysql_select_db("phapsy");


  //check for existence
if($fullname&&$business&&$business)
{

   $queryreg = mysql_query("INSERT INTO users VALUES                           ('','$fullname','$email','$business','$date')");
    echo ("<div id='message'><p>You have been registered! We will send you an email            with login information. Thank you for your interest in Phapsy!<p></div>");

   }
else echo "Please fill in <b>all</b> fields!";

   }


  ?>
    </div>
  </div>
4

3 回答 3

1

$submit只需用变量包裹您的表单:

+ <?php if($submit) { ?>
 <div class="wrap">
  ... 
  <form action='register.php' method='POST'>
  ...
  </form>
</div>
+ <?php }; ?>

    <?php
 if ($submit)
  {
    //open database
  $connect=mysql_connect("localhost","root","Ryweb1994");
  mysql_select_db("phapsy");


  //check for existence
if($fullname&&$business&&$business)
{

   $queryreg = mysql_query("INSERT INTO users VALUES                           ('','$fullname','$email','$business','$date')");
    echo ("<div id='message'><p>You have been registered! We will send you an email            with login information. Thank you for your interest in Phapsy!<p></div>");

   }
else echo "Please fill in <b>all</b> fields!";

   }


  ?>
    </div>
  </div>
于 2012-05-09T21:33:09.610 回答
1

既然您说您正在回显确认,那么您显然是在以某种方式测试是否成功输入 - 但您没有显示该代码。

所以像<input type='text' name='fullname' value='<?php if(!$successful){echo $fullname;} ?>'>会做的事情

于 2012-05-09T21:37:07.557 回答
1

将处理逻辑放在显示逻辑之前。

设置一个反映数据库交互是否成功的变量。

在整个表单中更新您的 echo 语句,以便仅在交互失败(或验证失败)时打印一个值:

<?php $successfullyProcessed = false;

//processing logic
$successfullyProcessed = true;


<input type='text' name='fullname' value='<?php if (!successfullyProcessed) echo $fullname;?>'>
于 2012-05-09T21:43:54.597 回答