PHP从html表单传递参数

时间:2014-02-24 19:09:43

标签: php html

我来自C#background和我是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>

5 个答案:

答案 0 :(得分:2)

您的表单输入应具有属性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>

答案 1 :(得分:1)

你没有为name设置属性inputs的原因,所以请将其改为

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

答案 2 :(得分:1)

您错过了输入的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>

答案 3 :(得分:1)

输入字段中没有名称属性,您使用了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>

答案 4 :(得分:1)

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

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

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

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