0

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.

4
  • 1
    You'll need to find out why your <select name="stateSelect"> isn't getting submitted with the form. Commented May 13, 2011 at 20:07
  • Is $country declared elsewhere while $stateSelect isn't? Or maybe it's not being submitted to the form? I'm a bit out of practice with PHP, but I'm not sure what should happen if you try to reference a variable that hasn't been given a value yet. Seems logical to me that it would produce an error. Tough to tell with this code, though. Can you reproduce the problem with less code? (And complete code?) Commented May 13, 2011 at 20:08
  • As an aside, make your life easier and separate HTML and PHP. If you find yourself going in to 2+ lines of html code within a PHP string, time to escape from PHP and just output the html. You can always use output buffering to make it easier, too. -- <?php ob_start(); echo '<p>Hello'; ?> world.</p><?php $foo = ob_get_contents(); ob_end_clean(); echo '<div>Body:'.$foo.'</div>'; ?> Commented May 13, 2011 at 20:08
  • Go back and accept your answers. Commented May 13, 2011 at 20:09

2 Answers 2

3
 $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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Hi. I tried what you said, but I get "Select One" when I recieve the form, no matter what item I select. Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.