PHP form submission

Associate
Joined
19 Jul 2006
Posts
258
Location
Gibraltar
Hi guys! :)

Hope someone can help with an issue I'm having. I'm an ok developer when it comes to HTML/CSS, but php forms have me a little confused.

This is a simple form I've got at the moment:

Code:
<form id="contactform" method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<h4 class="productCategory">First Category</h4>
         <div>
                <input class="quantity" type=text name="product1"> 
                 <label for="product1">Product1</label>
         </div>                    
<h4 class="productCategory">Second Category</h4>
         <div>
                <input class="quantity" type=text name="product2">
                <label for="product2">Product2</label>
         </div>
         <div><input type="submit" value="Submit" name="cfsubmit" id="cfsubmit" /></div>
</form>

I won't be getting a shopping card for a few months yet, so in the meantime since there aren't a great deal of products I'd like to get a simple order form up.

So my problem is, when it comes to posting data, I don't know how to do the following:

If someone enters a value into the input quantity fields I'd like that to be picked up in POST, my issue is I also want the corresponding label for that quantity to be mailed also with it's quantity. So the email received will be something like this:

2 x Build Muscle 1
4 x Pre Workout Boosters

I believe getting the quantity is just a case of $product1 = $_POST['product1'], but I just don't know how to get it to bring along its label.

Hope I'm making some sense...

Thanks! :)
 
PHP:
$product1 = $_POST['product1']."x Product 1";

You can append and prepend stuff by using a . then quote text

PHP:
$a = "Jenny";
$b = "Tom";

$string = $a." said hello to ".$b.", but ".$b." didn't say hello back!";
 
Last edited:
Hi Fabieno,

Wow that simple! Thanks. Just one more question.

Say for example that a user hasn't entered any value for a specific input field as they don't want any of the product. Is there anyway to check this so that the value isn't submitted? As in only post values for those inputs that have a value?

Thanks very much for your help

EDIT - would something like

if (isset($_POST['product1'])) {
$product1 = $_POST['product1']."x Product 1";
}

Or is there a better way?
 
Last edited:
If you want to cover all the eventualities, you need to check 3 things - Have they submitted something, is it actually a number and is it greater than 0?

This would look something like
Code:
if (isset($_POST['product1'])){
	if!(is_numeric($_POST['product1'])) 
	{
	echo 'Please enter a number';
	}
	else
	{

		if!($_POST['product1'] <= '1') {
		echo 'The quantity should be 1 or more';	
		}
		else
		{
		$product1 = $_POST['product1']."x Product 1";
		}

	}
}

The reason I don't put all the ifs into a single query is that it can generate warnings if the user enters text rather than a number, or doesn't enter anything.

Also, I just threw that together in the post box, I haven't tested it.
 
Hi Lilnkex,

That makes sense! Thanks so much for taking the time to write that up.
If I could ask one more thing....when it gets to like 10-15 products it'll be a little inconvenient writing them all up, is there anyway to perform a loop like

for ($i=0; $i<numberofinputfields; $i++ {
if (isset($_POST['product1'])){
if!(is_numeric($_POST['product1']))
{
echo 'Please enter a number';
}
else
{
if!($_POST['product1'] <= '1') {
echo 'The quantity should be 1 or more';
}
else
{
$product1 = $_POST['product1']."x Product 1";
}

}
}
}

But instead of using isset($_POST['product1'] I use isset($_POST['product$i'] Can $i be used in that way there?

Thanks again
 
Last edited:
You want the foreach command for that, like so:

Code:
foreach($_POST as $product => $qty){
	if!(is_numeric($qty)) 
	{
	echo 'Please enter a number';
	}
	else
	{

		if!($qty <= '1') {
		echo 'The quantity should be 1 or more';	
		}
		else
		{
		$product1 = $qty . "x " . $product ";
		}

	}
}

What the foreach does is run through every thing in POST (php treats POST and GET as an array) one key at a time, assigns the product name to $product and the quantity to $qty and does whatever you tell it to.

Where it gets tricky is if you have any other data sent from your form, because it will try and process everything sent via post. So, if you have a field called "firstname", you would get output like "Firefly86 x firstname". If you are intending to send other data through the form, you will need to change your data to a multidimensional array. To do this, first you edit your form:

Code:
<input class="quantity" type=text name="product1">
to
<input class="quantity" type=text name="product[product1]">

What this does is create an array within and array, so that it has 2 parts to identify which bit of the array you want. So, for example, where product 1 was previously $_POST['product1'], it would now be $_POST['product']['product1'].

Then in the php, change the line:

Code:
foreach($_POST as $product => $qty){
to
foreach($_POST[product] as $product => $qty){

What this does is make the "foreach" only run through the part of the array that was created with the [product] part on it

I'm not sure I've explained that particularly well, so feel free to post any questions you have and I'll try to clarify it.
 
thats fantastic Linkex you're a star! I think using multidimensional array as you've mentioned will sort out my issue. I'll let you know if i have any issues but I reckon you've hit the nail on the head for me.

Many thanks!
 
tis me again!
I've run into another issue, I've tried to apply the methods above to this but can't seem to get my head around it.

<div>
<input class="quantity" type=text name="product[ProductNameHere]">
<input class="radio" type="radio" name="flavour[Banana]" value="Banana"> Banana
<input class="radio" type="radio" name="flavour[Berry]" value="Berry"> Berry
<input class="radio" type="radio" name="flavour[Chocolate]" value="Chocolate"> Chocolate
</div>

I'd like to be able to add the radio options above to select flavours after the user enters quantity. I don't know how to get the posted flavour to carry across with its corresponding input fields instead of as its own independant post. Hope that makes sense...

Anyone got any ideas? :(
 
Ok, the first thing is that all the radio buttons should have the same "name" tag. You then set the specifics in the value field. So you would want something like:

Code:
<div>
<input class="quantity" type=text name="product[ProductNameHere]">
<input class="radio" type="radio" name="[product]flavour" value="Banana"> Banana
<input class="radio" type="radio" name="[product]flavour" value="Berry"> Berry
<input class="radio" type="radio" name="[product]flavour" value="Chocolate"> Chocolate
</div>

This way, when you run through the array using your foreach, your "product" array will now contain the key "flavour" which will be set to whatever value they selected. The only problem is if you have multiple items with the same flavour options, as there is no way to tell with this method which item goes with which flavour.

To carry the flavour over, you would need to give each flavour field a unique name, so lets assume that your product was "milkshake", your code would look like this:

Code:
<div>
<input class="quantity" type=text name="product[milkshake]">
<input class="radio" type="radio" name="[product]milkshake-flavour" value="Banana"> Banana
<input class="radio" type="radio" name="[product]milkshake-flavour" value="Berry"> Berry
<input class="radio" type="radio" name="[product]milkshake-flavour" value="Chocolate"> Chocolate
</div>

Then, when you pull out these items in your foreach loop, the relevent name would be attached. But that will get complicated, fast.

To be honest, if your talking about this level of complexity, you would be better off installing a shopping cart - they are simpler to set up than what you are trying to do, and there are a number of good, free ones out there. I'd be happy to talk you through setting one up if you like.
 
Thanks for getting back to me Linkex.
Think you might be right :/ Think this is going beyond the comfortable realm of php forms now lol.
If you don't mind I would actually really appreciate your help with a shopping cart.

Thanks!
 
Back
Top Bottom