I am sure this has been asked hundreds of times but i can’t findanything useful anywhere even after looking for about a week.
What I want to do is have a form with several buttons on it such as “add”, “delete”, “edit” and “save”. They will all be normal buttons not submit buttons.
I have a php function that does the database stuff for each button action but I need to know how I would call that function depending on which button was clicked.
I am sure that I would use a javascript onclick function call on each of the buttons to call a javascript function but how would i call the php function from the javascript? I have looked at ajax and everyone keeps showing the basic functions on making a request but i can’t see anything about calling a php function.
Does anyone know how i could achieve what i am trying to do?
function performAction(action)
{
// ASSIGN THE ACTION
var action = action;
// UPDATE THE HIDDEN FIELD
document.getElementById("action").value = action;
// SUBMIT THE FORM
document.nameOfYourForm.submit();
}
Then obv make sure that the action of your form is the relevant php script. which then does the following
if($_POST && array_key_exists("action", $_POST)){
// CARRY OUT SANITIZATION AND DATA VALIDATION HERE!!!!!!!!!
// CARRY OUT RELAVANT ACTION
switch($_POST['action'])
{
case "edit":
editRecord();
break;
case "add":
addRecord();
break;
case "delete":
deleteRecord();
break;
case "save":
saveRecord();
break;
}
}
I tried your example neron as it made sense to me how it would work but i get a javascript error on document.adminform.submit();
Here is my form if you can see what is wrong
<script type="text/javascript">
function performAction(action) {
// ASSIGN THE ACTION
var action = action;
// UPDATE THE HIDDEN FIELD
document.getElementById("action").value = action;
// SUBMIT THE FORM
document.adminform.submit();
}
</script>
My other question is that I have a file with all the php functions and i include that at the top of the page. Will it find these functions when it works?
For some reason only FF and other standards compliant browsers can submit forms using their ID’s. IE does not like it and requires a name attribute.
Yes it should do provided you use the PHP code I provided you with in my original post. So you would have my php code at the top of your included file then all your functions below.