Create Update Command at Same form With Many Data

寵の児 提交于 2019-12-10 12:18:18

问题


I have a form with PHP language and I want to use it for inserting data, updating data, and deleting data at once. First row of table is for insert data, second table and other row is for edit or delete data.

So, I have create form like this:

FYI : UBAH is Update/Edit, HAPUS is Delete, TAMBAH is Add/Insert

I've already done with inserting data, but I'm stuck while Updating or deleting data because the ID that the button I choose is always at last ID (in picture is the ID always refer to "Sarjana") so it doesn't works because I can only update and delete the last data that called at table.

So, what I want to ask is, how to make it works without AJAX? Because what I search in Google, they said it can be done with AJAX, but I don't understand AJAX (except it will stuck forever without ajax). And I don't want to create an href link just like "beginner example" that throw it to another pages then edit/delete it.

Note: It's just like a listview at ASP but I want to do it at PHP code. Final Words, I want to create Inline editing at my PHP code.

I hope You understand and sorry for my bad English.


回答1:


Add a unique id to each submit button:

<input type="submit" name="submit[1]" value="Submit">
<input type="submit" name="submit[2]" value="Submit">
<input type="submit" name="submit[3]" value="Submit">

Then you grab the id from $_POST or $_GET. For example:

Array
(
    [submit] => Array
        (
            [3] => Submit
        )

)

Edit:

Take this concept - HTML form elements to PHP arrays - and apply it to the other elements.

<table>
    <tr>
        <th>Program</th>
        <th>Department</th>
        <th></th>
    </tr>
    <tr>
        <td><input type="text" name="program[1]" value=""></td>
        <td><input type="text" name="department[1]" value=""></td>
        <td>
            <input type="submit" name="edit[1]" value="Edit">
            <input type="submit" name="delete[1]" value="Delete">
        </td>
    </tr>
    <tr>
        <td><input type="text" name="program[2]" value=""></td>
        <td><input type="text" name="department[2]" value=""></td>
        <td>
            <input type="submit" name="edit[2]" value="Edit">
            <input type="submit" name="delete[2]" value="Delete">
        </td>
    <tr>
        <td><input type="text" name="program[3]" value=""></td>
        <td><input type="text" name="department[3]" value=""></td>
        <td>
            <input type="submit" name="edit[3]" value="Edit">
            <input type="submit" name="delete[3]" value="Delete">
        </td>
    </tr>
</table>

Again: use the unique IDs to set each row's form elements apart.

Here's one method for grabbing the data in PHP. Obviously you need to get the ID from the submit button and then use that ID to reference elements in the $_POST or $_GET array.

if ( isset($_POST['edit']) && is_array($_POST['edit']) ) {
    $key = key($_POST['edit']);
    $program = $_POST['program'][$key];
    $department = $_POST['department'][$key];
}


来源:https://stackoverflow.com/questions/20550632/create-update-command-at-same-form-with-many-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!