-2

I have a dynamic checkbox that is created from the database. The textbox are for quantity here is a sample code:

$query="SELECT name,price FROM product";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$name=mysql_result($result,$i,"name");
$price=mysql_result($result,$i,"price");

echo "<form name='waz' action='calculate2.php' method='post'>
<br>
<input type='checkbox' name='$name' value='$name; '/> $name
<input type='text' name='$name'/>
<br>";$i++;
} echo "<input type='submit' value='calculate'/>"; ?><br>

How can i capture the textbox and the checkbox values on the calculate2.php page and save each checkbox and textbox values as different variable. Help please

user3342746
  • 13
  • 1
  • 4
  • `$_POST['xyz']`, where `xyz` is whatever the value of `$name` is. This is really basic stuff... – elixenide Feb 23 '14 at 07:45
  • THE checkbox wiil be shown in $_POST variables if it is true (checked) – JohnTaa Feb 23 '14 at 07:46
  • You needn't have repeating `form` tags. Put the `form` tag out of the loop and look up on `input arrays` - they allow you to save multiple values with same name (in form of array). – AyB Feb 23 '14 at 07:46
  • add the output of `print_r($_POST);` in the top of your calculate2.php page – Awlad Liton Feb 23 '14 at 07:48
  • But guys, i have several values being generated and for each value a checkbox is created. i need to save those values on different variable so as to be able to calculate qty*price latter on. – user3342746 Feb 23 '14 at 07:51
  • @user3342746 that's why I said look up on input arrays, stop using counters, they just make it difficult to retrieve values. – AyB Feb 23 '14 at 07:54
  • Please do a Google search before asking questions. Most answers can be easily found. Possible duplicate of [How to read if a checkbox is checked in PHP?](http://stackoverflow.com/questions/4554758/how-to-read-if-a-checkbox-is-checked-in-php) – Matt Clark Feb 23 '14 at 08:06

4 Answers4

0

try this

$query="SELECT name,price FROM product";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$name=mysql_result($result,$i,"name");
$price=mysql_result($result,$i,"price");

echo "<form name='waz' action='calculate2.php' method='post'>
<br>
<input type='checkbox' name='check$i' value='$name; '/> $name
<input type='text' name='text$i'/>
<br>";$i++;
} 
echo "<input type='submit' value='calculate'/>";
echo "<input type='hidden' value='$i' name='count'/>";

   ?><br>

in calculate.php

$count=$_POST["count"];
for($i=0;$i<count;$i++){
    $name="check" . $i;
    $checkvalue=$_POST[$name];
    $text="text" . $i;
    $textvalue=$_POST[$textvalue];
    //savetodatabase
}
Manian Rezaee
  • 1,012
  • 12
  • 26
  • It doesn't matter, you can change it to anything you want, i only need to capture each checkbox and textbox value on the next page. help please – user3342746 Feb 23 '14 at 07:55
0

HTML

<form action="process.php" method="post">
<input type="text" name="myText" />
<input type="checkbox" name="myCheck" value="someVal" />
<input type="sumbit" />
</form>

Process.php

$myTextVar = $_POST['myText'];
$myCheckVar = $_POST['myCheck'];

The $_POST[] array gets populated with any post data sent to the script, alternatively, you can use the get method, and use the $_GET[] array.

The index name comes from the name provided in the HTML form.

The value of the textbox comes for the whatever the user enters. The value in the checkbox comes from whatever value you set in the tag.

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
0

Try this (it uses input arrays like I mentioned in the comments):

$query="SELECT name,price FROM product";
$result=mysql_query($query);
$num_rows = mysql_num_rows($result);
if($num_rows>0){

    echo "<form name='waz' action='calculate2.php' method='post'>";
    while($row=mysql_fetch_array($result)){
        echo "<br>
        <input type='checkbox' name='name[]' value=".$row['name']." > ".$row['name']."
        <input type='text' name='text[]' >
        <br>";

    }
    echo "<input type='submit' name='submit' value='calculate'/>";
    echo "</form>";
}

Edited:

In calculate2.php:

if(isset($_POST['submit'])){

    if(isset($_POST['name'])){
      foreach($_POST['name'] as $key=>$val){
       echo $val; //this will return all checked values
      }
    }

    //Similarly for $_POST['text']
      foreach($_POST['text'] as $key=>$val){
       echo $val; //this will return all textbox values - you may need to check if they are empty
      }

}
AyB
  • 11,609
  • 4
  • 32
  • 47
  • Thanks but now how do it capture and save each value individually on calculate2.php? – user3342746 Feb 23 '14 at 08:05
  • Ya it work but i would not be able to calculate the total. previously on the calculate.php.. after getting the value to a php variable, i would use this code to calculate it: `$name=$_POST['name']; $qty=$_POST['qty']; $result = mysqli_query($con,"SELECT * FROM product WHERE name='$name'"); while($row = mysqli_fetch_array($result)) { echo $row['name']; echo "
    "; echo "price of product:
    "; echo $row['price']; echo "
    "; echo "Quantity of product:
    "; echo $qty; echo "
    "; echo "_____________
    "; echo "Total: "; echo $row['price']*$qty;`
    – user3342746 Feb 23 '14 at 11:25
  • @user3342746 make sure `$qty` contains a value – AyB Feb 23 '14 at 11:36
0

try like this:

echo "<form name='waz' action='calculate2.php' method='post'>
<br>";
while ($i < $num) {
 $name=mysql_result($result,$i,"name");
 $price=mysql_result($result,$i,"price");
 echo"
 <input type='checkbox' name='chk[]' value='$name; '/> $name
 <input type='text' name='txt[]' value='$name'/>
  <br>";$i++;
} 
echo "<input type='submit' value='calculate'/>"; ?><br>

in your calculate2.php :

you will have array of all text inputs:

$arrText =  $_POST['txt'];

you will have array of all checkbox inputs:

arrChk = $_POST['chk'];

then you can get each text and checkbox values e.g:

   $firstTxt = $arrText[0];
   $firstchk = $arrchk[0];
Awlad Liton
  • 9,366
  • 2
  • 27
  • 53