tags:

views:

99

answers:

7

can someone give me help, please.

here's my basic html

<form action="addSomething.php" method="POST">
<table>
 <tr>
   <th>Add Data</th>
   <th>Description</th>
   <th>Quantity</th>  
</tr>

 <tr>
  <td><input type="checkbox" name="data[]" value="sample1" /> </td>
  <td class="desc">Newbie</td>
  <td>2</td>
 </tr>

<tr>
<td><input type="checkbox" name="data[]" value="sample1" /> </td>
<td class="desc">Pro</td>
<td>1</td>
</tr>

<tr>
<td><input type="checkbox" name="data[]" value="sample1"/> </td>
<td class="desc" > Master </td>
<td>1</td>
</tr>

<br/>
     <input type="submit" name="add" value="SUBMIT"/>
.....

how can I get the one with the class "desc" and the column for quantity that is checked after submitting the form

because the only I can add when querying in mysql is the value of checkbox but I want also the value of the data in "Description" column and "Quantity" column

In my addSomething.php the code I have is

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

           foreach($_POST['data'] as $value)
           {

           $sql = "INSERT INTO tablename (column1) VALUES('$value');"

                //query stuff
            }

      }

what I will do , Any hints guys?

A: 

What you could do is write Javascript that triggers an AJAX request to your server.

Otherwise, you won't be able to get values in prior submit.

Vladimir Kocjancic
A: 

That, is by far, very inefficient. Executing a query for each field in your table.

Ben Fransen
+1, this is not the best way of doing what you want to do. Not at all.
Gausie
thanks for suggesting.
arnold
A: 

You will either have to put the description in a hidden field or read the description from the database. I suggest the later option.

Jan Hančič
A: 

You'll need to include these values in a hidden input

<input type="hidden" name="whatever" value="whatever>"

as an example

Gausie
A: 

I think the best way for your code is you create a javascript function and call it on submit of form :

<form action="addSomething.php" method="POST" onsubmit="return postVars()">

when you submit the form, this function will be called. create a 2 hidden forms like this :

<input type="hidden" name="description" id="description">
<input type="hidden" name="quantity" id="quantity">

give a unique id to your td s and pass the id to your js function. fill these inputs in your js function :

document.getElementById('description').value = document.getElementById('td_' + id + '_description').innerHTML;
document.getElementById('quantity').value = document.getElementById('td_' + id + '_quantity').innerHTML;

and in your server side just get description and quantity from $_POST. hope it's been helpful ;)

Ali Bozorgkhan
A: 

you can give your checkboxes different values

<input type="checkbox" name="data[]" value="newbie" />
<input type="checkbox" name="data[]" value="pro" />
<input type="checkbox" name="data[]" value="master" />

and then in addSomething.php define an array

$names = array('newbie'=>'Newbie', 'pro'=>'Pro', 'master'=>'Master');

and use it in your sql

if(isset($_POST['add']))
  {
       foreach($_POST['data'] as $value)
       {
           $sql = "INSERT INTO tablename (column1) VALUES ('".$names[$value]."');";
        }
  }

this applies only if you don't want users to edit the decription and quantity on the frontend. if you do, you need to put inputs in there and give them unique names.

jab11
A: 

I'd advise something along the lines of:

<form action="addSomething.php" method="POST">
<table>
 <tr>
   <th>Add Data</th>
   <th>Description</th>
   <th>Quantity</th>  
</tr>

 <tr>
  <td><input type="checkbox" name="data[0]" value="sample1" /> </td>
  <td class="desc">Newbie<input type="checkbox" name="desc[0]" value="Newbie" /></td>
  <td>2<input type="checkbox" name="quan[0]" value="2" /></td>
 </tr>

<tr>
<td><input type="checkbox" name="data[1]" value="sample1" /> </td>
<td class="desc">Pro<input type="checkbox" name="desc[1]" value="Pro" /></td>
<td>1<input type="checkbox" name="quan[1]" value="1" /></td>
</tr>

<tr>
<td><input type="checkbox" name="data[2]" value="sample1"/> </td>
<td class="desc" > Master <input type="checkbox" name="desc[2]" value="Master" /></td>
<td>1<input type="checkbox" name="quan[2]" value="1" /></td>
</tr>

<br/>
     <input type="submit" name="add" value="SUBMIT"/>
...

with php

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

           foreach($_POST['data'] as $i => $value) {
              $desc = mysql_real_escape_string($_POST['desc'][$i]);
              $quan = mysql_real_escape_string($_POST['quan'][$i]);
              $sql = "INSERT INTO tablename (desc,quan) VALUES('$desc','$quan');"

                //query stuff
            }

      }

You have to explicitly number your fields in html to preserve associacion of data with checkboxes.

Kamil Szot