ChrisBedoya
ChrisBedoya

Reputation: 747

undefined variable php form

I'm working on this PHP Form and I keep running into this problem.

<?php
  // User settings
  $to = "[email protected]";
  $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;
?>

When I try to send an email it says that I have an undefined variable on line 81:

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

When I remove the last $stateSelect and add "" it removes the error, but then when I send the form that field shows up empty.

The $country and $stateSelect are dropdowns. Whatever I select in $country affects the $stateSelect dropdown.

Upvotes: 0

Views: 1818

Answers (2)

V a a m Y o b
V a a m Y o b

Reputation: 492

Looks like there are 2 problems:

  1. the send stage is not sending the stateSelect request parameter
  2. when that occurs, at line 81, you're trying to assign the at-that-time-undefined variable $stateSelect to itself

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

I'd check to make sure the stateSelect dropdown's name is stateSelect (not just the id), to solve the first problem.

The second problem, I'd figure out a way to do this:

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

Upvotes: 0

Marc B
Marc B

Reputation: 360762

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

if the POST value isn't set, then you try to set stateSelect to equal itself. At this point, stateSelect hasn't been defined, so you're assigning an undefined variable to itself.

Upvotes: 3

Related Questions