Jump to content

Conditional Form Based On User Input


gotmedia

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.