Problem with PHP form not submitting on IE 8, But does submi

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeweline
Forum Newbie
Posts: 9
Joined: Fri Sep 11, 2009 3:24 pm

Problem with PHP form not submitting on IE 8, But does submi

Post by jeweline »

Hi All,

I am a newbie to this forum and a newbie to scripting. In fact this is my first type trying to make a form work. I've created a PHP contact form that when the submit button is clicked, I want it to send an e-mail to a certain address. Thanks so much for taking the time to look at this and help me! I'm so confused and frustrated! :banghead: Any help offered would be greatly appreciated. Thanks!

I created the form in Dreamweaver. I first added code for server-side verification of the fields and then I used Dreamweaver's Spry Validation widgets to add client-side verification of the form elements.

In Firefox and Google Chrome, when I test the form by correctly filling out the different form elements and clicking the submit button:
  • the form gets submitted
    the "thank you" message correctly displays
    and I receive an e-mail in my mailbox
The problem is with Internet Explorer 8. When I test the form by correctly completing the various form elements and clicking the submit button:

The form doesn't get submitted because
  • I see the following statement "Sorry, there was a problem sending your message. Please try later." This statement displays because part of an "If-then-else" statement failed. It is part of the client-side verification code I created
    Since, the form wasn't submitted, I don't get the resulting e-mail.
I really don't know enough about PHP to debug. All I can figure is that for some reason IE thinks the $_POST and $mailSent variables are empty, so it displays the "sorry, there was a problem" error message.

Here's a link to the form I'm testing:
http://www.buypuresilverbullion.com/sil ... tactus.php

[b]Here's the code I'm using in the contact form page to display either error messages or a confirmation message:[/b]

Code: Select all

<?php
if ($_POST && isset($missing) && !empty($missing)) {
?>
  <p class="warning2"><strong>Please complete the missing item(s) indicated.</strong></p>
<?php
} elseif ($_POST && !$mailSent) {
?>
  <p class="warning2">Sorry, there was a problem sending your message. Please try later.</p>
<?php
} elseif ($_POST && $mailSent) {
?>
  <p class="acknowledgement"><strong>Thanks so much for taking the time to send us a note! We’ve received your message, and will contact you shortly!</strong></p>
<?php } ?>

Here's the code of the include file that contains the script to process the form:

Code: Select all

<?php
if (isset($_SERVER['SCRIPT_NAME']) && strpos($_SERVER['SCRIPT_NAME'], '.inc.php')) exit;
 
// remove escape characters from POST array
if (PHP_VERSION < 6 && get_magic_quotes_gpc()) {
  function stripslashes_deep($value) {
    $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
    return $value;
  }
  $_POST = array_map('stripslashes_deep', $_POST);
}
 
// assume that there is nothing suspect
  $suspect = false;
  // create a pattern to locate suspect phrases
  $pattern = '/Content-Type:|Bcc:|Cc:/i';
  
  // function to check for suspect phrases
  function isSuspect($val, $pattern, &$suspect) {
    // if the variable is an array, loop through each element
    // and pass it recursively back to the same function
    if (is_array($val)) {
      foreach ($val as $item) {
         isSuspect($item, $pattern, $suspect);
      }
    } else {
      // if one of the suspect phrases is found, set Boolean to true
      if (preg_match($pattern, $val)) {
        $suspect = true;
      }
    }
  }
 
  // check the $_POST array and any subarrays for suspect content
  isSuspect($_POST, $pattern, $suspect);
 
  if ($suspect) {
    $mailSent = false;
    unset($missing);
  } else {
    // process the $_POST variables
    foreach ($_POST as $key => $value) {
      // assign to temporary variable and strip whitespace if not an array
      $temp = is_array($value) ? $value : trim($value);
      // if empty and required, add to $missing array
      if (empty($temp) && in_array($key, $required)) {
        array_push($missing, $key);
      } elseif (in_array($key, $expected)) {
        // otherwise, assign to a variable of the same name as $key
        ${$key} = $temp;
      }
    }
  }
  
  // validate the email address
  if (!empty($email)) {
    // regex to identify illegal characters in email address
    $checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
    // reject the email address if it doesn't match
    if (!preg_match($checkEmail, $email)) {
      $suspect = true;
      $mailSent = false;
      unset($missing);
    }
  }
 
  
  // go ahead only if not suspect and all required fields OK
  if (!$suspect && empty($missing)) {
  
     // initialize the $message variable
     $message = '';
     // loop through the $expected array
     foreach($expected as $item) {
       // assign the value of the current item to $val
       if (isset(${$item}) && !empty(${$item})) {
         $val = ${$item};
       } else {
         // if it has no value, assign 'Not selected'
         $val = 'Not selected';
       }
       // if an array, expand as comma-separated string
       if (is_array($val)) {
         $val = implode(', ', $val);
       }
       // add label and value to the message body
       $message .= ucfirst($item).": $val\n\n";
     }
 
    // limit line length to 70 characters
    $message = wordwrap($message, 70);
 
     // create Reply-To header
     if (!empty($email)) {
       $headers .= "\r\nReply-To: $email";
     }
 
     
    // send it  
 
    $mailSent = mail($to, $subject, $message, $headers);
    if ($mailSent) {
      // $missing is no longer needed if the email is sent, so unset it
      unset($missing);
    }
  }
