Jump to content


Photo

HTML Forms


  • Please log in to reply
6 replies to this topic

#1 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 24 September 2006 - 04:13 AM

Hello, I was wondering. If I had this script:

<form>
Name: <input type:'text' name:'name'>
</form>

if (!isset($_POST['name'])) {
// Run code'
}
?>

Some websites (im not sure if its HTML, PHP or Javascript) when you dont input values into a form then submit they highlight the bit of the form that you didnt fill in then in writing under it put "Please insert you name". And the form is still on the page. How do you do that in PHP.

- Cheers, Daniel

#2 invincible_virus

invincible_virus
  • Members
  • PipPip
  • Member
  • 27 posts
  • LocationDelhi, India

Posted 24 September 2006 - 04:22 AM

why would you like to do it using PHP, when this can be easily done using client side scripting (javascript) ??
PG accommodations in India - http://www.pgspace.com/
Technical Forum - http://www.enterpriseplatform.net/

#3 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 24 September 2006 - 04:28 AM

It can be done either way.

PHP would have to wait till the form is submitted and the server would have to check for you, whereas Javascript would do it before any of the information was sent to the server.
 
To do it with PHP would take a bit more code than I believe it would with javascript.  You would have to set up an array of values that were incorrect/missing etc and check them on the refresh of the page.  To me it's a bit hard to explain but it can be done.

<?php
if(isset($_POST['submit'])) {
  // user submitted form
  if(isset($_POST['name'])) {
    // do your stuff here
  }

} 
// Show form to user
?>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
  <input type="text" name="name" value="<?php if (isset($_POST['name'])) echo $_POST['name'];?>" ?>
  <input type="submit" value="Submit" name="submit" />
</form>




#4 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 24 September 2006 - 04:46 AM

how would u do it with javascript then?



#5 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 24 September 2006 - 06:30 AM

plz help.

- Cheers, Daniel

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 25 September 2006 - 04:03 AM

You simply need to write a routine that goes though a list of "required" fields, and validates each according to input type.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#7 KevinM1

KevinM1
  • Moderators
  • Snarkimus Prime
  • 5,248 posts

Posted 26 September 2006 - 04:24 PM

A simple JavaScript form validator I created (external .js file):

var W3CDOM = (document.createElement && document.getElementsByTagName);

function init(){
   if (!W3CDOM) return;
   var inputform = document.getElementById('inputform');
   inputform.onsubmit = validate;
}

function validate(evt){
   evt = (evt) ? evt : ((event) ? event : null);

   if(evt){
      var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);

      if(elem){
         var name__not_empty, name, pass_not_empty, pass, email__not_empty, email;

         name_not_empty = isNotEmpty(elem.username);
         name = isName(elem.username);
         pass_not_empty = isNotEmpty(elem.password);
         pass = len16(elem.password);
         email_not_empty = isNotEmpty(elem.email);
         email = validEmail(elem.email);

         if(name_not_empty && name && pass_not_empty && pass && email_not_empty && email){
            return true;
         }

         else {
            return false;
         }
      }
   }
}

function isNotEmpty(thing){
   str = thing.value;
   re = /.+/;

   if(!str.match(re)){
      alert(thing.id + " is empty!");
      return false;
   }

   else {
      return true;
   }
}

function isName(thing){
   str = thing.value;
   re = /^[a-zA-Z]+([ a-zA-Z-]+)*$/;

   if(!str.match(re)){
      alert("'" + thing.value + "'" + " is not a name!");
      return false;
   }

   else {
      return true;
   }
}

function len16(thing){
   str = thing.value;
   re = /\b.{16}\b/;

   if(!str.match(re)){
      alert("Your password is not 16 characters long!");
      return false;
   }

   else {
      return true;
   }
}

function validEmail(thing){
   str = thing.value;
   re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;

   if(!str.match(re)){
      alert("You did not enter a valid e-mail address!");
      return false;
   }

   else {
      return true;
   }
}

window.onload = init;

This isn't supposed to cure all your problems.  Your form will most likely be different than mine, so you'd need to modify the validator's code to match what's in your form.  But, at the very least, this should give you some idea on how to approach your problem.
Using 'global' is a sign of doing it wrong




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users