Jump to content

Recommended Posts

Hi, I am searching for a basic, easy to implement php form validation script that checks if the user has filled out a field, if they havent to tell them so.

 

i have tried basic if statements but it refreshes the page and all the other fields that were filled in are clear. Anyone know of any tricks or scripts that are good?

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/222080-simple-php-from-validation/
Share on other sites

You want to add a simple javascript function to your page. This will allow you to check the fields without refreshing the page.

 

If you want to use php, then the page will need to be refreshed each time, as the php get's executed back at the server, not up where the client is.

 

If you want an example of some javascript code, let me know. Otherwise just google it :).

 

Denno

I was merely suggesting javascript to check if the field has been filled in, as the original question asked.

 

Validating if the entered data matches when compared to a db (or something), like for usernames or passwords, is a different matter.

 

Denno

... that checks if the user has filled out a field, if they havent to tell them so.

 

Nothing is said about validating the actual information in the field. Only that there is something in there.

 

I don't want to start an argument, but from reading the original question, this it he impression that I got as to what was being asked.

 

Denno

so, anyway... check to see if the form is posted. if it is posted, validate the values. if they do not validate, display the posted values in the form fields. if the form is NOT posted, initialize those values, so you display the POST'ed values or the initialized values in the form fields. This is terribly simplified and untested:

 

<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    $fname = $_POST['fname'];

    // Validation here

    // If no errors, process request and display success message or header() to another page.

    // else prepare the post'ed value for re-display
    $fname = htmlspecialchars(strip_slashes($_POST['fname'));
} else {
    $fname = '';
}
?>
somewhere in a form... <br />
<input type='text' name='fname' value='<?php echo $fname; ?>' />

Send results back to your form, u can use $_GET

<?php
$username=isset($_GET['uname'])?$_GET['uname']:'';
?>
<form action="form_action.php" method="post">
  User name: <input type="text" name="uname" /><br />
  <input type="submit" value="Submit" />
</form>

 

in your process form

<?php
$uname=issset($_POST['uname'])?$_POST['uname']:'';
if(!is_valid($uname))
     header('Location: form.php?uname='.urlencode($uname))

 

well that should give u an idea of a pure php solution, but i would never send the password using this method (have them re-enter it)

 

Note: Me & Blue posted about the same time. His Solution is based on an all in one form/processing script. The solution i presented here is for seperate form/processing. Both will work, just depends which system you implement.

 

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.