【问题标题】:PHP processing form returns NULL for checkbox and radio buttons and does not display text area fieldPHP处理表单为复选框和单选按钮返回NULL并且不显示文本区域字段
【发布时间】:2014-09-16 23:38:05
【问题描述】:

我是 php 新手,非常感谢任何人对此提供的帮助!这是我的复选框代码的示例:

     <legend>Fiction of Interest</legend>
    <p class="group">Please check the literary genres that are of interest to you:</p>
    <ul class="formList">
    <li>
    <input type="checkbox" name="fictioninterest[]" value="literaryfiction"  id="lfitem" 
    <?php if ((isset($fictioninterest)) && (in_array("literaryfiction", $fictioninterest))) { echo "checked"; } ?> />
     <label for="in">Literary Fiction</label>

这是我的单选按钮代码示例:

    <legend>Connecting with the Author</legend>
    <p class="group">Would you like to coincide your Book Club event with the author's BlogTalk radio interview?</p>
    <p class="group">
    <label>
    <input type="radio" name="blogtalk" value="yes" id="yesitem" <?php if ((isset($blogtalk)) && ($blogtalk==='yes')) { echo "checked"; } ?> />
    <label for="yesitem">Yes</label>
      <br>
      <label>
        <input type="radio" name="blogtalk" value="no" id="noitem" <?php if ((isset($blogtalk)) && ($blogtalk==='no')) { echo "checked"; } ?> />
    <label for="noitem">Not at this time</label>
    </p>

这是我的文本区号:

    <p class="group">
      <label for="comments" class="text">Additional comments</label>
      <textarea name="comments" id="comments">
      <?php if (isset($comments)) { echo $comments; } ?></textarea>
    </p>

我将非常感谢任何帮助!非常感谢!

我很抱歉 - 正如我所说的,我正在尝试自学 php,但即使是基础知识,我仍然在摸索。当我的表单被处理并将结果发送到电子邮件时,即使我选择了几个,复选框也会返回注册“null”。单选按钮也返回“null”。并且没有内容从我的文本区域部分传输到电子邮件。很抱歉没有正确解释,再次感谢您的帮助!

<?php 
require_once("included_functions.php");
if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))):

$name = $_REQUEST['name'];
$email = $_REQUEST['email'];

$formerrors = false;

if ($name === '') :
    $err_name = '<div class="error">Sorry, your name is a required field</div>';
endif; // input field empty

if ($email === '') :
    $err_email = '<div class="error">Sorry, your email is a required field</div>';
endif; // input field empty

if (isset($_POST['comments'])) {
        $comments = filter_var($_POST['comments'], FILTER_SANITIZE_STRING ); 
}

$formdata = array (
    'name' => $name,
    'email' => $email,
    'fictioninterest' => $fictioninterest,
    'bookclub' => $bookclub,
    'newsletter' => $newsletter,
    'blogtalk' => $blogtalk,
    'skype' => $skype

);

if (!($formerrors)) :
    $to             =   "bookclub@literaryfictionreview.com";
    $subject    =       "From $name -- Signup Page";
    $message    =       json_encode($formdata);

    $replyto    =       "From: bookclub@literaryfictionreview.com \r\n".
                        "Reply-To: bookclub@literaryfictionreview.com \r\n";

    if (mail($to, $subject, $message)):
        $msg = "";
    redirect_to("confirmation.html");
    else:
        $msg = "Problem sending the message";
    endif; // mail form data

endif; // check for form errors

endif; //form submitted
 ?>

【问题讨论】:

  • 你有什么问题?
  • 由于您没有显示任何实际的 php 处理代码,我们无法真正帮助您。
  • 提示:计算右/左括号的金额。
  • var_dump($fictioninterest); 打印什么? (放在开头,在调用isset($fictioninterest)之前)
  • 我已经在上面添加了我的 php 处理代码,并且更清楚地解释了我的问题 - 非常感谢您尝试帮助我!

标签: php checkbox


【解决方案1】:

您的问题可能是没有定义像$fictoninterest 这样的变量。你只声明了$name$email。所以你可以添加类似的东西

$fictioninterest = $_REQUEST['fictioninterest'];

到您的代码 - 当然,您必须对所有要使用的输入字段执行此操作($comments$blogtalk 等)。哦,你可能会使用$_POST 而不是$_REQUEST

【讨论】:

  • 非常感谢乔尔!这对除了 cmets 文本区域部分之外的所有部分都有效,我在 php 处理部分中添加了该部分: $cmets = $_POST['cmets'];但是评论部分没有出现在生成的电子邮件中。我完全按照我对其他部分所做的那样做,文本区域需要什么不同吗?
  • 只是不确定如何使文本区域部分从表单显示到电子邮件中?
  • 很高兴能帮上忙。 cmets 没有出现在邮件中,因为您可能忘记在 $formdata-array 中添加 'comments' =&gt; $comments,
  • 你完全正确!非常感谢乔尔的帮助!我对 php 完全陌生,你一直是个奇迹 - 我很感激,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-18
  • 1970-01-01
  • 2016-12-07
  • 1970-01-01
  • 1970-01-01
  • 2016-10-30
相关资源
最近更新 更多