PHP从html表单传递参数

PHP从html表单传递参数,php,html,Php,Html,我来自C#背景,我是php新手,我有一个html表单将它的值提交到下面的php文件,由于某些原因,这些值没有设置,我做错了什么 <?php include('Mail.php'); $recipients = "myemail@hotmail.com"; $name = $_POST["txtName"] ; $from = $_POST["txtEmail"] ; $subject = $_POST["txtAppName"] ; $comments = $_POST["txtComme

我来自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);
?>

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

原因是您没有为您的
输入设置属性
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>

您缺少输入的
名称
属性:

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

您的输入字段中没有name属性,您使用了
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');
}

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

确保您添加的表单方法是Postal,并确保表单字段有名称,特别是您在PHP脚本中使用的名称。@putvande有它。当然,很多人都试图通过四次发布相同的内容来提高声誉。顺便说一句,我见过通过$\u REQUEST或$\u GET实现的,如果你不介意我问的话,有什么区别?
$\u GET
指的是URL中的查询字符串。因此,如果您的表单使用
GET
而不是
POST
,则url将类似于
sendfeed.php?txtName=ABC&txtmail=XYZ…
,并且
$\u GET
变量将包含这些参数<默认情况下,code>$\u请求
组合了
$\u POST
$\u GET
$\u COOKIE
。详情如下:
if(isset($_POST['txtName'] || isset($_POST['txtPass'])) {
    $txtName = $_POST['txtName'];
    $txtPass = $_POST['txtPass'];
    //Everything else here instead require('mail.php');
}