Page 1 of 2

Javascript confirm delete

Posted: Tue Aug 01, 2006 6:26 am
by mohson
Looking through the posts on this forum I found this code for confirming delete

Code: Select all

<form action='deleterecord.php' method='POST' onsubmit='return confirm("Are you sure?");'>
 <input type='hidden' name='id' value='123' />
 <input type='submit' name='delete' value='Delete record' />
</form>
I get the impression that this refers to a process page, my code does not link to a process page. It processes within the page.

So can someone show me how to do this delete are you sure within the same file?

Also I have 2 submit buttons one for save and one for delete.

how will I apply the javascript to the submit which links to the Delete submit and not the save submit?

My code for both save and delete are below:

Save

Code: Select all

if(isset($_GET['org_id'])){
			$org_id=$_GET['org_id'];
		}

		if ($_POST['Submit'] == 'Save'){

			foreach ($_POST as $formName => $phpName){
				$$formName = $phpName;
			}
Delete

Code: Select all

}
		


			if ($_POST['action'] == "Delete Record"){
$delete = mysql_query("DELETE FROM organisations WHERE org_id = '$org_id'") or die(mysql_error()); }

echo "<form action=\"editorganisations.html?org_id=$org_id\" method=\"post\">
<input type=\"hidden\" name=\"action\" value=\"Delete Record\">
<input type=\"submit\" value=\"Delete Record\">
</form>
        </td><p>";

Any help much appreciated.

Posted: Tue Aug 01, 2006 6:56 am
by volka
There might be a slight misconception on how and wheren a php script is executed.

Please try

Code: Select all

<html>
	<head><title>life cycle test</title></head>
	<body>
		<fieldset><legend>POST Parameters</legend>
			<pre><?php print_r($_POST); ?></pre>
		</fieldset>
		
		<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
			<div>
				<input type="text" name="abc" value="<?php echo rand(); ?>" />
				<input type="submit" />
			</div>
		</form>
		
		<div><?php echo date('H:i:s'); ?></div>
	</body>
</html>
There's php code before the <form> element and there is php code after it.
But each time you press the submit button both code blocks are executed - different data is displayed.
The whole script is processed each time it is invoked - always.
Therefore it makes no difference wether action="" points to the same scriptfile or to another.

Posted: Tue Aug 01, 2006 7:32 am
by mohson
ok I have put this into the top of my code byt nothing happens. cna anyone explain why?

Code: Select all

<script language="javascript">
function checkit(form)
{
    var answer = confirm('Are you sure?');

    if (answer) {
        form.submit();
    } else {
        return false;
    }
}
</script>

<form name="myform" action="" method="post" onSubmit="checkit(this)">
    <input type="submit" value="click.." />
</form>

Posted: Tue Aug 01, 2006 7:42 am
by mohson
Actually when inserting this code it has made a button called 'click' appear and when I select this button it gives me a pop up message exactly how I want. BUT the delete doesnt have effect. My old delete button still works

Heres my entire code for the page can someone provided assitance.

Code: Select all

<script language="javascript">
function checkit(form)
{
    var answer = confirm('Are you sure?');

    if (answer) {
        form.submit();
    } else {
        return false;
    }
}
</script>

<form name="myform" action="" method="post" onSubmit="checkit(this)">
    <input type="submit" value="click.." />
</form> 

<?php
		
/* Connecting, selecting database */
$link = mysql_connect("xxxx", "xxxx", "xxxx")
   or die("Could not connect : " . mysql_error());
