1

I hope to succeed to explain what is the problem. I have this form:

<form class="p_form" name="form1" method="POST" action="./index.php?product=3&amp;pn=1">    
<div class="poptionholder">    
<div class="poption"><input type="checkbox" value="34" id="o34" name="options[]">
<label for="o34">Red</label></div>    
<input type="text" value="" id="p34" name="opt_price[]"></div>

<div class="poptionholder">    
<div class="poption"><input type="checkbox" value="35" id="o35" name="options[]">
<label for="o35">Green</label></div>    
<input type="text" value="" id="p35" name="opt_price[]"></div>

<div class="poptionholder">    
<div class="poption"><input type="checkbox" value="36" id="o36" name="options[]">
<label for="o36">Yellow</label></div>
<input type="text" value="" id="p36" name="opt_price[]"></div>

<div class="poptionholder">    
<div class="poption"><input type="checkbox" value="37" id="o37" name="options[]">
<label for="o37">Orange</label></div>
<input type="text" value="12" id="p37" name="opt_price[]"></div>

<div class="poptionholder">    
<div class="poption"><input type="checkbox" value="38" id="o38" name="options[]">
<label for="o38">Blue</label></div>
<input type="text" value="" id="p38" name="opt_price[]"></div>

<div class="poptionholder">    
<div class="poption"><input type="checkbox" value="39" id="o39" name="options[]">
<label for="o39">Pink</label></div>
<input type="text" value="" id="p39" name="opt_price[]"></div>

    <input type="submit" value="Send"/>
</form>    

and the following php code:

if(isset($_POST['options'])){
$options = $_POST['options'];
$opt_price = $_POST['opt_price'];
foreach( $options as $key => $n ) {
$optid=$n;
$price=$opt_price[$key];
}    
}    

Basicaly this form represents checkbox with color label and input text where user to add price. the problem is that when I submit the form the checkbox posts data only if checked, but input text posts data in all cases - with or without price in the input. When I print posted data I can see:

array(3) { [0]=> string(2) "37" [1]=> string(2) "38" [2]=> string(2) "39" } - only 3 checkboxes are checked 
array(6) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(2) "12" [5]=> string(0) "" } - all 6 input texts..

I want to get data only for checked checkboxes and their prices like group - checkbox + its price ..How to do so? Thanks in advance

2 Answers 2

1

Don't use opt_price[] as name, just use the id as name and then use $price = $_POST['p'.$key]; like this:

<div class="poptionholder">    
<div class="poption"><input type="checkbox" value="35" id="o35" name="options[]">
<label for="o35">Green</label></div>    
<input type="text" value="" id="p35" name="p35"></div>

if(isset($_POST['options'])){
    $options = $_POST['options'];    
    foreach( $options as $key => $n ) {
       $optid=$n;
       $price=$_POST['p'.$n];
    }    
}    
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, Ragnar. looks like it will work but there is a problem with $_POST['p'.$key] - when I print $_POST['p37'] I can see the price, but with $_POST['p'.$key] when the key is 37 -it is empty , why is that?
Maybe the textbox doesn't has the correct name "p37"
Ragnar maybe you mean $_POST['p'.$n]; instead of $_POST['p'.$key]; because this way it works and I accept your answer, thank you !
1

Your fields seem to have id's (34 - 39) so you could use these to make sure the fields always match:

<div class="poptionholder">    
<div class="poption"><input type="checkbox" value="34" id="o34" name="options[34]">
                                                                              ^^ here
<label for="o34">Red</label></div>    
<input type="text" value="" id="p34" name="opt_price[34]"></div>
                                                     ^^ here

etc.

Now you can loop over the set checkboxes and using the key you can access the correct text field:

foreach( $options as $key => $n ) {
  $price=$opt_price[$key];
}

You could of course also loop over array_keys($options) as you don't use the value $n.

Comments

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.