0

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:

ask

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.

2
  • why dont you add a column for ids and use that. Commented Dec 12, 2013 at 17:57
  • just as i said, the PHP always detect only the last data, not the data I choosed :( Commented Dec 12, 2013 at 18:10

1 Answer 1

2

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];
}
Sign up to request clarification or add additional context in comments.

3 Comments

Already done that, that's why the Insert Button Works.. But not the Update or Delete, because the textbox (input text) is same name.. And it made by calling from database... (while (getdata) fetch data). That's why I'm really confuse.. I want it works just like ListView at ASP, but now I want to create like that in PHP
You need to do the same thing with each input. <input type="text" name="program[1]">.
Aah.. I see that, so I need to create an array for naming the input and submit button. Nice idea. Thank You very much, will try it now.. :)

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.