You can actually have 2 submit buttons in a form. Eg.
<?php
if($_POST['submit1']) {
echo "Submit1 pressed! Add/update the records";
}
if($_POST['submit2']){
echo "Submit2 was pressed ! Now delete the records !";
}
?>
<html>
<head>
<script type='text/javascript'>
function confirmdel() {
var val = confirm("Do you really want to delete ?");
if(val) {
return true;
} else {
return false;
}
}
function calltest() {
alert("Hi there!");
return true;
}
</script>
</head>
<body>
<form method="post" action="thispage.php" onsubmit="javascript: return calltest();">
<input type="submit" name="submit1" value="add"><input type="submit" name="submit2" value="delete" onclick="javascript: return confirmdel();">
</form>
</body>
</html>
But, since both the buttons are submit buttons, onSubmit event will be fired when you click either of the buttons.
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
DangerDev, My problem is that I don't know javascript very well and haven't been able to find any solutions. Is the section of code for submitting the form correct?
document.formname.submit();
P.S. You can have two submit buttons on one form. I've used it successfully on many pages.
Since you are already using the submit button in your form, you can't submit that way (I think).
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
DangerDev, you can know which button was pressed by giving each button a unique name. In my example, I have called it submit1 and submit2.
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
eg. document.formname.submitButtonName.submit();
I dont think you can. But you can do this. Instead of having a submit button for delete button, have a normal button as DangerDev has mentioned. Then onclick call a javascript function and submit the page. Eg.
<?php
print_r($_POST);
?>
<html>
<head>
<script type="text/javascript">
function delsubmit() {
var conf;
conf=confirm("Are you sure ?");
if(conf) {
document.test.submit();
} else {
return false;
}
}
</script>
</head>
<body>
<form name="test" method="post" action="<?php echo $PHP_SELF; ?>">
<input type="text" name="name" value="1">
<input type="button" name="button" value="Delete" onclick="javascript: return delsubmit();">
</form>
</body>
</html>
This is just a simple example. The next snippet of code will not work, because there is already a submit button. Eg.
<?php
print_r($_POST);
?>
<html>
<head>
<script type="text/javascript">
function delsubmit() {
var conf;
conf=confirm("Are you sure ?");
if(conf) {
document.test.submit();
} else {
return false;
}
}
</script>
</head>
<body>
<form name="test" method="post" action="<?php echo $PHP_SELF; ?>">
<input type="text" name="name" value="1">
<input type="submit" name="submit" value="add">
<input type="button" name="button" value="Delete" onclick="javascript: return delsubmit();">
</form>
</body>
</html>
To make both of them work, you should have 2 buttons. Eg.
<?php
print_r($_POST);
?>
<html>
<head>
<script type="text/javascript">
function delsubmit() {
var conf;
conf=confirm("Are you sure ?");
if(conf) {
document.test.submit();
} else {
return false;
}
}
</script>
</head>
<body>
<form name="test" method="post" action="<?php echo $PHP_SELF; ?>">
<input type="text" name="name" value="1">
<input type="button" name="button" value="add" onclick="javascript: document.test.submit();">
<input type="button" name="button" value="Delete" onclick="javascript: return delsubmit();">
</form>
</body>
</html>
If you still have problems, let me know!
Cheers,
Naveen
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
Nope.. $_POST['formname'] will not hold any value. You can do it this way (again, as DangerDev mentioned), have a hidden variable (task). When you click on 1st button, assign a value to task variable. document.formname.task.value='add'; Similarly, assign another value when another button is clicked. document.formname.task.value='delete';
Then you can check what value task variable holds.
if($_POST['task']=="add"){
//add
}
if($_POST['task']=="delete"){
//delete
}
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
document.formName.task.value=='del';
Should be document.formName.task.value='del';
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
Relying on Javascript to deliver the business functionality is dangerous to the business which you are trying to cater. If this is some school project, it doesn't matter anyways but if this is for real, you need to start rethinking your design and add a server side check for such critical functionality.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356