【问题标题】:PHP pass parameters from html formPHP从html表单传递参数
【发布时间】:2014-02-24 19:09:42
【问题描述】:

我来自 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>

【问题讨论】:

  • 确保你添加表单的方法是post
  • 并确保表单字段有名称,特别是您在 PHP 脚本中使用的名称。
  • @putvande 有。当然,很多人随后通过四次发布相同的东西来尝试声誉耕作。
  • 顺便说一句,我已经看到它是通过 $_REQUEST 或 $_GET 完成的,如果您不介意我问有什么区别?
  • $_GET 指的是 URL 中的查询字符串。因此,如果您的表单使用GET 而不是POST,则url 看起来像sendfeedback.php?txtName=ABC&amp;txtEmail=XYZ...,而$_GET 变量将包含这些参数。 $_REQUEST 默认组合了 $_POST$_GET$_COOKIE。更多信息:stackoverflow.com/a/1924958/2136840

标签: php html


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    你没有为你的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>
    

    【讨论】:

      【解决方案3】:

      您缺少输入的 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>
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          如上述答案所示,为每个输入标签添加名称属性。

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

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

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

          【讨论】:

            猜你喜欢
            • 2018-05-23
            • 2019-10-28
            • 1970-01-01
            • 1970-01-01
            • 2017-08-19
            • 1970-01-01
            • 1970-01-01
            • 2017-05-05
            • 1970-01-01
            相关资源
            最近更新 更多