Jetfire Posted December 26, 2008 Share Posted December 26, 2008 I want to make it so if someone enters something in a form, and hits enter, if the message entered is "Example", then it would make a messagebox saying "Welcome Example!" Thanks Link to comment https://forums.phpfreaks.com/topic/138432-if-else-help-with-forms/ Share on other sites More sharing options...
carrotcake1029 Posted December 26, 2008 Share Posted December 26, 2008 Can you show us your form? Forms by nature send their data to another page (or it could be the same page). To check whatever they entered, depending on whether you used a POST or GET method, it would be something along the lines of this: <?php if ($_GET['boxname']) { //However you wanna make a messagebox //Access the name by $_GET['boxname'] } ?> edit: oops forgot it was brackets, not parentheses Link to comment https://forums.phpfreaks.com/topic/138432-if-else-help-with-forms/#findComment-723805 Share on other sites More sharing options...
Jetfire Posted December 26, 2008 Author Share Posted December 26, 2008 Sure, <form action="welcome.php" method="post"> Name: <input type="text" name="name" /> <input type="submit" /> </form> Then, <html> <body> Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?> years old. </body> </html> I'm trying to learn PHP right now at w3schools =] Link to comment https://forums.phpfreaks.com/topic/138432-if-else-help-with-forms/#findComment-723807 Share on other sites More sharing options...
carrotcake1029 Posted December 26, 2008 Share Posted December 26, 2008 index.html <form action="welcome.php" method="post"> Name: <input type="text" name="name" /><br /> Age: <input type="text" name="age" /><br /> <input type="submit" /> </form> welcome.php <?php if ($_POST['name'] && $_POST['age']) { echo "Welcome " . $_POST['name'] . "<br />"; echo "You are " . $_POST['age'] . " years old."; } ?> Link to comment https://forums.phpfreaks.com/topic/138432-if-else-help-with-forms/#findComment-723810 Share on other sites More sharing options...
Jetfire Posted December 26, 2008 Author Share Posted December 26, 2008 index.html <form action="welcome.php" method="post"> Name: <input type="text" name="name" /><br /> Age: <input type="text" name="age" /><br /> <input type="submit" /> </form> welcome.php <?php if ($_POST['name'] && $_POST['age']) { echo "Welcome " . $_POST['name'] . "<br />"; echo "You are " . $_POST['age'] . "years old."; } ?> So in welcome.php to make it detect if a certain string is entered, like for example 99999 in age, how would I stop the form, and a messagebox saying "That is not possible". Thanks for your help =] Link to comment https://forums.phpfreaks.com/topic/138432-if-else-help-with-forms/#findComment-723814 Share on other sites More sharing options...
carrotcake1029 Posted December 26, 2008 Share Posted December 26, 2008 Well, with php, you wouldn't be able to tell what the user entered until it went to the next page. If you want to detect it immediately, you would have to use vbscript/javascript. Link to comment https://forums.phpfreaks.com/topic/138432-if-else-help-with-forms/#findComment-723815 Share on other sites More sharing options...
Jetfire Posted December 26, 2008 Author Share Posted December 26, 2008 Well, with php, you wouldn't be able to tell what the user entered until it went to the next page. If you want to detect it immediately, you would have to use vbscript/javascript. Oh, guess I'd have to go to a javascript/vbscript help forum now Link to comment https://forums.phpfreaks.com/topic/138432-if-else-help-with-forms/#findComment-723818 Share on other sites More sharing options...
Jetfire Posted December 26, 2008 Author Share Posted December 26, 2008 Oh wait, what if it detects it on the second page and make them enter it again? Link to comment https://forums.phpfreaks.com/topic/138432-if-else-help-with-forms/#findComment-723967 Share on other sites More sharing options...
Jetfire Posted December 27, 2008 Author Share Posted December 27, 2008 Bumpp Link to comment https://forums.phpfreaks.com/topic/138432-if-else-help-with-forms/#findComment-724274 Share on other sites More sharing options...
hobeau Posted December 27, 2008 Share Posted December 27, 2008 Here is some sample code that may help you: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Test</title> <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { $message = $_POST['message']; ?> <script type="text/javascript"> window.onload = function(){ var message = '<?= $message ?>'; if (message != '') { alert(message); } } </script> <?php } ?> </head> <body> <fieldset> <legend>Show Message</legend> <form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>"> <dl> <dt><label for"message">Message:</label></dt> <dd><input type="text" maxlength="50" id="message" name="message" /></dd> <dt> </dt> <dd><input type="submit" value="Show Messasge" /></dd> </dl> </form> </fieldset> </body> </html> So lets walk through the anatomy of this script. First, we have our form that defines a method type of 'POST' (which sends all data through the HTTP request as opposed to sending variables through the address bar). The form also has an action of <?= $_SERVER['PHP_SELF'] ?>. This is what is known as a 'Post back' as the page posts the data back to itself. Next - at the top of the page - we have some php code that utilizes the $_SERVER['REQUEST_METHOD']. The default method your browser sends to the web server is 'GET'. When you click the submit button on an HTML form that has the method of 'POST' the http request method then is 'POST' and the data is sent through the http request. $_SERVER['REQUEST_METHOD'] lets us know if the request was a POST method,, in which case the form has been posted. Then we use the $_POST['message'] global variable to get the message that was posted from the form. Note in the form, the <input textbox defines both the name and the id. HTML forms as of now send the data as defined by the 'name' property in the <input tags. Javascript typically uses the 'id' property so we make sure to define both. If the request method is post, we set the value of $message. Then we inject some php into our javascript using <?= which is the equivalent of <?php echo ... ?>. Then javascript uses the window.onload method and is assigned an anonymous function (anonymous functions are functions without names,,, this is currently not available in php, but is included in PHP6) that does the work of showing the message box. whew,, hope that helps!!! Link to comment https://forums.phpfreaks.com/topic/138432-if-else-help-with-forms/#findComment-724293 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.