• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

PHP getting checkbox to work in listed array

Hi still having problems which I thought were solved despite the efforts of EE help (due to my limited knowledge most likely)
Anyway I'm trying a different approach I'm listing names dynamically from database but iwant to put qty box to be filled by customer next to each name plus a chekcbox so that if chekbox ticked then the name and qty is submitted.eg

Checkbox            Name          Qty
                         (from db)

 x                      Apple              7
                          Pear
  x                     Banana          6

This is the code Im working with
Thanks in advance

<tr>
      <?php
            $i = 0
            while($bname = $imageList->fetch_assoc()) {
      ?>
     <tr><td><input type="text" name="bname_$i" value="<? echo $bname['bname']; ?>" disabled></td>
            <td><input type="text" name="price_$i" value="<? echo $bname['price']; ?>" disabled></td>
            <td><input type="text" name="qty_$i" value=""></td>
     </tr>
      <?php
            $i++;
            }
      ?>
</tr>
<tr>
0
LeighJor
Asked:
LeighJor
  • 7
  • 4
  • 3
2 Solutions
 
LeighJorAuthor Commented:
Oh bytheway I did try <input type="checkbox" name="bname_$i" value="<? echo $bname['bname']; ?>" >
that gave me the boxes but thats all- no data
0
 
nizsmoDeveloperCommented:
If you were to retrieve values of all the boxes that were ticked (their values) then you would need to do something like:

<input type="checkbox" id="someid" name="bname[]" value="<?php echo $bname['bname']; ?>" >



This would then submit through POST an array variable, and to get it:

$bnameArray = $_REQUEST['bname'];

And to get values out of the arrray:
foreach($bnameArray as $value)
      {
            echo $value;
                // Some more code here...
      }


Also it is always good practice to use <?PHP instead of just <? as PHP opening tag.

I also notice your attribute of the checkbox and textfields have name="bname_$i" or similar. I assume that $i is a php variable? If it is, then it is necessary to echo it like so:
name = "bname_<?php echo $i; ?>"

Please let me know how you go.
0
 
nizsmoDeveloperCommented:
Sorry forgot to mention, notice the square brackets added incase you were wondering about what was different.

<input type="checkbox" id="someid" name="bname[]" value="<?php echo $bname['bname']; ?>" >
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
nizsmoDeveloperCommented:
If you were wanting to retain the same variable name, the most ideal would be something like:

<input type="checkbox" id="someid" name="bname_<?php echo $i; ?>[]" value="<?php echo $bname['bname']; ?>" >

This also echos $i and makes it part of the checkbox name.
0
 
LeighJorAuthor Commented:
No I  cant get the $bname list up.
I can get vert checkboxes and qty input box but not the list from the db. Any thoughts?
appreciate your help
0
 
LeighJorAuthor Commented:
here is the code i've got so far

$getImages = 'SELECT * FROM journal ORDER BY bname';
$imageList = $conn->query($getImages) or die(mysqli_error($conn));
?>
<FORM ACTION="Email.php" method=post>
<table border=0>
<tr>
<td width=15>Name
</td>
<?php /*?><td ><input type="text" name="name" size="3" maxlength="3"></td><?php */?>

<td width=15>Quantity
</td>
<?php /*?><td ><input type="text" name="qty" size="3" maxlength="3"></td><?php */?>
</tr>
<tr>
<?php /*?><td>
<p>
      <label for="bname">BULBS:</label></td><?php */?>
   <td>
       
            <?php
                  while($bname = $imageList->fetch_assoc()){ ?>
               <tr><td><input type="checkbox" id="someid" name="bname[]" value="<?php echo $bname['bname']; ?>" ></td>
                    
     <td ><input type="text" name="qty" size="3" maxlength="3"></td></tr><?php }?>
   
</tr>
<tr>
<td colspan="2" align="center"><br>
<input type="submit" value="Enter Order"></td>
</tr>
</table>
</form>

</body>
</html>
0
 
nizsmoDeveloperCommented:
I'm not sure of your problem at the moment, are you saying that <?php echo $bname['bname']; ?> does not echo anything?
What exactly does your $bname variable store? And is it an array or a normal variable?
0
 
LeighJorAuthor Commented:
It does not echo anything at the moment
$bname should be echo of datadase column filled from 'SELECT * FROM journal ORDER BY bname'
0
 
LeighJorAuthor Commented:
i was using this before but it would not submit so needed a checkbox so that each checked row would be sent.  

<?php
            $i = 0;
            while($bname = $imageList->fetch_assoc()) {
      ?>
           <tr> <td><input type="text" name="bname_$i" value="<? echo $bname['bname']; ?>" disabled></td>
            <td><input type="text" name="price_$i" value="<? echo $bname['price']; ?>" disabled></td>
            <td><input type="text" name="qty_$i" value=""></td><tr>
      <?php
            $i++;
            }
      ?>
0
 
nplibCommented:
what I would do is have the

<tr>
      <?php
            $i = 0;
            while($bname = $imageList->fetch_assoc()) {
      ?>
     <tr>
            <td><input type="checkbox" name="cb[]" value ="<?=$i?>-<?=$bname['bname']?>-<?$bname['price']?>" />
            <td><input type="text" name="unused" value="<? echo $bname['bname']; ?>" disabled></td>
            <td><input type="text" name="unused" value="<? echo $bname['price']; ?>" disabled></td>
            <td><input type="text" name="qty[]" value=""></td>
     </tr>
      <?php
           $i++;
            }
      ?>
</tr>
<tr>
       <td colspan="4"><input type="submit" value="Go!" /></td>
</tr>

Then on the page that receives this data

$getCheckBoxData = $_POST['db'];
$getQTY = $_POST['qty'];

foreach ($getCheckBoxData as $x) {
       $temp = explode("-", $x);
       echo "The Price for ".$temp['1']." is ".$temp['2']." dollars and they want ".$getQTY[$temp['0']]." amount";
}
0
 
LeighJorAuthor Commented:
Sorry to take so long to get back to you nplib.
Thanki you very much that does enable me to display on page that "The Price for abracadabr is 20 dollars and they want 20 amount"
How do i get that into the sent message though.
Cheers and many thanks
Leighjor
0
 
nplibCommented:
I'm not sure what your asking for.

Can you give me an example?
0
 
LeighJorAuthor Commented:
How do I get the ouput of this :
foreach ($getCheckBoxData as $x) {
       $temp = explode("-", $x);
       echo "The Price for ".$temp['1']." is ".$temp['2']." dollars and they want ".$getQTY[$temp['0']]." amount";
}

into this:
$message = wordwrap($message, 70);
 Send
mail('caffinated@example.com', 'My Subject', $message);
0
 
nplibCommented:
you could do this, but I don't know the full structure of your code,
foreach ($getCheckBoxData as $x) {
       $temp = explode("-", $x);
       $str = "The Price for ".$temp['1']." is ".$temp['2']." dollars and they want ".$getQTY[$temp['0']]." amount";
       $message = wordwrap($str, 70);
       Sendmail('caffinated@example.com', 'My Subject', $message);
}

or
$tempArray = array();
foreach ($getCheckBoxData as $x) {
       $temp = explode("-", $x);
       array_push($tempArray, "The Price for ".$temp['1']." is ".$temp['2']." dollars and they want ".$getQTY[$temp['0']]." amount");
}

foreach ($tempArray as $t) {
     $message = wordwrap($t, 70);
     Sendmail('caffinated@example.com', 'My Subject', $message);
}
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 7
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now