Jump to content


check if form is filled out

  • Please log in to reply
4 replies to this topic

#1 RTS

  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 27 August 2006 - 03:11 AM

how do I check if a textbox in a form is filled out?

#2 Aurorius

  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationMalaysia

Posted 27 August 2006 - 03:51 AM

// assume your textbox name is 'textbox' and you process the form using POST method

if (trim($_POST['textbox']) == '') {
       echo 'Textbox is empty.';
else {
       echo 'The textbox has been filled.';

#3 RTS

  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 27 August 2006 - 06:15 AM

okay, but then how do I make it so it wont send the data to my database if it is not filled?

#4 .josh

  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 27 August 2006 - 06:48 AM

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:

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

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. 
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 AdRock

  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 27 August 2006 - 08:53 AM

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

$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>

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



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

    echo $empty_fields_message;


else {

// do the rest of your code to insert into the database

If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users