Skip to content
Advertisement

How to pass a mysql database data to a javascript function using php?

I’m creating a web where people can add fanbase on it and delete it by clicking an X button. Here’s what I want: when a user click on the X button , a modal will pop-up saying “are you sure”, and when he presses delete button on the modal, the data will be deleted from database (I’m using mysql database).

When the user clicks the X button, I hope the modal showed up with the specific name of the fanbase that the user want to delete. But I don’t know how to pass the fanbase’s id (from my database) to a function i made and use it. Here what it looks like:

the modal from “welcome.php”

JavaScript

the code for iteratively add divs of fanbases from “showfanbase.php”

JavaScript

and onclick of that button, a function (deletefunction()) on the “Welcome.php” will be executed. inside it i pass the fanbase’s id that is about to be deleted. This function suppose to be only showing the modal, but i want to make this function can hold the fanbase’s id that is about to be deleted.

JavaScript

what do I had to add to my code so I can show the fanbase’s name on the modal and so I can pass that fanbase’s id to deletefanbase.php(this is for deleting the data at mysql database)?

Advertisement

Answer

you should take a hidden field to the form the post that value to the deletefanbase.php

take a hidden field in welcome.php in e.g.

JavaScript

when function goes execute set delete id to the hidden field befor you submit the form

JavaScript

it will post the value of that field with $_POST[‘delbyid’] on deletefanbase.php here you can execute the query to delete particular record with that id

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