Jump to content

If, Else, Help with forms


Jetfire

Recommended Posts

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

 

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 =]

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.";
    }
?>

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 =]

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!!! ;D

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.