简单的 PHP 提交表单不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20861093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:37:00  来源:igfitidea点击:

Simple PHP submit form not working

phpformssubmitform-submitcontact-form

提问by mildrenben

Just making a simple submit form and can't seem to get it working.

只是制作一个简单的提交表单,似乎无法让它工作。

It won't even report errors which is odd.

它甚至不会报告奇怪的错误。

Checked the php.ini and that all seems fine too.

检查了 php.ini,一切似乎也很好。

HTML:

HTML:

<form id="submit-form" action="receiving.php" method="POST">
        <h3>Submit a Link</h3>
        <fieldset>
            <table>
                <tr>
                    <td><label for="name">You</label></td>
                    <td><input id="name" name="name" type="text" placeholder="Your Twitter or Blog ect." /></td>
                </tr>
                <tr>
                    <td><label for="submit-links">Link(s)</label></td>
                    <td><input id="sumbit-links" name="submit-links" type="text" placeholder="" required="" /></td>
                </tr>
                <tr>
                    <td><input name="submit" type="submit" value="SUBMIT" /></td>
                </tr>
            </table>
        </fieldset>
    </form>

receiving.php:

接收.php:

<?php 
error_reporting(-1);
$name = $_POST['name']; 
$submit-links = $_POST['submit-links']; 
if(isset($_POST['submit'])){
 $from_add = "[email protected]"; 
 $to_add = "[email protected]"; 
 $subject = "Your Subject Name";

 $message = "Name:$name \n Sites: $submit-links";

 $headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion()

 if(mail($to_add,$subject,$message,$headers)){
    $msg = "Mail sent";
 } 
}
print "<p>Thanks $name</p>";
?>

Any help would be much appreciated :)

任何帮助将非常感激 :)

采纳答案by Funk Forty Niner

There were a few things wrong with your form, which I testedbefore posting this answer.

您的表单存在一些问题,我在发布此答案之前对其进行了测试

As Jeremy Millerpointed out in his answer (+1Jeremy btw), using hyphens in a variable is invalid, use underscores instead.

正如Jeremy Miller他的回答中所指出的(+1顺便说一句,杰里米),在变量中使用连字符是无效的,请改用下划线。

You're also missing a closing semi-colon after 'X-Mailer: PHP/' . phpversion()which by the way, you shouldn't be using (for security purposes) but... if you absolutely want to use it, add it like this 'X-Mailer: PHP/' . phpversion();- Consult EDIT(suggestive usage) below.

您还缺少一个结束分号'X-Mailer: PHP/' . phpversion(),顺便说一下,您不应该使用(出于安全目的)但是...如果您绝对想使用它,'X-Mailer: PHP/' . phpversion();请像这样添加它- 咨询编辑(建议用法)以下。

This $msg = "Mail sent";won't print a message "Mail sent" after successful submit, since you're only assigning the variable to text; you need to echo itwhich I added below; it's not an error but why have it if you're not going to use it. (wink).

$msg = "Mail sent";不会Mail sent在成功提交后打印消息“ ”,因为您只是将变量分配给文本;你需要echo it我在下面添加;这不是错误,但如果您不打算使用它,为什么要使用它。(眨眼)。

HTML form

HTML 表单

<form id="submit-form" action="receiving.php" method="POST">
        <h3>Submit a Link</h3>
        <fieldset>
            <table>
                <tr>
                    <td><label for="name">You</label></td>
                    <td><input id="name" name="name" type="text" placeholder="Your Twitter or Blog ect." /></td>
                </tr>
                <tr>
                    <td><label for="submit_links">Link(s)</label></td>
                    <td><input id="sumbit_links" name="submit_links" type="text" placeholder="" required="" /></td>
                </tr>
                <tr>
                    <td><input name="submit" type="submit" value="SUBMIT" /></td>
                </tr>
            </table>
        </fieldset>
</form>

PHP

PHP

<?php 
error_reporting(-1);

$name = $_POST['name']; 
$submit_links = $_POST['submit_links']; 

if(isset($_POST['submit']))
{
$from_add = "[email protected]"; 
$to_add = "[email protected]"; 
$subject = "Your Subject Name";
$message = "Name:$name \n Sites: $submit_links";

$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

if(mail($to_add,$subject,$message,$headers)) 
{
    $msg = "Mail sent";

echo $msg;

} 
}

print "<p>Thanks $name</p>" ;

?>


EDIT (suggestive usage)

编辑(建议用法)

I suggest you use the following PHP, since your present conditional statements will throw the following errors, if the PHP file is accessed directly, which could happen.

我建议你使用下面的 PHP,因为你现在的条件语句会抛出以下错误,如果直接访问 PHP 文件,可能会发生这种情况。

Plus, using 'X-Mailer: PHP/' . phpversion()lets people know which PHP version you're using.

另外, using'X-Mailer: PHP/' . phpversion()可以让人们知道您使用的是哪个 PHP 版本。

I have it on good authority, that using this is a security hole. His name escapes me right now, but I will add it once I do remember.

我有很好的权威,使用它是一个安全漏洞。我现在忘记了他的名字,但一旦我记得,我会添加它。

Notice: Undefined index: name in... on line 4

Notice: Undefined index: submit_links in... on line 5

注意:未定义索引:第 4 行中的名称...

注意:未定义索引:submit_links in... 第 5 行

I've set your variables inside your if(isset($_POST['submit']))conditional statement.

我已经在你的if(isset($_POST['submit']))条件语句中设置了你的变量。

<?php 
error_reporting(-1);

if(isset($_POST['submit']))
{
$name = $_POST['name']; 
$submit_links = $_POST['submit_links']; 
$from_add = "[email protected]"; 
$to_add = "[email protected]"; 
$subject = "Your Subject Name";
$message = "Name:$name \n Sites: $submit_links";

$headers = 'From: [email protected]' . "\r\n" .

'Reply-To: [email protected]' . "\r\n";

if(mail($to_add,$subject,$message,$headers)) 
{
    $msg = "Mail sent";

 echo $msg;
} 

print "<p>Thanks $name</p>" ;
}

// else conditional statement for if(isset($_POST['submit']))
else {
echo "Sorry, you cannot do that from here. Please fill in the form first.";
}

?>

回答by Sylas Seabrook

$submit-linksis not a valid variable name. Switch all instances to $submit_links

$submit-links不是有效的变量名。将所有实例切换到$submit_links

回答by olga

The action may be wrong. For, example, if your htaccessredirects to https.wwwinstead of http, you will not see $_POSTin the following example :

操作可能是错误的。例如,如果您htaccess重定向到https.www而不是http,您将不会$_POST在以下示例中看到:

<?php  $formAction = 'http://somedomain.com/receiving.php'; ?>
<form  method="post"  action="<?php echo $formAction; ?>" >
//$_POST will be empty

but it will work if you use

但如果你使用它会起作用

<?php  $formAction = 'https://www.somedomain.com/receiving.php'; ?>
<form  method="post"  action="<?php echo $formAction; ?>" >
//$_POST will contain variables