Get latest news and Web Designing tutoria

Saturday, 30 May 2015

Check for blank Textboxes with PHP

f you remember the script that we wanted to create earlier it was this:
  1. Get the text that a user entered in a textbox on a form
  2. Trim any blank spaces from the left and right of the text
  3. Check that what you have left is not a blank string
So we want to check that the textbox doesn't just contain this "". There has to be something in it, like "Bill Gates". Here's a script that does all three items on our list:
<?PHP

$user_text = trim("Bill Gates");
display_error_message($user_text);
function display_error_message($user_text) {
if ($user_text == "") {
print "Blank text box detected";
}
else {
print "Text OK";
}
}
?>
Try it out. When you run the script, you should find that Text OK prints. Now change this line:
$user_text = trim("Bill Gates");
to this:
$user_text = trim("");
Run your script again. This time, Blank text box detected should print out. Obviously, we're not getting the text from a textbox on a form, but just simulating the process. If you want to try out a version with all the HTML, here it is. This next script checks two textboxes on a form.

Related Posts:

  • Getting values out of PHP functions When you're creating your own functions, you may notice that they can be broken down in to two categories: functions that you can leave, and just let them do their jobs; and functions where you need to get an answer ba… Read More
  • PHP Functions and Arguments Arguments Functions can be handed variables, so that you can do something with what's inside of them. You pass the variable over to your functions by typing them inside of the round brackets of the function name… Read More
  • Check for blank Textboxes with PHP f you remember the script that we wanted to create earlier it was this: Get the text that a user entered in a textbox on a form Trim any blank spaces from the left and right of the text Check that what you have left i… Read More
  • PHP Functions What is a Function? A function is just a segment of code, separate from the rest of your code. You separate it because it's nice and handy, and you want to use it not once but over and over. It's a chunk of code that … Read More
  • Sorting PHP Array values There may be times when you want to sort the values inside of an array. For example, suppose your array values are not in alphabetical order. Like this one: $full_name = array(); $full_name["Roger"] = "Wate… Read More

0 comments:

Post a Comment