繁体   English   中英

PHP通过html表单传递参数

[英]PHP pass parameters from html form

我来自C#背景并且是php的新手,我有一个html表单将其值提交到以下php文件,由于某种原因未设置这些值,我在做什么错?

<?php
include('Mail.php');
$recipients = "myemail@hotmail.com";
$name = $_POST["txtName"] ;
$from = $_POST["txtEmail"] ;
$subject = $_POST["txtAppName"] ;
$comments = $_POST["txtComment"] ;

$headers = array (
    'From' => $from,
    'To' => $recipients,
    'Subject' => $subject,
);
$body = "Name: ".$name."\r\n"."Comments: "."\r\n".$comments;
$mail_object =& Mail::factory('smtp',
    array(
        'host' => 'prwebmail',
        'auth' => true,
        'username' => 'username',
        'password' => 'pass', # As set on your project's config page
        'debug' => true, # uncomment to enable debugging
    ));
$mail_object->send($recipients, $headers, $body);
?>

这是html形式:

<form action="sendfeedback.php" method="post">
            <div><input placeholder="Name" id="txtName" type="text" /></div>
            <div><input placeholder="Email (Optional)" id="txtEmail" type="text" /></div>
            <div><input placeholder="Application Name (Type in the application name you're using)" id="txtAppName" type="text" /></div>
            <div style="height:4px"></div>
            <div><textarea placeholder="Comments..." id="txtComment"></textarea></div>
            <div style="width:407px; text-align:right;"><input id="submitComment" type="submit" value="Send" /></div>
        </form>

您的表单输入应具有属性name ,如下所示:

<form action="sendfeedback.php" method="post">
    <div><input placeholder="Name" id="txtName" name='txtName' type="text" /></div>
    <div><input placeholder="Email (Optional)" id="txtEmail" name="txtEmail" type="text" /></div>
    <div><input placeholder="Application Name (Type in the application name you're using)" id="txtAppName" name="txtAppName" type="text" /></div>
    <div style="height:4px"></div>
    <div><textarea placeholder="Comments..." id="txtComment" name='txtComment'></textarea></div>
    <div style="width:407px; text-align:right;"><input id="submitComment" type="submit" value="Send" /></div>
</form>

您未为inputs设置属性name的原因,请更改为

<form action="sendfeedback.php" method="post">
            <div><input name = 'txtName' placeholder="Name" id="txtName" type="text" /></div>
            <div><input name = 'txtEmail'  placeholder="Email (Optional)" id="txtEmail" type="text" /></div>
            <div><input name ='txtAppName' placeholder="Application Name (Type in the application name you're using)" id="txtAppName" type="text" /></div>
            <div style="height:4px"></div>
            <div><textarea name ='txtComment' placeholder="Comments..." id="txtComment"></textarea></div>
            <div style="width:407px; text-align:right;"><input id="submitComment" type="submit" value="Send" /></div>
        </form>

您缺少输入的name属性:

    <form action="sendfeedback.php" method="post">
        <div><input placeholder="Name" name="txtName" id="txtName" type="text" /></div>
        <div><input placeholder="Email (Optional)" name="txtEmail" id="txtEmail" type="text" /></div>
        <div><input placeholder="Application Name (Type in the application name you're using)" name="txtAppName" id="txtAppName" type="text" /></div>
        <div style="height:4px"></div>
        <div><textarea placeholder="Comments..." name="txtComment"  id="txtComment"></textarea></div>
        <div style="width:407px; text-align:right;"><input id="submitComment" type="submit" value="Send" /></div>
    </form>

输入字段中没有名称属性,您已经使用了id属性,但是应该使用name属性来发布表单值。

<form action="sendfeedback.php" method="post">
        <div><input name="txtName" placeholder="Name" id="txtName" type="text" /></div>
                    ^^^^^^^^^^^^^^
        <div><input name="txtEmail"  placeholder="Email (Optional)" id="txtEmail" type="text" /></div>
        <div><input  name="txtAppName"  placeholder="Application Name (Type in the application name you're using)" id="txtAppName" type="text" /></div>
        <div style="height:4px"></div>
        <div><textarea name="txtComment" placeholder="Comments..." id="txtComment"></textarea></div>
        <div style="width:407px; text-align:right;"><input id="submitComment" type="submit" value="Send" /></div>
    </form>

如上面的答案所示,将name属性添加到每个输入标签中。

另外,如果您以这种方式使用表格,我将不胜感激

if(isset($_POST['txtName'] || isset($_POST['txtPass'])) {
    $txtName = $_POST['txtName'];
    $txtPass = $_POST['txtPass'];
    //Everything else here instead require('mail.php');
}

我希望它能解决您的问题,并帮助您调试和保护您的代码。

暂无
暂无

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

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