Issues encountered with the delete function using distinct row identification format

I am encountering an issue with variable value in input field.

To provide some context, I have client information stored in a MySQL database. Using PHP, I retrieve this data and display it in a table. Currently, I am working on implementing a delete option.

The intended functionality is that when a button is clicked, the ID of the corresponding row gets captured and sent to "delete.php" for deletion.

However, I am facing problems as whenever there are multiple rows (representing different clients), the ID from the first row is always obtained.

Please review the code below and assist me in identifying the mistake causing the incorrect retrieval of the row ID in the form.

<?php
    while($row = mysql_fetch_array($result))
    {
    echo "
    <table align='center' width='962px' style='color:#000; font-family:Tahoma;'>
        <tr>
            <td width='5%' height='35'><center><span class='text3'>" . $row['id'] . "</span></center></td>  
            <td width='22%' height='35'><center><span class='text3'>" . $row['imeinpriimek'] . "</span></center></td>
            <td width='18%' height='35'><center><span class='text3'>" . $row['datuminkrajrd'] . "</span></center></td>
            <td width='25%' height='35'><center><span class='text3'>" . $row['datumvnosa'] . "</span></center></td>     
            <td width='30%' height='35'> 

    <form action='delete.php' method='post' id='form'>

    <button type='button' style='border: 0; background: #fff;' onclick=\"popup('Are you sure you want to delete the following client?<br><br> <b>" . $row['imeinpriimek'] . "</b>')\"><img src='images/delete.png' title='Delete'><input type='text' name='id' value='" . $row['id'] . "' /</button>



    <div id='dialog-box'>
    <div class='dialog-content'>
    <div id='dialog-message'></div>

    <button type='submit' onclick=\"$('#form').submit()\" style='border:1px solid #125e94; border-radius: 3px; -moz-border-radius: 3px; font-size:12px; width: 30px; height: 25px; text-align:center; color: #e6e4e2; background: #0397ff;'>Yes</button> 

    <button style='border:1px solid #125e94; border-radius: 3px; -moz-border-radius: 3px; font-size:12px; width: 30px; height: 25px; text-align:center; color: #e6e4e2; background: #0397ff;'>No</button>
    </form> 
    </div>
    </div>

    </td>
    </tr>
    </table>

    ";
    }
    mysql_close($con);
?>

Thank you.

Answer №1

It is important to ensure that all forms in your code have unique IDs. In order to achieve this, make the following changes:

<form action='delete.php' method='post' id='unique-form'>

instead of:

<form action='delete.php' method='post' id='form'>

Additionally, modify the code for the submit button as follows:

<!-- Since this is a type="submit" button, there's no need for onclick -->
<button type='submit'>Submit</button>

instead of:

<button type='submit' onclick=\"$('#form').submit()\">Submit</button>

Answer №2

Each form should have a unique identifier. Consider modifying the line:

 <form action='delete.php' method='post' id='form'>

To:

 <form action='delete.php' method='post' id='form<?php echo $row['id']; ?>'>

Furthermore, make sure to update the line referencing the submit button to match the modified form:

<button type='submit' onclick=\"$('#form<?php echo $row['id']; ?>').submit()\" style='border:1px solid #125e94; border-radius: 3px; -moz-border-radius: 3px; font-size:12px; width: 30px; height: 25px; text-align:center; color: #e6e4e2; background: #0397ff;'>Da</button> 

Remember that when processing the form data on the server side, how you handle it matters. Additionally, ensure that other <div> elements like dialog-box and dialog-message also have distinct identifiers. Otherwise, your HTML will be invalid. However, I cannot confirm if this is causing any issues.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to designate touch as the top finger and always have touch 2 be the bottom finger on the screen?

Is there a way to specify touch1 as the upper finger on the screen and always have touch2 as the lower finger, even if the lower touch is detected first? var touch1 = e.originalEvent.touches["0"]; var touch2 = e.originalEvent.touches["1"]; Can these ...

Step-by-step guide to implementing a user-friendly search input field using the powerful AngularJS material design framework

I am searching for an effortless method to implement a feature similar to the expandable search text field in angular-mdl. By clicking on a search button, it will expand into a text field. <!-- Expandable Textfield --> <form action="#"> < ...

Sorting outcomes based on HTML file

Attempting to narrow down results based on certain criteria within a large HTML file. Unsure if my query is attainable, but it never hurts to inquire. Currently immersed in a browser game featuring a vast map measuring 100x100, with each pixel representing ...

