Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP表单-提交时保持在同一页面上_Php_Html_Forms_Submit - Fatal编程技术网

PHP表单-提交时保持在同一页面上

PHP表单-提交时保持在同一页面上,php,html,forms,submit,Php,Html,Forms,Submit,我有一个PHP表单,它位于文件contact.html中 表单是从文件processForm.php处理的 当用户填写表单并单击提交时, processForm.php发送电子邮件并将用户引导至-processForm.php 页面上有一条消息“成功!您的消息已发送。” 我对PHP了解不多,但我知道需要采取的行动是: // Die with a success message die("<span class='success'>Success! Your message has b

我有一个PHP表单,它位于文件
contact.html

表单是从文件
processForm.php
处理的

当用户填写表单并单击提交时,
processForm.php
发送电子邮件并将用户引导至-
processForm.php
页面上有一条消息“成功!您的消息已发送。”

我对PHP了解不多,但我知道需要采取的行动是:

// Die with a success message
die("<span class='success'>Success! Your message has been sent.</span>");
//带着一条成功消息死去
死亡(“成功!您的信息已发送。”);
如何将消息保存在表单div中而不重定向到
processForm.php
page


如果需要,我可以发布整个
processForm.php
,但它很长。

有两种方法:

  • 将表单提交到同一页面:使用PHP脚本处理提交的表单。(这可以通过将表单
    操作
    设置为当前页面URL来实现。)

  • 使用AJAX表单提交对于初学者来说,这比方法1稍微困难一些


  • 您必须使用与此类似的代码:

    echo "<div id='divwithform'>";
    
    if(isset($_POST['submit']))  // if form was submitted (if you came here with form data)
    {
        echo "Success";
    }
    else                // if form was not submitted (if you came here without form data)
    {
        echo "<form> ... </form>";
    } 
    
    echo "</div>";
    
    echo”“;
    if(isset($\u POST['submit'])//如果表单已提交(如果您带着表单数据来到这里)
    {
    呼应“成功”;
    }
    else//如果表单未提交(如果您来这里时没有表单数据)
    {
    回声“…”;
    } 
    回声“;
    
    使用if这样的代码对于许多页面来说是典型的,但是这非常简单

    通常,您必须验证第一个“if”(检查表单字段是否为空等)中的一些数据


    请访问或访问。有非常好的PHP视频教程,包括表单。

    保持在同一页面的最佳方式是发布到同一页面:

    <form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
    

    为了在提交时保持在同一页面上,您可以将action空(
    action=”“
    )保留在表单标记中,或者将其全部保留

    对于消息,创建一个变量(
    $message=“Success!您输入了:“.$input;”“
    ),然后在页面中希望消息显示的位置回显该变量

    像这样:

    <?php
    $message = "";
    if(isset($_POST['SubmitButton'])){ //check if form was submitted
      $input = $_POST['inputText']; //get input text
      $message = "Success! You entered: ".$input;
    }    
    ?>
    
    <html>
    <body>    
    <form action="" method="post">
    <?php echo $message; ?>
      <input type="text" name="inputText"/>
      <input type="submit" name="SubmitButton"/>
    </form>    
    </body>
    </html>
    
    
    
    您可以在表单操作中使用
    #
    操作:

    <?php
        if(isset($_POST['SubmitButton'])){ // Check if form was submitted
    
            $input = $_POST['inputText']; // Get input text
            $message = "Success! You entered: " . $input;
        }
    ?>
    
    <html>
        <body>
            <form action="#" method="post">
                <?php echo $message; ?>
                <input type="text" name="inputText"/>
                <input type="submit" name="SubmitButton"/>
            </form>
        </body>
    </html>
    

    试试这个……对我有用

    <form action="submit.php" method="post">
    <input type="text" name="input">
    <input type="submit">
    </form>
    
    <form action="" method="post">
    <table border="1px">
        <tr><td>Name: <input type="text" name="user_name" ></td></tr>
        <tr><td align="right"> <input type="submit" value="submit" name="btn"> 
    </td></tr>
    </table>
    </form>
    
    <?php
      if(isset($_POST['btn'])){
         $name=$_POST['user_name'];
         echo 'Welcome '. $name; 
       }
     ?>
    
    
    
    ------submit.php------


    朋友。使用这种方式,将不会出现“未定义的变量消息”,并且可以正常工作

    <?php
        if(isset($_POST['SubmitButton'])){
            $price = $_POST["price"];
            $qty = $_POST["qty"];
            $message = $price*$qty;
        }
    
            ?>
    
        <!DOCTYPE html>
        <html>
        <head>
            <title></title>
        </head>
        <body>
            <form action="#" method="post">
                <input type="number" name="price"> <br>
                <input type="number" name="qty"><br>
                <input type="submit" name="SubmitButton">
            </form>
            <?php echo "The Answer is" .$message; ?>
    
        </body>
        </html>
    
    
    


    我要做的是,当出现错误时,我希望页面在提交后保持不变……因此我希望重新加载页面:

    ($_SERVER["PHP_SELF"])
    
    而我将sript包含在一个单独的文件中,例如

    include_once "test.php";
    
    我也在某个地方读到

    if(isset($_POST['submit']))
    
    是一种新老的发布表单的方式,以及

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

    应该使用(不是我的话,请在某处阅读)

    您可以在同一页上看到表单操作的以下示例

    <form action="submit.php" method="post">
    <input type="text" name="input">
    <input type="submit">
    </form>
    
    <form action="" method="post">
    <table border="1px">
        <tr><td>Name: <input type="text" name="user_name" ></td></tr>
        <tr><td align="right"> <input type="submit" value="submit" name="btn"> 
    </td></tr>
    </table>
    </form>
    
    <?php
      if(isset($_POST['btn'])){
         $name=$_POST['user_name'];
         echo 'Welcome '. $name; 
       }
     ?>
    
    
    姓名:
    
    ajax或发布到同一页面。我更喜欢发布到同一页面。
    header('contact.html?result=success');
    类似的问题(有一些调整)如下:[php,提交后使用同一页面形成][1][1]:在contact.html或processForm.php中,我应该将标题放在哪里?应该注意的是,此方法在上没有通过html验证检查,因为它说“元素表单上的属性操作的错误值:必须为非空”。“虽然它确实有效,但HTML打印方式是这样的,这会导致错误:
    这里有一个这样的代码示例,这当然是可以理解的,而且它不会改变任何东西,因为上面已经提到了action=”“,它与action=“”相同。注意,您应该使用htmlspecialchars($\u SERVER['PHP\u SELF'))。否则,如果有人将JS放入url,它将受到xss攻击。这与将其保留为空完全相同,但增加了xss安全风险。坏主意。你检查代码了吗?1) 在第一次运行时,在提交之前,您会收到“Undefined variable message”(未定义变量消息)(当然!)2),然后在每次提交时,都会加载一个新表单来显示消息,并且它们都会建立在浏览器的历史记录中!为什么你们在发布代码之前不检查代码??@Apostolos那么我该如何正确使用这个示例呢?有什么需要改变的?我不记得了。大约一年前我就不再使用PHP了,而且目前我还没有可用的Apachi服务器进行测试。很抱歉但据我记忆所及,我设法使用“include”…Add
    if(isset($message)){…}
    around
    echo$message保持在同一页面(即PHP文件)以确保“注意:未定义的变量消息”不再出现。如何?利与弊?是的,这至少部分有效。您不会停留在同一个oage上——因为结果会显示在新页面上——但这要好得多,因为您必须返回到第一个页面,从而避免在浏览器历史记录中建立相同PHP的副本。@Apostolos-如果
    操作
    (在
    标记中)设置为当前页面的URL(或者简单地说
    )然后重新加载同一页面。(不是新窗口。)这将刷新页面。