Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - How to submit a form containing input fields that point to different MySQL rows

I am setting up a form using this PHP that loops through all records a user may have:

    <?php foreach ($items as $row): ?>
            <tr>
                <td>
                    <?php echo form_hidden('id', $row->id); ?>
                </td>
                <td>
                    <?php echo '<strong>' . $row->name . '</strong>'; ?>
                </td>
                <td>
                    <?php echo form_input('number', $number); ?>
                </td>
                <td>
                    <?php echo form_input('registry', $registry); ?>
                </td>
                <td>
                    <?php echo form_checkbox('OK', $ok, $ok); ?>
                </td>
            </tr>
    <?php endforeach; ?>

This gives me a form with the following look:

enter image description here

The idea here is that each row belongs to a unique ID/row in the database, and I would like to allow the user to edit all on the same page/form, using a single submit button.

What would be the best way of implementing this?

When this data is submitted, there should be a way of looping through each packet of information (from each user) in my controller. Would this be done via ajax/json?

like image 739
pepe Avatar asked Dec 05 '25 18:12

pepe


2 Answers

This does not use codeigntier, but you should be familiar with the general technique before attempting to use CI to shortcut this process. Codeigniter will help you with rendering the form elements, performing validation, escaping your input and performing your query - but it will only help you (do anything) if you understand the basic principles involved. Hope this helps

MARKUP

<form action="/process.php">
<div>
    <h2>GORDON</h2>
    <input type="text" name="user[1][number]" /> <!-- The number corresponds to the row id -->
    <input type="text" name="user[1][registry]" />
    <input type="checkbox" name="user[1][ok]" value="1" />
</div>
<div>
    <h2>ANDY</h2>
    <input type="text" name="user[242][number]" />
    <input type="text" name="user[242][registry]" />
    <input type="checkbox" name="user[242][ok]" value="1" />
</div>
<div>
    <h2>STEWART</h2>
    <input type="text" name="user[11][number]" />
    <input type="text" name="user[11][registry]" />
    <input type="checkbox" name="user[11][ok]" value="1" />
</div>

<input type="submit" />

PHP

$users = $_REQUEST['user'];

foreach ($users as $rowId => $info){

    // YOU SHOULD MAKE SURE TO CLEAN YOUR INPUT - THIS IS A GUESS AT WHAT YOUR DATA TYPES MIGHT BE
    $id = (int) $rowId;
    $number = (int) $info['number'];
    $registry = mysql_real_escape_string($info['registry']);
    $ok = (int) ($info['ok']);

    $q = "UPDATE user SET number = $number, registry = '$registry', ok = $ok WHERE id = $id";
    mysql_query($q);

    // You may want to check that the above query was sucessful and log any errors etc.

}
like image 131
calumbrodie Avatar answered Dec 08 '25 07:12

calumbrodie


There's no need to use ajax mate.

For each put a hidden input with the ID of the row in this format:

<input type="hidden" name="id[<?= $row->id ?>]" value="<?= $row->id ?>" ?>

Do the same for each element in the tr, i.e. name them as

name="number[<?= $row->$id ?>]"
name="registry[<?=$row->$id ?>]"
name="ok[<?=$row->$id ?>]"

and once you post the FORM you can iterate each row with:

foreach ($_POST['id'] as $key => $value) {
    echo $_POST['name'][$key];
}
like image 42
hex4 Avatar answered Dec 08 '25 08:12

hex4



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!