Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't my basic PHP validation process in IE, FireFox or Opera?

The PHP based validation of my form does not work in the latest versions IE, Firefox or Opera. I click the submit button with empty values in my form fields to trigger validation but the page simply refreshes.

The odd thing is that validation and error messages work in Chrome and Safari.

View this for yourself here: http://www.directsponsor.org/contact/

Can anyone advise me on known issues regarding why PHP might fail on certain browsers certain browsers behave differently?

Apologies for the mountains of code in advance, though I presume its required:

Here's the form:

    <form method="post" action="http://www.directsponsor.org/contact/">
  <fieldset>
  <ul>
  <?php if(isset($_POST['contactsubmit'])){
  if (!$contactnamevalidation){
  echo "<li class='contactfail'><p><img src='"; echo bloginfo('stylesheet_directory'); echo "/images/icons/101-large.png'>&nbsp;&nbsp;&nbsp;You did not input your <strong>name</strong>.</p></li>";
  }
  }
  ?>
  <li>
    <label for="contactname">Your name <em>(required)</em></label> 
    <input type="text" name="contactname" class="textbox" value="<?php if (isset($_POST['contactname'])) {echo htmlspecialchars($_POST['contactname'], ENT_QUOTES);    }?>">
 </li>
 <?php if(isset($_POST['contactsubmit'])){
  if (!$contactemailvalidation1){
  echo "<li class='contactfail'><p><img src='"; echo bloginfo('stylesheet_directory'); echo "/images/icons/101-large.png'>&nbsp;&nbsp;&nbsp;You did not input your <strong>email</strong>.</p></li>";
  }
  }
  ?>
 <?php if(isset($_POST['contactsubmit'])){
  if (!$contactemailvalidation2){
  echo "<li class='contactfail'><p><img src='"; echo bloginfo('stylesheet_directory'); echo "/images/icons/101-large.png'>&nbsp;&nbsp;&nbsp;You did not input a <strong>valid email</strong>.</p></li>";
  }
  }
  ?>
  <li>
    <label for="contactemail">Your email <em>(required)</em></label>
    <input type="text" name="contactemail" class="textbox" value="<?php if (isset($_POST['contactemail'])) {echo htmlspecialchars($_POST['contactemail'], ENT_QUOTES);    }?>">
 </li>
 <?php if(isset($_POST['contactsubmit'])){
  if (!$contactmessagevalidation){
  echo "<li class='contactfail'><p><img src='"; echo bloginfo('stylesheet_directory'); echo "/images/icons/101-large.png'>&nbsp;&nbsp;&nbsp;You did not input a <strong>message</strong>.</p></li>";
  }
  }
  ?>
  <li>
    <label for="contactmessage">Message <em>(required)</em></label>
    <textarea name="contactmessage"><?php if (isset($_POST['contactmessage'])) {echo htmlspecialchars($_POST['contactmessage'], ENT_QUOTES);    }?></textarea>
  </li>
  <li class="buttons">
  <label for="contactsubmit">&nbsp;</label><input type="image" src="/wp-content/themes/directsponsor/images/button-sendmessage.png" name="contactsubmit" value="Submit Message">

  <?php if(isset($_POST['contactsubmit'])){echo"<a href='http://www.directsponsor.org/contact/'><img src='/wp-content/themes/directsponsor/images/button-clearform.png'/></a>";} ?>
  </li>
  </ul>
  </fieldset>
  </form>

Here's the (relevant part of the) validation:

    <?php 
if(isset($_POST['contactsubmit'])){

//Validation here.

}
?>

The answer

As suggested by Dr. Molle, IE, FireFox and Opera handle the value of images used as submit button differently in comparison to Chrome and Safari.

My validation began with:

if(isset($_POST['contactsubmit'])){

IE, Firefox and Opera posts the following values:

//Values for image as a submit button.
contactsubmit_x
contactsubmit_y

Since both of these value are different to contactsubmit, the if(isset()) condition isn't triggered. To rememdy this I needed to use a form element other than the image as a submit button such as the email field:

if(isset($_POST['contactemail'])){

This will trigger the condition for validation whether or not contactemail is empty or not.

like image 901
Dominor Novus Avatar asked Apr 23 '26 16:04

Dominor Novus


1 Answers

Look at this:

if(isset($_POST['contactsubmit']))

this variable does'nt exists in the failing browsers, if you use an image-submit there are variables $_POST['contactsubmit_x'] and $_POST['contactsubmit_y'] instead.

It works in Chrome/Safari because they also send the variable "contactsubmit" if you set a value-attribute for the image-submit(but the occurence of this variable isn't reliable as you see).

But I would suggest not to use those variables at all. It depends on the browser if those variables will be send always or only if the button has been clicked, so you run into problems when a user uses [ENTER] to submit the form in some browsers.

Check another variable, e.g. $_POST['contactemail'] , instead.

like image 64
Dr.Molle Avatar answered Apr 26 '26 06:04

Dr.Molle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!