0

i have a similar thread using javascript but i found some problem and some bugs. so i changed to php

so far this is my code

if(!empty($_POST['check'])) {
    foreach($_POST['check'] as $check) {
        //get the html data beside the checked checkboxes

    }
    }else{
        echo '<script language="javascript">';
        echo 'alert("Nothing checked")';
        echo '</script>'; 
    }

and my stripped html code

<tr>
                                <td><?php echo $r['Name'] ?></td>
                                <td><?php echo $r['Age'] ?></td>
                                <td><?php echo $r['Address'] ?></td>

                                <td><input name="check[]" type="checkbox"  ></td>
                            </tr>

i wan't to get the data on the same row where a checkbox is checked. please help me. thanks

2 Answers 2

1

You have to loop through the POST data, and assign $r[NAME] = VALUE

For the PHP code:

    array $r;
    foreach($_POST as $post=>$value) {
        //get the html data beside the checked checkboxes
        $r[ $post] = $value;
        } 
    }
    }else{
        echo '<script language="javascript">';
        echo 'alert("Nothing checked")';
        echo '</script>'; 
    }

Assuming that the HTML part your referring to is the result page For the HTML:

<tr>
                                <td><?php echo $r['Name'] ?></td>
                                <td><?php echo $r['Age'] ?></td>
                                <td><?php echo $r['Address'] ?></td>

                            </tr>
Sign up to request clarification or add additional context in comments.

2 Comments

can you please elaborate sir? or to be exact. what code do i need to get the data of a html table using php. kindly explain clearly sir. english is not my language. thank you, i tried the code above and it doesn't work.
Can you update OP to show what the desired outcome is?
0

First, lets add some data:

<?php

$data = [
  '111' => [
    'Name' => 'John',
    'Age' => '25',
    'Address' => 'Baker street, 11'
  ],
  '222' => [
    'Name' => 'Mary',
    'Age' => '19',
    'Address' => 'Elm street, 14'
  ],
  '333' => [
    'Name' => 'Nick',
    'Age' => '23',
    'Address' => '64th stree, 2'
  ]
];

?>

Output all data items and set checks from previous posted data:

<form method="POST">
<table>
  <?php foreach($data as $id => $item): ?>
  <tr>
    <td><?= $item['Name'] ?></td>
    <td><?= $item['Age'] ?></td>
    <td><?= $item['Address'] ?></td>
    <td><input name="check[]" type="checkbox" value="<?= $id; ?>"
      <?php if (isset($_POST['check']) && in_array($id, $_POST['check'])): ?>
      checked="checked"
      <?php endif; ?>/></td>
  </tr>
  <?php endforeach; ?>
</table>
<input type="submit" />
</form>
<?php if (empty($_POST['check'])): ?>
<script>alert("Nothing checked");</script>
<?php endif; ?>

You'll get reloaded page with properly marked checkboxes after the form submission.

4 Comments

what is want to do sir is get the data on the same row where a checkbox is checked.
@knowmeifyou You got it there.
can you show me sample code in jsfiddle sir to solve my problem. i really dont understand. i understand a code better when it's an example
PHP is a server-side language, create a test index.php file, copy whole code to it and request the page by your browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.