gotmedia Posted May 20, 2011 Share Posted May 20, 2011 Hi there, I found a Javascript with what I want, but I want it to be in PHP because if people don't have Javascript enabled, they won't see the login. Here is what I have, but I need it to be converted to PHP: function loginArea() { val = document.loginForm.password.value; switch(val) { case "password1": document.location = 'http://www.google.com/password1-page/'; break; case "password2": document.location = 'http://www.google.com/password2-page/'; break; default: document.location ='http://www.google.com/sorry/'; break; } } <form name="loginForm" id="loginForm" method="post" action=""> <input name="password" type="text" id="password" maxlength="5" /> <input name="login" type="button" id="login" value="Check" onclick="loginArea()" /> </form> Help? - Steph Link to comment https://forums.phpfreaks.com/topic/236986-conditional-form-based-on-user-input/ Share on other sites More sharing options...
Psycho Posted May 20, 2011 Share Posted May 20, 2011 switch($_POST['password']) { case "password1": header("Location: http://www.google.com/password1-page/"); exit(); case "password2": header("Location: http://www.google.com/password2-page/"); exit(); default: header("Location: http://www.google.com/sorry/"); exit(); } Link to comment https://forums.phpfreaks.com/topic/236986-conditional-form-based-on-user-input/#findComment-1218131 Share on other sites More sharing options...
gotmedia Posted May 20, 2011 Author Share Posted May 20, 2011 Wow that was fast! I learned about 'header', but it sounded like its only use was for PHP includes. Guess not! I put that in and received this error: "unexpected '='". I'll look into it and research it, but if you can throw me a bone, that would be awesome. Updated code: <?php function loginArea() { val = document.loginForm.password.value; switch($_POST['password']) { case "password1": header("Location: http://www.google.com/password1-page/"); exit(); case "password2": header("Location: http://www.google.com/password2-page/"); exit(); default: header("Location: http://www.google.com/sorry/"); exit(); } } ?> <form name="loginForm" id="loginForm" method="post" action=""> <input name="password" type="text" id="password" maxlength="5" /> <input name="login" type="button" id="login" value="Check" onclick="loginArea()" /> </form> Link to comment https://forums.phpfreaks.com/topic/236986-conditional-form-based-on-user-input/#findComment-1218141 Share on other sites More sharing options...
wildteen88 Posted May 20, 2011 Share Posted May 20, 2011 You cannot place JavaScript code within PHP code. $_POST['password'] will get the text the user typed in the password field when the form has been submitted. Link to comment https://forums.phpfreaks.com/topic/236986-conditional-form-based-on-user-input/#findComment-1218143 Share on other sites More sharing options...
Psycho Posted May 20, 2011 Share Posted May 20, 2011 Remove this line val = document.loginForm.password.value; Although, you could have done this: $val = $_POST['password']; switch($val) { Link to comment https://forums.phpfreaks.com/topic/236986-conditional-form-based-on-user-input/#findComment-1218147 Share on other sites More sharing options...
gotmedia Posted May 20, 2011 Author Share Posted May 20, 2011 Sure, I just removed that line which gets rid of the errors and display a form. Thank you kindly! Now, when I click the submit button, it doesn't go anywhere. There must be something I'm missing. I have this in the PHP: switch($_POST['password']) { and this in the form: <input name="password" type="text" id="password" maxlength="9" /> . Is that what's linking those 2 together or is there something else I need? I thought it might have to do with the "action", but I'm not turning up any results. - Steph Link to comment https://forums.phpfreaks.com/topic/236986-conditional-form-based-on-user-input/#findComment-1218150 Share on other sites More sharing options...
Psycho Posted May 20, 2011 Share Posted May 20, 2011 If you leave the action parameter for a form empty, the form posts to itself. You can add the PHP code to the top of the form page (but only run it conditionally if the form was posted) or you can specify the PHP processing page in the action parameter. Link to comment https://forums.phpfreaks.com/topic/236986-conditional-form-based-on-user-input/#findComment-1218161 Share on other sites More sharing options...
gotmedia Posted May 20, 2011 Author Share Posted May 20, 2011 mjdamato, Okay, sure. I just put that in, but still not working. What did you mean by "PHP processing page in the action parameter"? See below for the code in my form.php file. Here is the link I'm referring to: http://notimeformorning.com/php/form.php <?php // Portal Password if ( 'POST' == $_SERVER['REQUEST_METHOD'] ) { function commPortal() { $val = $_POST['password']; switch($val) { case "pass1": header("Location: http://www.google.com/"); exit(); case "pass2": header("Location: http://www.yahoo.com/"); exit(); default: header("Location: http://www.404.com/"); } } } ?> <!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=utf-8" /> <title>Form</title> </head> <body> <form name="commportalForm" id="commportalForm" method="post" action=""> <input name="password" type="text" id="password" maxlength="5" /> <input name="login" type="button" id="login" value="Go" /> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/236986-conditional-form-based-on-user-input/#findComment-1218176 Share on other sites More sharing options...
wildteen88 Posted May 20, 2011 Share Posted May 20, 2011 Its not doing anything because you're not calling your commPortal() function. Link to comment https://forums.phpfreaks.com/topic/236986-conditional-form-based-on-user-input/#findComment-1218181 Share on other sites More sharing options...
Psycho Posted May 20, 2011 Share Posted May 20, 2011 You created a function, but never called the function. How do you expect that code to get executed? You don't need a function in this instance. <?php // Portal Password if (isset($_POST['password'])) { switch($_POST['password']) { case "pass1": header("Location: http://www.google.com/"); exit(); case "pass2": header("Location: http://www.yahoo.com/"); exit(); default: header("Location: http://www.404.com/"); exit(); } } ?> <!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=utf-8" /> <title>Form</title> </head> <body> <form name="commportalForm" id="commportalForm" method="post" action=""> <input name="password" type="password" id="password" maxlength="5" /> <input name="login" type="button" id="login" value="Go" /> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/236986-conditional-form-based-on-user-input/#findComment-1218185 Share on other sites More sharing options...
gotmedia Posted May 21, 2011 Author Share Posted May 21, 2011 OH, I see. Thank you for letting me know! That works when I hit enter on my keyboard. How to make it so that it works if I click "go" or "submit"? I'm researching that, but not turning up results. I tried adding this (isset($_POST['submit'])) but wherever I seem to put it, it doesn't work. Thoughts? <?php // Portal Password if (isset($_POST['password'])) { switch($_POST['password']) { case "pass1": header("Location: http://www.google.com/"); exit(); case "pass2": header("Location: http://www.yahoo.com/"); exit(); default: header("Location: http://www.404.com/"); exit(); } } ?> <!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=utf-8" /> <title>Form</title> </head> <body> <form name="commportalForm" id="commportalForm" method="post" action=""> <input name="password" type="text" id="password" maxlength="5" /> <input name="login" type="button" id="login" value="Go" /> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/236986-conditional-form-based-on-user-input/#findComment-1218445 Share on other sites More sharing options...
gotmedia Posted May 21, 2011 Author Share Posted May 21, 2011 Or would it have to do with this: if($_SERVER['REQUEST_METHOD'] == 'POST'){ ? Link to comment https://forums.phpfreaks.com/topic/236986-conditional-form-based-on-user-input/#findComment-1218447 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.