Jump to content

html form, can i $_POST multiple variables?


Go to solution Solved by mac_gyver,

Recommended Posts

<form action="shop1b.php" method="post">
Buy this many archers: <input type="text" name="unit1">
<input type="submit">
</form>

<form action="shop1b.php" method="post">
Buy this many shield men: <input type="text" name="unit2">
<input type="submit">
</form>

I am using two forms, but i wanted to use about 20.

 

Then they go to shop1b.php

<?php
$buy = $_POST["unit1"];
if ($buy == "1")
{
echo $_POST["unit1"];
echo $buy;
}
else exit("Please use only numbers.");

$buy = $_POST["unit2"];
if ($buy == "5")
{
echo $_POST["unit2"];
echo $buy;
}
else exit("Please use only numbers.");
?>

The goal of this is on the first page, with html, you put in the text variable, then in the next page, the php calculates how many units and what type of unit you want to build.

 

"unit2" ends up as not being defined, and i don't know how to work around that.

 

the php seems to need "unit1" and "unit2" to both be defined.

 

 

I am trying my first steps at making a shop for a php game.

If you can imagine that, maybe you will be able to see what I am struggling to accomplish.

<form action="shop1b.php" method="post">
Option number placed here: <input type="text" <input name="unit">
<input type="submit">
</form>

<br />

Select a number, which ever number you select causes you to buy a certain amount of a certain unit.
<br />
<br />
1 = buy 1 archer. - 100 gold <br />
2 = buy 5 archers. - 500 gold <br />
3 = buy 50 archers. -5000 gold <br />
<br />
4 = buy 1 shield man. - 100 gold <br />
5 = buy 5 shield men. - 500 gold <br />
6 = buy 50 shield men. -5000 gold <br />

this is the first html page

<?php
$buy = $_POST["unit"];

if ($buy == "1")
{
echo "you bought 1 archer";
}

if ($buy == "2")
{
echo "you bought 5 archers";
}

if ($buy == "3")
{
echo "you bought 50 archers";
}
?>

and that is the php page which the single number is $_POST ed to.

 

I had hoped that you could $_POST more than one number or string in a single form. Maybe you can and i dont know. But, the thing I posted just now works for me. I had to do some thinking.

  • Solution

you seem to be implying you want to be able to select multiple items at one time? you would make one form with a way of selecting/entering each choice within that one form. it's also not clear if you want to allow any quantity to be entered or just the 1,5,50 choices (you need to define this before you write any code since that determines what sort of user interface you make.)

 

you should also not expect the visitor to read your list and type in a number to select anything. provide him/her with some sort of menu (select, checkbox, radiobutton) with the possible choices already listed and all they need to do is pick the choice(s) they want and then submit the one form.

 

you should also not hard-code each possible selection and hard-code the tests for the values. make a data driven design, where you define the choices (in an array or a database table). use this definition for dynamically producing the user interface and for validating the submitted data. to add, subtract, or alter any of the choices, all you do is change the data definition. you don't touch your code.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.