简体   繁体   中英

PHP - form $_POST data array doesn't return the needed information

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

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];
    }    
}    

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM