Sending FORM action to a PHP script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Sending FORM action to a PHP script

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post 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>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 ;)
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post by php_wiz_kid »

Oops, should've put the /" escape character, thanks.
Post Reply