0

I have asked this before here, but I'm not sure, if the question was clear enough, or right. So now I ask again, the solution I have is not 100% right!

I have a form and I submit it, there is a table in it. Each line of the table has a checkbox that you can check for ordering a product, a select field for a charge for the product and of course the price (in a select field, so I have a value for the price).

The goal to achieve is to have blocks in the mail, like this (below):

product: greek salad xl
quantity: 4pcs.
price: 5.50$

product: tomatos
quantity: 8pcs.
price: 2.50$

....

so I have this, but the price is always taken from the first element, so, if I chose the second and third product: the price is taken from the first. The quantity is taken from the first - it is shifted one.

What can I do?

This is the (simple) foreach PHP setup that I have tried (and many other variations):

$product = $_POST['bestellung']; --> this is the checkbox that is  correct!
$quantity = $_POST['menge']; --> always shifted...
$price = $_POST['liq_preis']; --> always shifted...

foreach( $product as $key => $item ) {
$bestell_tab .= "\n\nProdukt: $item  \n\nStueckzahl: ".$quantity[$key]." \n\nPreis: ".$price[$key].";
}

(the $bestell_tab I put in the PHP mail for submit ...)

If somebody can help a php-greenhorn with a "simple" solution, it would be great!

Here HTML Markup:

<select class="kat-preis" name="liq_preis[]"><option value="10.00">10.00</option></select>
<select class="1-100" name="menge[]"><option value="1">1</option><option value="2">2</option></select>
<input type="checkbox" name="bestellung[]" value="expl-tomatos">

Edit

array(14) {
["tablepress-23_length"]=>
string(2) "10"
["menge"]=>
array(6) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "2"
[3]=>
string(1) "2"
[4]=>
string(1) "2"
[5]=>
string(1) "2"
}
["liq_preis"]=>
array(6) {
[0]=>
string(4) "1.00"
[1]=>
string(4) "2.00"
[2]=>
string(4) "3.00"
[3]=>
string(4) "4.00"
[4]=>
string(4) "5.00"
[5]=>
string(4) "6.00"
}
["bestellung"]=>
array(5) {
[0]=>
string(9) "tomatos109"
[1]=>
string(9) "tomatos111"
[2]=>
string(9) "tomatos116"
[3]=>
string(9) "tomatos118"
[4]=>
string(9) "tomatos209"
}

In this, I have clicked on the 2nd Product. The first is empty, but the value isn't right. It is only for the product name "bestellung" right

Edit

<td class="column-1">51234</td><td class="column-2">goodchoice</td><td class="column-3">tomatos from Spain</td><td class="column-4"><select class="kat-preis" name="liq_preis[]"><option value="1.00">10.00</option></select></td><td class="column-5">1.00 CHF</td><td class="column-6"><select class="1-100" name="menge[]"><option value="1">1</option><option value="2">2</option></select></td><td class="column-7"><input type="checkbox" name="bestellung[]" value="51234"> </td>
11
  • 1
    what does your html look like Commented Aug 4, 2014 at 15:09
  • it is a normal form that is working... (in it is a table, generated with the tablepress plugin 4 wordpress) at the end, it is a normal table in a form, thats it. you think is a html thing? Commented Aug 4, 2014 at 15:20
  • Yes it could be for example you need to make sure you have brackets [] on your name attribute in order to send arrays to the server Commented Aug 4, 2014 at 15:36
  • yes, brackets i have, see my edit Commented Aug 4, 2014 at 16:12
  • Can you update with a var_dump($_POST); ? Commented Aug 4, 2014 at 16:25

1 Answer 1

1

The php code becomes:

product = $_POST['bestellung']; --> this is the checkbox that is  correct!
$quantity = $_POST['menge']; --> allways shifted...
$price = $_POST['liq_preis']; --> allways shifted...

foreach( $product as $item ) {
$bestell_tab .= "\n\nProdukt: ".$item."  \n\n
Stueckzahl: ".$quantity[$item]." \n\n
Preis: ".$price[$item];//here there was an error in original code - an extra "   
}

For the above to work, you must put the value of the bestsellung in the array of the other two. How will the user know it is tomatoes though? Anyhow, the html becomes:

<table>
<td class="column-1">51234</td>
<td class="column-2">goodchoice</td>
<td class="column-3">tomatos from Spain</td>
<td class="column-4">
    <select class="kat-preis" name="liq_preis[51234]">
        <option value="1.00">10.00</option>
    </select>
</td>
<td class="column-5">1.00 CHF</td>
<td class="column-6">
    <select class="1-100" name="menge[51234]">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
</td>
<td class="column-7">
    <input type="checkbox" name="bestellung[]" value="51234"> 
</td>
</table>
Sign up to request clarification or add additional context in comments.

4 Comments

thanks man, it seems to work. - for me the solution is that what i searched, but just for better understanding: there was a other solution for this? - so that i d'ont have to fill out over 100 values in my table? ($product as $key => $item) but i will try next time something to learn more. again: thx !
the point is, i d'ont can bring a empty line after each block, for better reading.
Well, the "better" way is to load the data with a loop from a database. That way you do it once and let the program do the 100 times.
if you dont want an empty line, remove one \n

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.