Jump to content

check if form is filled out


RTS

Recommended Posts

put your database query inside the else statement, as shown in the example above.  basically the logic is as follows: you have a form that you want the user to fill out.  the user hits the submit button, and the data is posted to your processing script.  The processing script pretty much does this: if the form is filled out, and it is filled out the way you want it to be filled out, then update your database. if not, then redirect the user to the form with an error message.  So a very simple piece of code might look like this:

[code=php:0]
if ($_POST['blah'] != '') {
  // do your sql query with the posted var
} else {
  header('Location: form.php');
}
[/code]

or if you are doing it all on one page, then instead of redirecting to the form page with the header function, you would display the form inside the else statement. 

now in reality, your if/else should be much more complicated than this.  In reality, you would want to not only check to see if the variable exists, but you will also want to make sure the user has entered something appropriate (example, if the field is supposed to be a phone number, check to make sure it's proper format, correct area code, whatever).

Also, after checking that it exists, you will also want to sanitize it.  Lots of people out there like exploiting forms, so it's always a good idea to sanitize your variables.  It is especially important to sanitize your variables if the entered data is going to end up being used in a sql query. 

In addition to all that, it is a good idea to have a method of sending custom error messages back to the form, if the entered data does not meet the requirements you laid out for it. 
Link to comment
Share on other sites

This is how I check for empty form fields.  If any of the fileds are blank a message is displayed asking for the user to go back in fill in all fields

[code]$empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";

$name = stripslashes($_POST['txtName']);
$message = stripslashes($_POST['txtMessage']);

if (!isset($_POST['txtName'])) {

?>
<form  method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

    <p>Please enter a title for the news article.
    <input style="width:400px;" type="text" title="Please enter a title for the news article" name="txtName" size="30"/></p>

    <p>Please enter the content for the news article.
    <textarea style="width:400px;" title="Please enter the content for the news article" name="txtMessage" rows="10" cols="30"></textarea></p>

    <p>Please select an image for the news article.
    <input style="width:400px;" type="file" name="photo" title="Please select an image for the news article" size="30"></p>

    <p>
    <input type="Submit" value="Submit"><input type="reset" value="Reset">

</form>
<?php

}

elseif (empty($name) || empty($message)) {

    echo $empty_fields_message;

}

else {

// do the rest of your code to insert into the database[/code]
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.