Hi there

I have a form that I'm trying to submit but it just doesn't want to play nice.

The form i have has 2 submit buttons. One processes php code that updates info in a DB and the other deletes the info in the DB. The problem is that I have a popup asking for confirmation on the deleting and when I click ok nothing happens. All I need to know is how to submit my form using javascript so that it processes the PHP code to delete the DB entry.

form tag and delete button

<form name="formname" id="formname" method="post"  action="<?= $_SERVER['PHP_SELF']?>" onSubmit="return checkWholeForm(this);">

<input onclick=\"confDelete()\" type="submit" name="delete" value="Delete">

confirmation popup

<script type="text/javascript">
<!--
function confDelete() {
	var answer = confirm("Msg")
	if (answer==true){
		document.formname.submit();
		return true;
	}
}
//-->
</script>

Code that deletes DB entry

if (isset($_POST['formname'])){
\\ code goes here
}

Hi,
you cant have two submit button on one form, do one thing:
have two simple button< input type="button" value="delete"......> and have on hidden field in form with name "task".On onclick event of these button call a function where you set the value of task="delete" or whatever you want, after this submit the form using javascript.
On server side check the value of task, according to value you take the action( either delete or add or....), one more thing in all cases you'll be having only one php page, where you will perform action according to "task" value.
Have a nice time !!!

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.

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.

yes it is correct, but when you are submitting how you are making difference at server side(of which button user has clicked), if you have problem in that see my suggested solution....

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).

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.

I have called it submit1 and submit2.

yes i got it, i just forgot it, any way thanks,,,
then there is no problem at all, you can check at server side the name of button and on the base of this you can have intended functionality.....

Exactly !

so is there a way i can call the delete submit button with javascript?

eg. document.formname.submitButtonName.submit();

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

Thanks for that nav33n. Slowly getting somewhere, the form is submitting but it's not actually executing the php code. Is my php code below correct?:

if (isset($_POST['FormName'])) {

Nope.. $_POST 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
}

Still having problems executing the php code. this is what I have so far:

Javascript for setting the task value to del and then submitting the form

function confDelete() {
	var answer = confirm("Are you sure")
	if (answer){
		document.formName.task.value=='del';
		document.formName.submit();
	} else {
		return false;
	}
}

The hidden "task" field and the delete button

<input type="hidden" name="task" id="task" value="">

<input onclick="javascript: return confDelete();" type="button" name="delete" value="Delete">

If task has the value of del then execute PHP code

if ($_POST['task']=="del"){
// execute code
}

My second button is set up in a similar way. Right now neither of them execute the required PHP code.

document.formName.task.value=='del';

Should be document.formName.task.value='del';

commented: helped alot ;) +1

You would do far better not to rely on javascript for this at all. Instead you should parse the post with PHP and from that display a confirmation for the delete if delete was selected. That is simply good coding practice.

It is common to use javascript for error checking (I do myself) however the form should still submit and also be checked with PHP if javascript is disabled.


Matti Ressler
Suomedia

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.

Thanks nav33n. Got it working. ;)

Suomedia, ~s.o.s~...Thanks for your comments. I realize javascript could be switched off on some machines and would never use this kind of functionality if it was being used by the public. It's a simple admin system that's only going to be used by one person. ;)

You are welcome ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.