echo "";
mysql_select_db("contact_management_system") or die("Could not select database");
	
		if(isset($_GET['org_id'])){
			$org_id=$_GET['org_id'];
		}

		if ($_POST['Submit'] == 'Save'){

			foreach ($_POST as $formName => $phpName){
				$$formName = $phpName;
			}
			
					$sql ="UPDATE organisations SET "
					. " orgname='"	.$orgname."'," 
					. " web_url='"	.$web_url	."'," 
					. " notes='" .	$notes	."'"
					. " WHERE org_id='" . $org_id."'";

			echo "<br><div align=center><br>";
			mysql_query($sql) or die('could not insert' . mysql_error());

			echo  "		<div align=left><table width=70% border=1 cellpadding=10 align=center>"
				. "		<tr>"
				. "			<td width=40%>"
				. "					<font face=arial size=2><b>"
				. "					Organisation"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $orgname . "</font>"
				. "				</td>"
				. "			</tr>"
				. "			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "					Web_ Url"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $web_url . "</font>"
				. "				</td>"
				. "			</tr>"
				. "			<tr>"
				. "				<td>"
				. "					<font face=arial size=2><b>"
				. "					Notes"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $notes . "</font>"
				. "				</td>"
				. "			</tr>"
				
				. "		</table>"; 
		}
		
		$sql = "SELECT * FROM organisations WHERE org_id='" . $org_id . "'";
			echo "<br><div align=center><br>";
			$memb = mysql_query($sql) or die(mysql_error());
			$row = mysql_fetch_array($memb);

			echo  "\n"
				. "		</div>\n"
				. "		<form name=form2 method=post action=editorganisations.html?org_id=" . $org_id . ">\n"
				. "			<table width=70% border=1 cellpadding=10 align=center>\n"
				. "				<tr>\n"
				. "					<td width=40%>\n"
				. "						<font face=arial size=2><b>\n"
				. "						Organisation\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=orgname type=text id=orgname value='" . $row['orgname'] . "'></font>\n"
				. "				<tr>\n"
				. "					<td width=40%>\n"
				. "						<font face=arial size=2><b>\n"
				. "						Web_Url\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=web_url type=text id=web_url value='" . $row['web_url'] . "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td>\n"
				. "						<font face=arial size=2><b>\n"
				. "						Notes\n"
				. "						</font>\n"
				. "					</td>\n"
				. "					<td>\n"
				. "						<font face=arial size=2>\n"
				. "						<input name=notes type=text id=notes value='" . $row['notes'] . "'></font>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "				<tr>\n"
				. "					<td colspan=2 align=center>\n"
				. "						<input type=reset name=Reset value=Cancel>\n"
				. "						<input type=submit name=Submit
				value=Save>\n"
				. "					</td>\n"
				. "				</tr>\n"
				. "			</table>\n"
				. "		</form>\n";
		
	//	}
		


			if ($_POST['action'] == "Delete Record"){
$delete = mysql_query("DELETE FROM organisations WHERE org_id = '$org_id'") or die(mysql_error()); }

echo "<form action=\"editorganisations.html?org_id=$org_id\" method=\"post\">
<input type=\"hidden\" name=\"action\" value=\"Delete Record\">
<input type=\"submit\" value=\"Delete Record\"</form>
        </td><p>";
?>

Posted: Tue Aug 01, 2006 7:44 am
by volka
Is there any data from that form transmitted?
print_r($_POST); will tell you.

Posted: Tue Aug 01, 2006 8:32 am
by mohson
I added the print Post thing you said to the bottom of the code and I got this:

Array ( )

No idea whats going on.


Have you got any ideas?

Posted: Tue Aug 01, 2006 8:53 am
by mohson
I think my question is quite simple really

How do I incorporate this:

Code: Select all

<script language="javascript">
function checkit(form)
{
    var answer = confirm('Are you sure?');

    if (answer) {
        form.submit();
    } else {
        return false;
    }
}
</script>
Into This

Code: Select all

cho "<form action=\"editorganisations.html?org_id=$org_id\" method=\"post\">
<input type=\"hidden\" name=\"action\" value=\"Delete Record\">
<input type=\"submit\" value=\"Delete Record\">
</form>

Posted: Tue Aug 01, 2006 8:56 am
by feyd
although I wouldn't personally use javascript for confirmation of the delete request, here's a hint: onclick.

Posted: Tue Aug 01, 2006 9:01 am
by mohson
thanks for the hint but I ant got a clue, surely it cant be that simple.

Posted: Tue Aug 01, 2006 9:27 am
by volka
Your form isn't submitting any data. No data the php script can use to decided what to do: delete or not delete.

Posted: Wed Aug 02, 2006 2:23 am
by mohson
So how do I correct this problem?

my form displays a persons record I then click my delete button and it deletes the record. when I click this new delete button created by the javascript nothing happens.

I suspect ive put the script in the wrong place or something.

What does anyone think.

Posted: Wed Aug 02, 2006 7:10 am
by volka
Your script reacts on
if ($_POST['action'] == "Delete Record"){
Your "new" form does not send this or any other data. Your old one does.

Posted: Wed Aug 02, 2006 7:30 am
by mohson
So basically your saying I have to incorporate this script

Code: Select all

<script language="javascript"> 
function checkit(form) 
{ 
    var answer = confirm('Are you sure?'); 

    if (answer) { 
        form.submit(); 
    } else { 
        return false; 
    } 
} 
</script>
into this

Code: Select all

echo "<form action=\"editorganisations.html?org_id=$org_id\" method=\"post\">
<input type=\"hidden\" name=\"action\" value=\"Delete Record\">
<input type=\"submit\" value=\"Delete Record\">
</form>

So how do I do that, really appreciate your help and patience.

Posted: Wed Aug 02, 2006 8:48 am
by volka
What makes the "new" form call that javascript function?
This piece of code you have to add to the "old" form.

Posted: Thu Aug 03, 2006 4:56 am
by mohson
Sorry I dont understand that?