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! :)
 
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:
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:
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? :(
 
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