未定义的变量php形式

时间:2011-05-13 19:59:32

标签: php forms

我正在处理这个PHP表单,我一直在遇到这个问题。

<?php
  // User settings
  $to = "myemail@yahooo.com";
  $subject = "Test Form";

  // Include extra form fields and/or submitter data?
  // false = do not include
  $extra = array(
    "form_country"  => true,
    "form_stateSelect" => true
  );

  // Process
  $action = isset($_POST["action"]) ? $_POST["action"] : "";

  if (empty($action)) {
    // Send back the contact form HTML
    $output = "<div style='display:none'> 
    <div class='contact-content'>
      <h1 class='contact-title'>Send us a message:</h1>
      <div class='contact-loading' style='display:none'></div>
      <div class='contact-message' style='display:none'></div>
      <form action='#' style='display:none'>
        <label for='contact-name'>Name</label>
        <input type='text' id='contact-name' class='contact-input' name='name'  />";

    if ($extra["form_country"]) {
      $output .= "
        <label for='contact-country'>Servicio</label>
        <table>
          <tr>
            <td>
            <select id='contact-country' name='country' onchange='populateState()'>
            </select>
            </td>
          </tr>
          </table>";
    }
    if ($extra["form_stateSelect"]) {
      $output .= "
        <label for='contact-stateSelect'>Sub-servicio</label>
              <table>
        <td>
            <select id='contact-stateSelect' name='stateSelect'>
            </select>
            <script language='javascript'>initCountry('US'); </script>
            </td>           </table>
      ";
    }

    $output .= "
        <label>&nbsp;</label>
        <button type='submit' class='contact-send contact-button' tabindex='1006'>Send</button>
        <button type='submit' class='contact-cancel contact-button simplemodal-close' tabindex='1007'>Cancel</button>
        <br/>
        <input type='hidden' name='token' value='" . smcf_token($to) . "'/>
      </form>
    </div>
  </div>";

    echo $output;
  }
  else if ($action == "send") {
    // Send the email
    $name = isset($_POST["name"]) ? $_POST["name"] : "";
    $country = isset($_POST["country"]) ? $_POST["country"] : $country;
    $stateSelect = isset($_POST["stateSelect"]) ? $_POST["stateSelect"] : $stateSelect ;
    $token = isset($_POST["token"]) ? $_POST["token"] : "";

    // make sure the token matches
    if ($token === smcf_token($to)) {
      smcf_send($name, $country, $stateSelect );
      echo "Your message was successfully sent.";
    }
    else {
      echo "Unfortunately, your message could not be verified.";
    }
  }

  function smcf_token($s) {
    return md5("smcf-" . $s . date("WY"));
  }

  // Validate and send email
  function smcf_send($name, $country, $stateSelect) {
    global $to, $extra;

    // Filter and validate fields
    $name = smcf_filter($name);
    $country = smcf_filter($country);
    $stateSelect = smcf_filter($stateSelect);

    // Set and wordwrap message body
    $body = "From: $name\n\n";
    $body .= "Servicio: $country\n\n";
    $body .= "Sub-servicio: $stateSelect";

    // Build header
    $headers = "From: $name\n";
    $headers .= "X-Mailer: PHP/SimpleModalContactForm";

    // UTF-8
    if (function_exists('mb_encode_mimeheader')) {
      $country = mb_encode_mimeheader($country, "UTF-8", "B", "\n");
      $stateSelect = mb_encode_mimeheader($stateSelect, "UTF-8", "B", "\n");
    }
    else {
      // you need to enable mb_encode_mimeheader or risk 
      // getting emails that are not UTF-8 encoded
    }
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/plain; charset=utf-8\n";
    $headers .= "Content-Transfer-Encoding: quoted-printable\n";

    // Send email
    @mail($to, $subject, $body, $headers) or 
      die("Unfortunately, a server issue prevented delivery of your message.");
  }

  // Remove any un-safe values to prevent email injection
  function smcf_filter($value) {
    $pattern = array("/\n/","/\r/","/content-type:/i","/to:/i", "/from:/i", "/cc:/i");
    $value = preg_replace($pattern, "", $value);
    return $value;
  }

  exit;
?>

当我尝试发送电子邮件时,它说第81行我有一个未定义的变量:

$stateSelect = isset($_POST["stateSelect"]) ? $_POST["stateSelect"] : $stateSelect ;

当我删除最后一个$stateSelect并添加""时,它会删除错误,但是当我发送表单时,该字段显示为空。

$country$stateSelect是下拉列表。我在$country中选择的任何内容都会影响$stateSelect下拉列表。

2 个答案:

答案 0 :(得分:3)

 $stateSelect = isset($_POST["stateSelect"]) ? $_POST["stateSelect"] : $stateSelect ;
                                                                       ^^^^^ undefined at this point

如果未设置POST值,则尝试将stateSelect设置为等于自身。此时,stateSelect尚未定义,因此您要为自己分配一个未定义的变量。

答案 1 :(得分:0)

看起来有两个问题:

  1. send阶段未发送stateSelect请求参数
  2. 当发生这种情况时,在第81行,您尝试将at-that-time-undefined变量$stateSelect分配给自身

    $ stateSelect = isset($ _ POST [“stateSelect”])? $ _POST [“stateSelect”]:$ stateSelect;

  3. 我会检查以确保stateSelect下拉列表的namestateSelect(而不仅仅是id),以解决第一个问题。

    第二个问题,我想办法做到这一点:

    $stateSelect = isset($_POST["stateSelect"]) ? $_POST["stateSelect"] : "Select One" ;