Retrieve the id of the clicked hyperlink and then send it to JQuery

<a class = "link" href="#" id = "one"> <div class="hidden_content" id = "secret_one" style = "display: none;"> <p>This information is confidential</p> </div> <a class = "link" href="#" id = "two" style = "display: non ...

Guide to adding checkbox IDs and inserting them into a textarea

Hello, my name is Prasath and I am currently learning about AJAX. I am working on a project involving seat layouts and finding it challenging to retrieve both the seat fare and seat number simultaneously. Is there anyone who can assist me in resolving this ...

The height and alignment of the <a> tag and <p> element vary within a flex container

Currently, I am in the process of constructing a bottom navigation bar for a blog page. This navigation bar will allow users to easily navigate between the last blog entry, the main blog home/index, and the upcoming post. However, when the user reaches the ...

ng-class not functioning properly when invoked

In my controller, I have the following function: $scope.menus = {}; $http.get('web/core/components/home/nav.json').success(function (data) { $scope.menus = data; $scope.validaMenu(); }).error(function () { console.log('ERRO') }); ...

Utilize jQuery to swiftly align elements within a designated container

Check out my JSFiddle example: http://jsfiddle.net/c62rsff3/ I'm attempting to implement snapping behavior between 2 elements using this code: $('.draggable-elements').draggable({ snap: true }); In addition, I've defined a container ...

Using AJAX and PHP to retrieve and store variables in a database

This is my first time attempting this and I'm feeling frustrated because I'm struggling to figure out how everything fits together. I have a function that needs to call two PHP files - one for selecting information from a database, and the other ...

Retrieving JSON database entries from MySQL using PHP

I'm currently working on an app with a challenging client who often changes their requirements. To handle this, I've decided to store some data as JSON objects. Within the app, I have a system for matching users and storing their information as ...

Display or conceal content based on checkbox status

i am trying to create a system where content is hidden by default and only displayed when a checkbox is checked. The goal is to show the content when the checkbox is checked and hide it when unchecked, so that other content can be shown as well upon checki ...

A white background emerges when I select md-dropdown

Lately, I've been experiencing an issue on my website where a white background pops up whenever I click on a dropdown (md-select) in my form. Initially, it only happened with forms inside a modal, but now it's affecting dropdowns across the entir ...

Display MQTT information on a Django template

I've been utilizing paho-MQTT successfully to receive messages. However, I'm facing a challenge in displaying the received data in a template. Below is an excerpt of my code: import paho.mqtt.client as mqtt import json def on_connect(client, use ...

Disabling Bootstrap Js, JQuery, and CSS in Yii2

Can anyone suggest an alternative to using bootstrap.css and bootstrap.js? I've attempted the following: 'assetManager' => [ 'bundles' => [ 'yii\bootstrap\BootstrapAsset' => [ & ...

Exploring the Possibilities of Wordpress Search with Multiple Dropdown Options

Is it possible to search across multiple categories? For example, I have 4 dropdown menus: 1. City 2. Area 3. Month 4. Products/Services When a user visits my site, they will see a static page with 4 dropdown lists and a "search" button. After the user ...

Investigating the presence of a file within the superior directory with RewriteCond

I am attempting to enhance this .htaccess file in order to redirect all requests to a script when the requested file or directory is non-existent. However, I want to refactor it so that it checks for the existence of the file in the parent folder. Curren ...

What's causing this error with jQuery and JSON?

When utilizing the following code snippet: - $.getJSON("admin.php?format=json", { module: "data", action: "allBusinessUnitsByClientName", clientname : $('#client').val() }, function(json) { $.each(json.items, function(i,item){ alert ...

Get the image name from the database using the background:url property

I have a database reference to an image and I want to use it as a background:url in my design. Here is the image I created: . However, when I try to display the image using a div style in my code, it doesn't show up. Can someone please help me with th ...

Error: Trying to access the parent node of an undefined property

After incorporating this script into my webpage for the newsletter sign-up process, I aimed to reveal customized content post user subscription. <script> function insertAfter(referenceNode, newNode) { referenceNode.parentNode.insertBefore(newNode ...

Is it possible to trigger a winform function from an HTML page?

Looking for assistance with my WinForm application that includes a C# WebBrowser control. The WebBrowser control loads an HTML page which features a button. I am seeking a way to trigger a function in the WinForm (such as Form1.cs) when this button is cl ...