?>
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Problem with PHP form not submitting on IE 8, But does submi

Post by Eric! »

For the most part this is browser independant. However there must be some quirk that makes your code work for some browsers. I think I would need to see the full form to understand what you are doing. But I don't get what this line is trying to do:

Code: Select all

if ($_POST && isset($missing) && !empty($missing))
if($_POST) I guess it would return false if nothing has been posted from your form, but it's a little odd, I don't know exactly how this would work for all cases. Usually you check the status of your $_POST['submit_button_name'] to see if data was submitted or not.

if(!isset($missing)) doesn't mean much to me unless somewhere you have both set $_POST['missing']=somevalue and then before your if statement you pulled it back out of the post $missing=$_POST['missing'];

Likewise for if(!empty($missing))

Sorry, I'm not much more help. Maybe someone else can deduce what's wrong, but I need to see the full code.
jeweline
Forum Newbie
Posts: 9
Joined: Fri Sep 11, 2009 3:24 pm

Re: Problem with PHP form not submitting on IE 8, But does submi

Post by jeweline »

Thanks Eriic!

I tried to post the entire code for the form, but ran into the 60,000 character limit.(I have a select box listing countries -- and there's a lot of countries in this world!)

I used several exercises in the book titled, "The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP" by David Powers. This is what he said about the if-then-else condition statement that displays the error messages or the confirmation:

The isset() function checks whether a variable exists. If $missing doesn't exist, that means that all required fields were filled in and the email was sent successfully, so the condition fails, and the script moves on to the consider the else-if condition. However, if all required fields were filled in but there was a problem sending the email, $missing still exists, so you need to make sure it's empty.

On the other hand, if $missing exists and isn't empty, you know that at least one required field was omitted, so the warning message is displayed. The $mailSent variable won't even be set if any required fields have been omitted, so you must test for $missing first.

*****************
I have found that if I swap the image file that I'm using the submit button to the standard form button, the form will be submitted in IE.

Here's the php code I have at the very top of the page:

Code: Select all

<?php
if (array_key_exists('send', $_POST)) {
    // mail processing script
    $to = 'jeweline1961@gmail.com';
    $subject = 'Contact Form Inquiry from Silverline Market Website Visitor';
    
 // list expected fields
  $expected = array('First_Name', 'Last_Name', 'company', 'Street_Address1', 'Street_Address2', 'city', 'state_region', 'Postal_Code',  'country_code', 'daytime_phone', 'evening_phone', 'email', 'comments', 'prefer_to_be_contacted_by', 'best_time_to_call');
 //set required fields
  $required = array('First_Name', 'Last_Name', 'Street_Address1', 'city', 'state_region', 'Postal_Code', 'country_code', 'daytime_phone', 'comments', 'state');
  // create empty array for any missing fields
  $missing = array();
 // create additional headers
     $headers = "From: Silverline Market Corporation<jeweline1961@gmail.com>\r\n";
     $headers .= 'Content-Type: text/plain; charset=utf-8';
     $process = 'includes/process_mail.inc.php';
      if (file_exists($process) && is_readable($process)) {
        include($process);
      } else {
        $mailSent = false;
        mail($to, 'Server problem', "$process cannot be read", $headers);
      }
    }
?>
jeweline
Forum Newbie
Posts: 9
Joined: Fri Sep 11, 2009 3:24 pm

Re: Problem with PHP form not submitting on IE 8, But does submi

Post by jeweline »

Eric!

Here's the code for the form (minus the country select menu): Thanks so much!

Code: Select all

<form id="frmcontactform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
 <div>
  <fieldset>
   <div><label for="First_Name">*First Name:
   <?php
    if (isset($missing) && in_array('First_Name', $missing)) { 
   ?>
   <span class="warning"> Please enter your first name</span>
   <?php } ?>
   </label>
     <span id="spryfirstname">
     <input name="First_Name" type="text" class="inputstyle" id="First_Name" 
      <?php if (isset($missing)) {
echo 'value="' . htmlentities($_POST['First_Name'], ENT_COMPAT, 'UTF-8') . '"';
} ?>
          />
     <span class="textfieldRequiredMsg"><span class="textfieldRequiredMsg">A
     value is required.</span></span><span class="textfieldMaxCharsMsg">Exceeded
     maximum number of characters.</span></span></div>
   <div><label for="Last_Name">*Last Name: <?php
   if (isset($missing) && in_array('Last_Name', $missing)) { ?>
   <span class="warning"> Please enter your last name.</span><?php } ?></label>
     <span id="sprylastname">
     <input name="Last_Name" type="text" class="inputstyle" id="Last_Name" 
        <?php if (isset($missing)) {
echo 'value="' . htmlentities($_POST['Last_Name'], ENT_COMPAT, 'UTF-8') . '"';
} ?>
         />
     <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldMaxCharsMsg">Exceeded
     maximum number of characters.</span></span></div>
   <div><label for="company">Company:</label>
     <span id="sprycompany">
     <input name="company" type="text" class="inputstyle" id="company" size="41" maxlength="50" />
     <span class="textfieldMaxCharsMsg">Exceeded maximum number of characters.</span></span></div>
   <div><label for="Street_Address1">*Street Address 1: <?php
   if (isset($missing) && in_array('Street_Address1', $missing)) { ?>
   <span class="warning"> Please enter the first line of your street address.</span>
   <?php } ?>
   </label>
     <span id="spryaddress1">
     <input name="Street_Address1" type="text" class="inputstyle"
        <?php if (isset($missing)) {
echo 'value="' . htmlentities($_POST['Street_Address1'], ENT_COMPAT, 'UTF-8') . '"';
} ?>
         />
     <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldMaxCharsMsg">Exceeded
     maximum number of characters.</span></span></div>
   <div><label for="Street_Address2">Street Address 2:</label>
     <span id="spryaddress2">
     <input name="Street_Address2" type="text" class="inputstyle" id="Street_Address2" size="41" maxlength="50" />
<span class="textfieldMaxCharsMsg">Exceeded
     maximum number of characters.</span></span></div>
   <div>
        <label for="city">*City:<?php
   if (isset($missing) && in_array('city', $missing)) { ?>
   <span class="warning"> Please enter the city.</span><?php } ?>
        </label>
        <span id="sprycity">
        <input name="city" type="text" class="inputstyle" id="city"
        <?php if (isset($missing)) {
echo 'value="' . htmlentities($_POST['city'], ENT_COMPAT, 'UTF-8') . '"';
} ?>
         />
        <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldMaxCharsMsg">Exceeded
        maximum number of characters.</span></span></div>
   <div>
        <label for="state_region">*State/Province/Region:<?php
   if (isset($missing) && in_array('state_region', $missing)) { ?>
   <span class="warning"> Please enter a state, province or region.</span><?php } ?>
        
        </label>
        <span id="sprystateregion">
        <input name="state_region" type="text" class="inputstyle" id="state_region" 
        <?php if (isset($missing)) {
        echo 'value="' . htmlentities($_POST['state_region'], ENT_COMPAT, 'UTF-8') . '"';
        } ?>
        />
        <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldMaxCharsMsg">Exceeded
        maximum number of characters.</span></span></div>
   <div>
     <label for="Postal_Code">*Zip:<?php
   if (isset($missing) && in_array('Postal_Code', $missing)) { ?>
   <span class="warning"> Please enter your zip code.</span><?php } ?>
     </label>
     <span id="sprypostalcode">
     <input name="Postal_Code" type="text" class="inputstyle" id="Postal_Code" 
        <?php if (isset($missing)) {
echo 'value="' . htmlentities($_POST['Postal_Code'], ENT_COMPAT, 'UTF-8') . '"';
} ?>
         />
     <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid
     format.</span><span class="textfieldMaxCharsMsg">Exceeded maximum number
     of characters.</span></span></div>
   <div>
     <label for="country">*Country:<?php
   if (isset($missing) && in_array('country_code', $missing)) { ?>
   <span class="warning"> Please select a country.</span>
   <?php } ?> 
     </label>
   <div>
   <p id="website">
    <label for="url">Website: </label>
    <input type="text" name ="url" id="url" />
   </p>
   </div>
   <div>
        <label for="daytime_phone">*Daytime Phone:
          <?php
   if (isset($missing) && in_array('daytime_phone', $missing)) { ?>
   <span class="warning"> Please enter your daytime phone number.</span><?php } ?>
        </label>
        <span id="sprydayphone">
        <input type="text" class="inputstyle" name="daytime_phone" id="daytime_phone"  
        <?php if (isset($missing)) {
echo 'value="' . htmlentities($_POST['daytime_phone'], ENT_COMPAT, 'UTF-8') . '"';
} ?>
         />
        <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid
        format.</span></span>
        <div class="instructions">*Example: (800) 704-1596</div>
   </div>
   <div>
     <label for="evening_phone">Evening Phone:</label>
     <span id="spryeveningphone">
     <input name="evening_phone" type="text" class="inputstyle" id="evening_phone" size="41" maxlength="50" />
     <span class="textfieldInvalidFormatMsg">Invalid format.</span></span></div>
   <div>
     <label for="email">E-mail Address:</label>
     <span id="spryemail">
     <input type="text" name="email2" id="email" size="41" class="inputstyle" />
     <span class="textfieldInvalidFormatMsg">Invalid format.</span><span class="textfieldMaxCharsMsg">Exceeded
     maximum number of characters.</span></span></div>
   <div>
     <label for="comments">*Comments  <?php
   if (isset($missing) && in_array('comments', $missing)) { ?>
   <span class="warning"> Please enter your comments.</span><?php } ?></label>
     <span id="sprycomments">
     <textarea id="comments" name="comments" cols="31" rows="5" class="textareastyle"><?php if (isset($missing)) {
echo htmlentities($_POST['comments'], ENT_COMPAT, 'UTF-8');}?></textarea>
     <span> Count: </span><span id="countsprycomments">&nbsp;</span><span class="textareaRequiredMsg">A
     value is required.</span><span class="textareaMaxCharsMsg">Exceeded maximum
     number of characters.</span></span></div>
   <div>
     <label for="preferredway">Preferred Way to 
       Contact You:</label>
        <input type="radio" name="prefer_to_be_contacted_by" value="Email" checked="checked" 
        <?php
        if (!$_POST || isset($missing) && $_POST['Email'] == 'Email') {
          echo 'checked="checked"';
        } ?> 
        /> Email &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        <span class="rightac">
        <input type="radio" name="prefer_to_be_contacted_by" value="Phone" 
        <?php
        if (isset($missing) && $_POST['Phone'] == 'Phone') {
          echo 'checked="checked"';
        } ?>
        /> 
        Phone</span><br></br>
   </div>
   <div>
     <label for="bestway">Best Time to  Reach You:</label>
          <p>
            <input type="radio" name="best_time_to_call" value="Morning" checked="checked" 
            <?php
            if (!$_POST || isset($missing) && $_POST['Morning'] == 'Morning') {
              echo 'checked="checked"';
            } ?>  
        />
            Morning &nbsp; &nbsp; &nbsp;
            <input type="radio" name="best_time_to_call" value="Noon" 
            <?php
              if (isset($missing) && $_POST['Noon'] == 'Noon') {
                echo 'checked="checked"';
              } ?>
            /> 
            Noon &nbsp; &nbsp; &nbsp;
              <input type="radio" name="best_time_to_call" value="Evening" 
              <?php
              if (isset($missing) && $_POST['Evening'] == 'Evening') {
                echo 'checked="checked"';
              } ?>
              /> Evening<br></br>
          </p>
       <!-- </blockquote>-->
   </div>
   <div class="centerac">
     <p>
       <input type="image" src="images/contactmedia/contact_btn.png" alt="Submit button" title="Send Us Your Comments" name="send" id="sendusyourcomments" value="Send us your comments!" width="232" height="33" hspace="0" vspace="0" border="0" />
     </p>
   </div>
  </fieldset>
 </div>
 </form>
jeweline
Forum Newbie
Posts: 9
Joined: Fri Sep 11, 2009 3:24 pm

Re: Problem with PHP form not submitting on IE 8, But does submi

Post by jeweline »

Here's the code for the include file that is called in the form page:

Code: Select all

<?php
if (isset($_SERVER['SCRIPT_NAME']) && strpos($_SERVER['SCRIPT_NAME'], '.inc.php')) exit;
 
// remove escape characters from POST array
if (PHP_VERSION < 6 && get_magic_quotes_gpc()) {
  function stripslashes_deep($value) {
    $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
    return $value;
  }
  $_POST = array_map('stripslashes_deep', $_POST);
}
 
// assume that there is nothing suspect
  $suspect = false;
  // create a pattern to locate suspect phrases
  $pattern = '/Content-Type:|Bcc:|Cc:/i';
  
  // function to check for suspect phrases
  function isSuspect($val, $pattern, &$suspect) {
    // if the variable is an array, loop through each element
    // and pass it recursively back to the same function
    if (is_array($val)) {
      foreach ($val as $item) {
         isSuspect($item, $pattern, $suspect);
      }
    } else {
      // if one of the suspect phrases is found, set Boolean to true
      if (preg_match($pattern, $val)) {
        $suspect = true;
      }
    }
  }
 
  // check the $_POST array and any subarrays for suspect content
  isSuspect($_POST, $pattern, $suspect);
 
  if ($suspect) {
    $mailSent = false;
    unset($missing);
  } else {
    // process the $_POST variables
    foreach ($_POST as $key => $value) {
      // assign to temporary variable and strip whitespace if not an array
      $temp = is_array($value) ? $value : trim($value);
      // if empty and required, add to $missing array
      if (empty($temp) && in_array($key, $required)) {
        array_push($missing, $key);
      } elseif (in_array($key, $expected)) {
        // otherwise, assign to a variable of the same name as $key
        ${$key} = $temp;
      }
    }
  }
  
  // validate the email address
  if (!empty($email)) {
    // regex to identify illegal characters in email address
    $checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
    // reject the email address if it doesn't match
    if (!preg_match($checkEmail, $email)) {
      $suspect = true;
      $mailSent = false;
      unset($missing);
    }
  }
 
  
  // go ahead only if not suspect and all required fields OK
  if (!$suspect && empty($missing)) {
  
     // initialize the $message variable
     $message = '';
     // loop through the $expected array
     foreach($expected as $item) {
       // assign the value of the current item to $val
       if (isset(${$item}) && !empty(${$item})) {
         $val = ${$item};
       } else {
         // if it has no value, assign 'Not selected'
         $val = 'Not selected';
       }
       // if an array, expand as comma-separated string
       if (is_array($val)) {
         $val = implode(', ', $val);
       }
       // add label and value to the message body
       $message .= ucfirst($item).": $val\n\n";
     }
 
    // limit line length to 70 characters
    $message = wordwrap($message, 70);
 
     // create Reply-To header
     if (!empty($email)) {
       $headers .= "\r\nReply-To: $email";
     }
 
     
    // send it  
 
    $mailSent = mail($to, $subject, $message, $headers);
    if ($mailSent) {
      // $missing is no longer needed if the email is sent, so unset it
      unset($missing);
    }
  }
?>
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Problem with PHP form not submitting on IE 8, But does submi

Post by Eric! »

I'm sorry, I skimmed your code, but I couldn't quickly find how $missing is being passed around. I just don't have the time today. Anyone else want to try?
Post Reply