Hi there~ I am kinda coding a verrry simple information system which uses Javascript.

I am not familiar with JS and I only know the basics, but maybe what I'll ask is a veeery simple one but oh well, should I be asking if I found a solution after searching in google? No. So I am here again in DW since I have a LOT of good impression and like 90% of the posts I made were solved. :D So here goes:

delete.php

    <?php
    $con=mysql_connect("localhost","root");
    mysql_select_db("dbfirest",$con);

    $s=$_POST['id'];
    ?>
    <script type="text/javascript">
    var answer = confirm ("Delete Item?")
    if (answer)
    <?php $sql="delete from tblfireincidents where fire_id='".$s."'";
    mysql_query($sql);?>
    alert ("Incident Deleted")
    else
    alert ("Delete Aborted")
    self.location="fire_index.php"
    </script>

So as you can see, here is the code for the delete button from a form which deletes a certain record ($s).. The thing is, I added a javascript prompt that asks the user if he is sure.. When clicked Yes, the record will be deleted.. If clicked Cancel.. It will not delete the record.. But that situation SHOULD be happening right?

But no.

That code, (up up there) if clicked Cancel will ALSO delete the record which shouldn't be happening at all. Uggh. Can you help me with this, guys?

Member Avatar for LastMitch

[HELP] Regarding If..Else statements..and prompts combined with PHP

Where is your <form>?

From this:

<?php
$con=mysql_connect("localhost","root");
mysql_select_db("dbfirest",$con);
$s=$_POST['id'];
?>
<script type="text/javascript">
var answer = confirm ("Delete Item?")
if (answer)
<?php $sql="delete from tblfireincidents where fire_id='".$s."'";
mysql_query($sql);?>
alert ("Incident Deleted")
</script>

to this:

<?php
$con=mysql_connect("localhost","root");
mysql_select_db("dbfirest",$con);
$s=$_POST['id'];
?>

<script language="javascript">
function vform(){
var answer = document.form.answer.value;
answer = answer.trim();
if (answer == '<?php $sql="delete from tblfireincidents where fire_id='".$s."'";mysql_query($sql);?>') {
alert("Delete Item?.");
document.form.answer.focus();
return false;
}
}
</script> 

Form:

<form action="yourfile.php" method="post" name="form" id="form" onSubmit="vform('answer')">
<input name="answer" type="text" id="answer'" size="30" />
<input name="Submit" id="Submit" onClick="vform('answer')" value="Next" type="Submit"> 

It's not tested so try it then post the result on how it goes.

Hi

Thanks for the quick response!

Basically the <form> only contains an input hidden (containing the $s) and input submit.

I will try this when I got home later. Hopefully this works. I was thinking last night that it needs a terminator (return false).. but when I added that, the prompt doesnt appear and simply delets the records... I see a lot have been revised from the original (lame) code ^^;;

Thanks again!

In my view deleting record can not be done buy pure html.

If you embed php code in javascript, that code will execute at the time of page loading and not when buttton is clicked.

simple javascript can not tell server to delete record (unless you use ajax).

And for taking confirmation, you must use confirm() function rather alert

if(confirm("Are you sure you want to delete"))
{
    document.form.submit();
}
else
{
    return false;
}

hi again Mitch, i have tried it. The record was deleted but when you click submit, there is no prompt asking if the user is sure to proceed with the deletion of the record. It should be -- If clicked cancel nothing will happen and will redirect back to the page -meaning the record will not be deleted.

hi utrivedi,

Yes, that is embedded with php so I can delete a record using javascript.

so if i understand it correctly,

    if(confirm("Are you sure you want to delete"))
    {
    document.form.submit();
    <?php $sql="delete from tblfireincidents where fire_id='".$s."'";mysql_query($sql);?>
    }
    else
    {
    return false;
    }

I would like to say that there is some misunderstanding.
You can not run php code under javascript (line 4 in above your code (your last post) will not work.

1) One basic thing you must understand that, php runs at server and javascript runs at clients browser

2) When page is loading in user's browser, php is executed at server, before user can see or do any action on that page. (not at client browser) (just view page source and see your function in javascript how it looks, do u find any php code there)

3) after getting confirmation using javascript, you must use javascript to submit your form
a) form may have hidden key value to delete
b) form action may be a php page which handles delete request
c) php page will delete that record and prints output in client browser, that record is deleted now.

mainpage.php

<script language="javascript">
function delform()
{
  if(confirm("Are you sure you want to delete"))
  {
      document.myform.submit();
  }
  else
  {
      return false;
  }
}
</script> 


<form action="delete.php" method="post" name="myform" id="myform" >
<input name="delid" id="delid" type="hidden" value="53" size="30" />
<input name="btnsubmit" id="btnsubmit" onClick="delform()" value="Delete record 53" type="button"> 
</form>

delete.php page

<?php
$con=mysql_connect("localhost","root");
mysql_select_db("dbfirest",$con);

$s=$_POST['delid'];

$sql="delete from tblfireincidents where fire_id='".$s."'";
$result=mysql_query($sql);

if($result)
   echo "Record $s deleted successfully";
else
   echo "Error occured while deleting Record $s";

?>

^sorry i was kinda dead brained last night when i read your post. and this one makes sense! (it shows how dumb i am at JS >.<) i will surely try this later and tell you if it's working!

Thanks heaps!

Hi I've tried it but it doesnt delete anything ;____;

post ur both code files

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.