Skip to content Skip to sidebar Skip to footer

Checkbox To Send Only Specific Rows To Php Form Parser

I'm trying to use a form with php. I populate the form info from a database. The form input is separated into rows. The form offers a checkbox to only pass that row of data and w

Solution 1:

Try linking the input arrays together by giving them the same key, in this case $id -

while($row = mysql_fetch_assoc($query)){
    $id = $row["id"];
        $email = $row["user_email"];
    $androidId = $row["user_androidid"];
    $amount = $row["amount"];

    echo "<tr><td><inputid='data_id'name='data_id[".$id."]'size='25'value='".$id."'/></td><td><inputid='mail'name='mail[".$id."]'size='25'value='".$email."'/></td><td><inputid='androidId'name='androidId[".$id."]'size='25'value='".$androidId."'/></td><td><inputid='amount'name='amount[".$id."]'size='25'value='".$amount."'/></td><td>coming soon...</td><td><inputid='currency'name='currencyCode[".$id."]'size='25'value='USD'/></td><td><inputtype='checkbox'name='chosen[".$id."]'value='".$id."'/><td></tr>"; 
}

Then on the post you can get only the arrays that were checked -

foreach($_POST["chosen"] as$key=>$value){
     echo$_POST["data_id"][$key];
     echo$_POST["mail"][$key];
     echo$_POST["androidId"][$key];
     echo$_POST["amount"][$key];
     echo$_POST["currencyCode"][$key];
     echo$_POST["chosen"][$key];
}

Solution 2:

Maybe this is a better structure, that way you will have an array of values with the id as key, and an array of chosen values that holds the selected keys. Check also html errors that you have as <td><input type='checkbox' name='chosen[]' value='".$id."'<td>

echo "<tr><td><inputid='data_id'name='".$id."[data_id]'size='25'value='".$id."'/></td><td><inputid='mail'name='".$id."[mail]'size='25'value='".$email."'/></td><td><inputid='androidId'name='".$id."[androidId]'size='25'value='".$androidId."'/></td><td><inputid='amount'name='".$id."[amount]'size='25'value='".$amount."'/></td><td>coming soon...</td><td><inputid='currency'name='".$id."[currency]'size='25'value='USD'/></td><td><inputtype='checkbox'name='chosen[]'value='".$id."'/></td></tr>";

Now the PHP for testing:

if ( !empty($_POST['chosen']) ) {
    foreach ( $_POST['chosen'] as$value ) {
        print_r( $_POST[$value] );
    }
}

Solution 3:

I suggest you do a var_dump($_POST) and look at what you're getting sent overall because it amazes me (unless I have misunderstood) that you are getting the information (and only that information) for the row with the checkbox checked. For starters, if you don´t specify a "type" in your input type, the default type will be of "text". Which means you can edit whatever is in that textbox, so that when you hit the submit button (whether you have checked one of the check boxes or not) all of the values inside each text box are going to be sent.

There are multiple ways of doing what you need (processing only the information that belongs to the row with the checked checkbox).

Javascript help:

  1. Add a function called disable(), that will be called whenever your submit button is pressed. This function is in charge of going through the values of the unchecked check boxes rows and setting the text box's to "disabled", that way they won´t get sent to the server.

The pros of this is that ONLY the information associated to the checked check box is going to be sent, and you won´t have to worry about filtering through in the server.

Cosn,if you are not used to javascript this obviously is no help.

PHP server side:

I just saw Sean's answer, and it´s a good one, you could do that if you wanted to validate on the server instead. Just let me warn you though that the user could edit your text box data, for example, leave it empty or add what he pleases, and when he sends it and you try to access it via $_POST[input_name][$key] you won´t get what you had established from the server.

Post a Comment for "Checkbox To Send Only Specific Rows To Php Form Parser"