Skip to content
Advertisement

Javascript won’t work properly with my form

So I am currently doing a project for our class, in which I have to display something that I previously stored in a database on a separate page. I chose to display every dataset, as it is the easiest to do. With PHP I implemented a foreach loop which is supposed to show each dataset with a button “EDIT” underneath it. When I don’t use javascript and just have the class of the edit form set to show, it works, and I can edit all my entries but the form is always visible. If I try to hide the form with javascript, all the entries are still shown, but if I press the button “EDIT” which has a seprete function to set the class to “show” it only gives me the option to edit the first entry in the database.

Here is my code for the php/html:

and here is the javascript (it is kept very simple, as we haven’t had a javascript instruction yet)

(I am sorry for all grammatical mistakes, I am not native)

I found a solution that worked for me… I don’t know why it didn’t work before but I had to work with variables.

Advertisement

Answer

IDs need to be unique on your HTML document. document.getElementById('edit-form') will return the first element on the page with the id edit-form. You could for an example append the row number to your id, and then pass it as a parameter to your javascript functions.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement