zero-perfoliate
zero-perfoliate

Author Topic: PHP Contact Form... drop down issue!  (Read 265 times)

Offline pmoz84

  • New PHP Members
  • Posts: 1
  • Karma: +0/-0
PHP Contact Form... drop down issue!
« on: March 18, 2010, 01:42:21 AM »
Hi,

I am really new to PHP and I am after some help if possible.

I have a contact form (which I am using as a questionnaire) which works fine... it gathers data and emails it out.

The problem I have is when people don't fill out all of the mandatory fields...
I have a mix of free text fields and drop down menus.

If I partly fill in the form and click Submit, the form tells you that some field haven't been completed as expected, but it resets all of the data in the drop-down menus... how can I get this to keep the option selected?

The data in the free text fields is kept!

I'm completely lost on this, so any help is appreciated.

Thanks. Paul.

hotnoob

  • Guest
Re: PHP Contact Form... drop down issue!
« Reply #1 on: March 19, 2010, 09:44:11 AM »
Please post the code...

However, from what i can tell... you are checking the fields via php...

You should check the fields with JavaScript, and then check them with php so that issue doesn't happen as often.

Here is a quick example of how to...

Code: [Select]
<script>
function check()
{
  var formObject = document.getElementById('objectId');
  var errors = 0;
  var errorTxt = "";
  if(formObject.value == null)
  {
       errors++;
       errorTxt+= "Field Empty";
  }

  if(errors == 0)
  {
    return true;
  }
  else
   {
     alert(errorTxt);
     return false;
    }

}
</script>
<form action='submit.php' method=post onSubmit="return check()">
<br /><input type=text id=objectId />
<input type=submit value='check'>
</form>

I havn't done js with forms in a while so... i might be a bit off...

However, the script is set up so that all you have to do is add is add your fields and so on...

Offline RoseKnight

  • PHP Help Guru's
  • ****
  • Posts: 52
  • Karma: +0/-0
Re: PHP Contact Form... drop down issue!
« Reply #2 on: March 19, 2010, 07:51:31 PM »
I agree with hotnoob. JS is the way to go when checking forms. But JS isn't the only way to accomplish that.

Another way would be to implement cookies to store the information
Using Session and/or your post variables to send the info back to the page as default values

but JS is way cheaper on your server resources.

hotnoob

  • Guest
Re: PHP Contact Form... drop down issue!
« Reply #3 on: March 20, 2010, 12:25:00 AM »
unfortunately, Sessions are very bad for the server's hard drives... *not quite as bad as mysql*
but there isn't much choice :P

you can avoid them with get or post variables tho... that way non of the data is written onto the hard drive... but no one thinks that much about code efficiency :P