首页 文章

提交后以表格回复

提问于
浏览
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>

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>
    
  • 1

    既然你说你正在回应确认,你显然是以某种方式测试成功的输入 - 但你没有显示该代码 .

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

  • 1

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

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

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

    <?php $successfullyProcessed = false;
    
    //processing logic
    $successfullyProcessed = true;
    
    
    <input type='text' name='fullname' value='<?php if (!successfullyProcessed) echo $fullname;?>'>
    

相关问题