简体   繁体   中英

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.

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, eg $_POST['contactemail'] , instead.

I have just looked at your form and it seems to be working ok here using FF 3.6.10. I think this is a caching issue, clear your browsers cache and try again.

Also you may want to look at the Web Devoloper plugin for FF as it offers an easy way to disable/enable your cache. https://addons.mozilla.org/en-US/firefox/addon/web-developer/

As php is server side it will not be affected by different browsers.

Kind regards Garry

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM