Jump to content

HTML Forms


Drezard

Recommended Posts

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

[CODE]
<form>
Name: <input type:'text' name:'name'>
</form>

if (!isset($_POST['name'])) {
// Run code'
}
?>
[/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
Link to comment
Share on other sites

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.

[code]
<?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>

[/code]

Link to comment
Share on other sites

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

[code]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;[/code]

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.