Get latest news and Web Designing tutoria

Friday, 29 May 2015

PHP Submit buttons

In the previous lesson you saw how to get text from a textbox when a Submit button on a form was clicked. However, when you first load the page the text still displays.
The reason why the text displays when the page is first loaded is because the script executes whether the button is clicked or not. This is the problem you face when a PHP script is on the same page as the HTML, and is being submitted to itself in the ACTION attribute.

To get round this, you can do a simple check using another IF Statement. What you do is to check if the Submit button was clicked. If it was, then run your code. To check if a submit button was clicked, use this:
if ( isset( $_POST['Submit1'] ) ) { }
Now that looks a bit messy! But it actually consists of three parts:
if ( ) { }
isset( )
$_POST['Submit1']
You know about the if statement. But in between the round brackets, we have isset( ). This is an inbuilt function that checks if a variable has been set or not. In between the round brackets, you type what you want isset( ) to check. For us, this is $_POST['Submit']. If the user just refreshed the page, then no value will be set for the Submit button. If the user did click the Submit button, then PHP will automatically return a value. Change you script from the previous lesson to the following and try it out:
if (isset($_POST['Submit1'])) {
$username = $_POST['username'];
if ($username == "letmein") {
print ("Welcome back, friend!");
}
else {
print ("You're not a member of this site");
}
}
Make a note of where all those messy round, square and curly brackets are. Miss one out and you'll get an error!

Related Posts:

  • PHP and HTML Radio Buttons Make sure you save your work as radioButton.php, as that's where we're posting the Form – to itself. To get the value of a radio button with PHP code, again you access the NAME attribute of the HTML form elements.… Read More
  • PHP and HTML Checkboxes Note one thing about the HTML checkbox elements: they all have different NAME values (ch1, ch2 ch3, etc). When we coded for the Radio Buttons, we gave the buttons the same NAME. That's because only one option can be se… Read More
  • PHP Submit buttons In the previous lesson you saw how to get text from a textbox when a Submit button on a form was clicked. However, when you first load the page the text still displays. The reason why the text displays when the page i… Read More
  • The HTML ACTION attribute and PHP You don't have to submit your form data to the same PHP page, as we've been doing. You can send it to an entirely different PHP page. To see how it works, try this: Create the following page, and call it basicForm2.ph… Read More
  • PHP and Text Boxes on HTML Forms If you've been following along from the in the previous lesson then your basicForm.php now has a METHOD and ACTION set. We're going to use these to process text that a user has entered into a text box. The MET… Read More

0 comments:

Post a Comment