简体   繁体   English

PHP无法与我们联系表格

[英]PHP not working in contact us form

I am just a beginner with PHP & Html. 我只是PHP和HTML的初学者。 I have seen many questions on SO related to this, but somehow cannot fix a very simple looking problem at my end. 我已经看到了很多与此相关的问题,但是最终我还是无法解决一个非常简单的问题。 So, kindly help. 因此,请提供帮助。

The following code does not seem to be working. 以下代码似乎无效。 There is always a message "There seems to be a problem right now. Please try again after sometime" before the form when I run this code. 运行此代码之前,在表单之前总是出现一条消息“当前似乎有问题。请稍后再试”。 Whether I press the submit button or not, it does not make any difference. 无论我是否按下提交按钮,都没有任何区别。

<h2 >Inquiry form</h2>

<?php
    if (isset($_POST['submit'])){

        echo "Thank You!";
    }
    else {
        echo "There seems to be a problem right now. Please try again after sometime";
    }                                   

?> 

<form name="input" method="POST" action="contact.php">
    <label for="Name">Name (required):</label>
    <br />
    <input type="text" name="Name" />
    <br />
    <div class="clear"></div>
    <label for="inputmail">Email(required):</label>
    <br />
    <input type="text" name="email" />
    <br />
    <div class="clear"></div>
    <label for="inputtelefon">Phone:</label>
    <br />
    <input type="text" name="phone" />
    <br />
    <div class="clear"></div>
    <label for="inputmessage">Message:</label>
    <br/>
    <textarea name="message" cols="28" rows="3" ></textarea>
    <div class="clear"></div>
    <div id="send">
        <input type="submit" value=" Submit " />
        <input type="reset" value="  Clear  "  />
    </div>
</form>

Changed code: 更改的代码:

<h2 >Inquiry form</h2>

                                  <?php
                                  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                            //if ($_SERVER['REQUEST_METHOD'] == 'POST') {  This is not working as well
                                        echo "Thank You!";
                                    }
                                    else {
                                        echo "There seem to be a problem right now. Please try again after sometime";
                                    }
                             ?> 

                       <form name="input" method="POST" action="contact.php">
                            <label for="Name">Name (required):</label>
                            <br />
                            <input type="text" name="Name" />
                            <br />
                             <div class="clear"></div>
                             <label for="inputmail">Email(required):</label>
                            <br />
                            <input type="text" name="email" />
                            <br />
                            <div class="clear"></div>
                            <label for="inputtelefon">Phone:</label>
                            <br />
                            <input type="text" name="phone" />
                            <br />
                            <div class="clear"></div>
                            <label for="inputmessage">Message:</label>
                            <br/>
                            <textarea name="message" cols="28" rows="3" ></textarea>
                            <div class="clear"></div>
                  </div>
                  <div id="send">
                <input type="submit" value=" Submit " name="submit"/>
                </div>
            </form>

That's because your submit button does not have a name: 这是因为您的“提交”按钮没有名称:

<input type="submit" value=" Submit " name="submit"/>

Edit 编辑

Based on the comment of RandomCoder (in Marc B's answer). 基于RandomCoder的评论(Marc B的回答)。

Found this similar question: isset($_POST['submit']) vs $_SERVER['REQUEST_METHOD']=='POST' 找到了类似的问题: isset($ _ POST ['submit'])vs $ _SERVER ['REQUEST_METHOD'] =='POST'

Invalid method of checking for a submission. 无效的提交检查方法。 Never check for the presence/absence of a form field. 切勿检查表单字段的存在/不存在。 It's unreliable. 这是不可靠的。 You might change the field name, but forget to update the PHP. 您可能会更改字段名称,但忘记更新PHP。 Use this instead: 使用此代替:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   ...
}

it's 100% reliable and will always be "true" if a POST was performed. 它是100%可靠的,如果执行了POST,则始终为“ true”。

Take a look at your updated if statement. 查看更新后的if语句。 It is saying: if the form has been submitted then show "thank you", if the form hasn't been submitted then show "there seems to be a problem". 就是说:如果表单已提交,则显示“谢谢”,如果表单尚未提交,则显示“似乎有问题”。

That's the reason you're always seeing "there seems to be a problem", because when you view the form without submitting it, the else block of your if statement fires. 这就是为什么您总是看到“似乎有问题”的原因,因为当您查看表单而不提交表单时,if语句的else块就会触发。

You should remove the else block because it doesn't make any sense, so it becomes: 您应该删除else块,因为它没有任何意义,因此变为:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // form has been submitted, handle the data and say thanks...
    echo "Thank You!";
}

Using this method instead of checking for the submit button is better because Internet Explorer will not send the submit button as a post variable if the user pressed the enter key to submit the form. 使用此方法而不是检查“提交”按钮会更好,因为如果用户按下Enter键提交表单,Internet Explorer不会将提交按钮作为帖子变量发送。

More info - Why isset($_POST['submit']) is bad . 更多信息- Why isset($_POST['submit'])不好

There is Only one reasons which I see ie you have not defined the name submit to any variable. 我看到的原因只有一个,即您尚未定义将名称提交给任何变量。 There are two ways to find the submitting form as: 查找提交表单的方式有两种:

Either change: 可以更改:

 <input type="submit" value=" Submit " />

 to

 <input type="submit" value=" Submit " name="submit"/>

or add the extra line to do things something like this 或添加多余的行来执行类似这样的操作

 <input type = "hidden" name="passed" />

and to check 并检查

 <?php
      if (isset($_POST['passed'])){
          echo "Thank You!";
      }else {
           echo "There seems to be a problem right now. Please try again after sometime";
      }                                   

 ?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM