Page 1 of 1

Sending FORM action to a PHP script

Posted: Wed Mar 23, 2005 9:12 am
by irishmike2004
Hello:

This seems rather a simplistic question, however, it would appear that I am still not sure how to do this :-)

I need to take the input of a web form and put the result into variables:

for example: I want to take a webform that asks two questions, 1 zipcode and 2 radio button selection for metric or standard and have the action return what the user types into a 2 variables.

Also I would like to have this puppy have a default situation before the form is even entered.

any help is appreciated.

Posted: Wed Mar 23, 2005 9:36 am
by feyd
simply pointing the action to a php script will give you all the data they entered. As for the "default situation," you can simply fill the form fields with default bits of data if you like..

Posted: Wed Mar 23, 2005 10:37 am
by s.dot

Code: Select all

<form action=&quote;code.php&quote; method=&quote;post&quote;>
would point your form towards your php script, automatically turning your forum input into variables such as:

Code: Select all

$_POST['zip']
$_POST['radiobutton']
As for defaults

for the zip code

Code: Select all

<input type=&quote;text&quote; name=&quote;zip&quote; value=&quote;12345&quote;>
Standard <input type=&quote;radio&quote; name=&quote;standardormetric&quote; value=&quote;standard&quote; checked>
Metric <input type=&quote;radio&quote; name=&quote;standardormetric&quote; value=&quote;metric&quote;>
This would give you the default zip of 12345 and default radio value of standard.

Posted: Wed Mar 23, 2005 11:26 am
by php_wiz_kid
Here's the XHTML form:

Code: Select all

<form method=&quote;post&quote; action=&quote;validateform.php&quote;>
<input type=&quote;text&quote; name=&quote;zipcode&quote; />
<input type=&quote;radio&quote; name=&quote;radioselect&quote; value=&quote;1&quote; checked=&quote;checked&quote; />
<input type=&quote;radio&quote; name=&quote;radioselect&quote; value=&quote;0&quote; />
</form>
Here's the PHP code to retrieve the form objects:

Code: Select all

/*-This will retrieve the forms zipcode textbox.-*/
$ZipCode = $_POST['zipcode'];

/*-This will retrieve the users radioselect selection (either 1 or 0)-*/
$RadioSelect = $_POST['radioselect']
Note that you should use the $_POST and $_GET global variables according to the forms method (either post or get).

as for defaults you could do something like this:

Code: Select all

<?php
if(empty($_POST['zipcode'])) {
  $ZipCode = "Please enter your zip code.";
}

/*-Please note that if an option was selected that the radio buttons WILL NOT appear.
 * you would need to extend the if-statement.  This is only to show how to do a 
 * default value.-*/
if(empty($_POST['radioselect'])) {
  $RadioSelect = 
    "<input type="radio" name="radioselect" value="1" checked="checked" />".
    "<input type="radio" name="radioselect" value="0" />";
}
?>
<form method="post" action="validateform.php">
<input type="text" name="zipcode" value="<?php echo $ZipCode; ?>" />
<?php echo $RadioSelect; ?>
</form>

Posted: Wed Mar 23, 2005 11:29 am
by John Cartwright

Code: Select all

<?php
if(empty($_POST['zipcode'])) {
  $ZipCode = "Please enter your zip code.";
}

/*-Please note that if an option was selected that the radio buttons WILL NOT appear.
 * you would need to extend the if-statement.  This is only to show how to do a 
 * default value.-*/
if(empty($_POST['radioselect'])) {
  $RadioSelect = 
    '<input type="radio" name="radioselect" value="1" checked="checked" />
     <input type="radio" name="radioselect" value="0" />';
}
?>
<form method="post" action="validateform.php">
<input type="text" name="zipcode" value="<?php echo $ZipCode; ?>" />
<?php echo $RadioSelect; ?>
</form>
fixed parse error ;)

Posted: Wed Mar 23, 2005 11:40 am
by php_wiz_kid
Oops, should've put the /" escape character, thanks.