0

i'm creating a front end to view a mysql database with options to filter by certain criteria (such as age and sex) as well as options for deleting specific entries in the database.

i used this http://www.tizag.com/ajaxTutorial/ajaxxmlhttprequest.php as a guide to build everything.

so far this is what i have: view.php - authenticates user, creates basic html with input boxes in order to define the filters, a submit button, javascript function that reads the values from the input boxes and calls ajax.php, basic html to display results of mysql query.

ajax.php - connects to the mysql DB, gets the input values from view.php, builds the mysql query from the input values, creates a table of all the mysql query result which is displayed by view.php.

within each row of the mysql query result, i have an html input button which, onclick, calls a javascript function intended for deleting the specific row. so each row has it's own delete button. this delete function right now resides in view.php.

my intention, is that the delete js function will call another file, say delete.php and that delete.php will activate the mysql query to delete the specific row based upon which delete button was clicked.

the only part i don't have working correctly is how to pass the correct row ID to delete.php.

any thoughts?

Constantino
  • 2,243
  • 2
  • 24
  • 41
  • It would help to show your existing code that constructs the buttons, the `delete` javascript function and that in `delete.php`. – eggyal May 04 '12 at 22:52

1 Answers1

1

In the onclick event of each button which is generated through php, store the row ID as the parameter of the function which sends that parameter to your delete.php.

Example:

In the php which generates each of your delete buttons:

echo '<input type="button" value="Delete" onclick="delete('. $row_ID .')" />';

Then just make the corresponding AJAX function delete(row_id) in your javascript which will pass that parameter as part of the request to your delete.php.

Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
  • great! that was exactly the info i needed and it worked. thanks Fabricio! – Constantino May 05 '12 at 01:12
  • a follow up question: is there any way to include some sort of 'Are you sure' message that pops up once the delete button is pressed? – Constantino May 05 '12 at 01:20
  • Of course, in the first line of the `delete` javascript function you'd put something like: `if (!confirm("Are you sure you want to delete?")) return false;` – Fabrício Matté May 05 '12 at 01:44
  • No problem, if that's all you can also click in the "accept answer" button which looks like a "V" check in the left side of my answer. :P – Fabrício Matté May 06 '12 at 17:37