RTS Posted August 27, 2006 Share Posted August 27, 2006 how do I check if a textbox in a form is filled out? Quote Link to comment Share on other sites More sharing options...
Aurorius Posted August 27, 2006 Share Posted August 27, 2006 // assume your textbox name is 'textbox' and you process the form using POST method[code]<?phpif (trim($_POST['textbox']) == '') { echo 'Textbox is empty.';}else { echo 'The textbox has been filled.';}?>[/code] Quote Link to comment Share on other sites More sharing options...
RTS Posted August 27, 2006 Author Share Posted August 27, 2006 okay, but then how do I make it so it wont send the data to my database if it is not filled? Quote Link to comment Share on other sites More sharing options...
.josh Posted August 27, 2006 Share Posted August 27, 2006 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. Quote Link to comment Share on other sites More sharing options...
AdRock Posted August 27, 2006 Share Posted August 27, 2006 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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.