WebDeveloper.com

WebDeveloper.com (http://www.webdeveloper.com/forum/index.php)
-   JavaScript (http://www.webdeveloper.com/forum/forumdisplay.php?f=3)
-   -   Confirm stop query string var code (http://www.webdeveloper.com/forum/showthread.php?t=93500)

tripwater 01-30-2006 02:53 PM

Confirm stop query string var code
 
I have a link beside each record in a list that when clicked, it calls the same page and passes ?delete=true


if my page sees the php $_GET["delete"] == true then it removes the record. Now I have a confirm dialog that pops up to tell them that if they do this, it cannot be undone giving them the chance to cancel. But as of now, the dialog comes up but it still passes the query string no matter if they hit ok or cancel. Can someone tell me how to stop or reset the delete query string from telling the page to remove if they hit cancel? Then place focus back on the form?

here is my code

Code:

<SCRIPT LANGUAGE=\"JavaScript\" type=\"text/javascript\">
function doublecheck()
{
var yorn = confirm(\"You are about to remove a company and all associated departments, employees and events. This cannot be undone.\");

if(!yorn)
{
//form name is company
document.company.name.focus();
return false;
}
else
return true;
}
</script>




Code:


//here is my removal php code
if (!empty($_GET["delete"]))               
company_removal($_GET["delete"], $HTTP_SESSION_VARS["db"]);

Code:

//here is my removal link (it is in a for loop with more than one)
<a href=../main/company.php?delete=".$row["ID"]." onClick=\" return doublecheck();\"> Delete</a>

Thanks for any help with this

bathurst_guy 01-30-2006 02:55 PM

Code:

<SCRIPT LANGUAGE=\"JavaScript\" type=\"text/javascript\">
function doublecheck()
{
var yorn = confirm(\"You are about to remove a company and all associated departments, employees and events. This cannot be undone.\");

if(!yorn)
{
//form name is company
document.company.name.focus();
return false;
}
else{
return true;
}
}
</script>


tripwater 01-30-2006 02:57 PM

what? Is that not my exact code? Or did you add something?

bathurst_guy 01-30-2006 02:58 PM

I added a couple of brackets you missed out

tripwater 01-30-2006 03:02 PM

Are you talking about on the else statement? I am confused...those brackets are not required for my code to work...Am I missing something?

The confirm dialog does come up, it does place focus back on the page...however what I need is if they Click cancel, for the query string ?delete=true to not equal true as this will tell my page to remove them anyway. So if they click on the delete link, and they choose cancel from the dialog, even though it still calls the same page, I need the delete var to not equal true or to stop the page from going to the URL of the link...either way. Does your brackets take care of this?

bathurst_guy 01-30-2006 03:08 PM

oh ok, not sure, too tired

tripwater 01-30-2006 03:26 PM

ok, that did not work...let me clarify. This link has nothing to do with the form that is also on the page. The form allows you the ability to add another company. While at the bottom of the page there is a list of the current companies in the system(which is a separate section) and by each there is a delete link that holds the ID of each company. If you click the delete link beside a company, it calls the same page, telling some php code to remove the record from the database. I want the link to prompt a confirm box(which it does) and if the cancel button of the confirm box is hit, then it places focus back onto the page and the php code never sees the $_GET["delete"] value because if it did, it would remove the record anyway. As of right now, the confirm box comes up and if you click on the cancel button, it still passes the delete query string and removes the record.


The last bit of code posted by bathurst_guy does not even bring up the confirm dialog box so I am restoring my code to the original posted code FYI.

***EDIT***well bathurst removed the code so nevermind the above remark, but I still need help :)

Thanks again for your time and any help with this

bathurst_guy 01-31-2006 12:03 AM

try
Code:

//form name is company
document.location("page.php");
return false;


tripwater 01-31-2006 09:40 AM

I tried that...I thought that would work. I see what you are doing there. But unfortunately it still is passing the $_GET["delete"] from the link that I click to call that function.

Is there a programmatic way to set that "?delete=id" var equal to something else in that function if they clicked cancel in the confirm box? That way, I can check against it in the php if statement and if it equals say "nodelete" then It won't go in there...

Thanks again for your help.

Any ideas?

bathurst_guy 01-31-2006 09:43 AM

It would be easier if rather than links you made them buttons and then return false would work. You can use CSS to style the buttons so that they still appear as a regular link if thats what you want?

pixelDepth 01-31-2006 09:49 AM

This works fine...

PHP Code:

<html>
<
title>Confirm</title>
<
head>

<
script type="text/javascript">
<!--

function
doublecheck(){
    if(
confirm("You are about to remove a company and all associated departments, employees and events. This cannot be undone.")){
        return
true;
    }

    return
false;
}

//-->
</script>

</head>
<body>

<a href="?action=delete&id=1" onclick="return doublecheck()">Delete</a>

</body>
</html>

Hope it helps :)

tripwater 01-31-2006 01:40 PM

Pixeldepth,

Thank you first off for your help.

I tried your code and I get the same results as I have been. What I have does popup the dialog box for confirmation and if you hit cancel, it does return you to the page. The issue I am having is that when you click the delete link it still passes id=1 (or delete=1 in my case) to the page and it tells my php code to remove the company with id of 1.

If they hit cancel it needs to not enter the php code meaning the $_GET["id"] (or $_GET["delete"] in my case) needs to no longer hold the ID or needs to hold something else so I can prevent it from entering the removal function.

I hope this clarifies what I am trying to do...I am running out of ways to explain it :)

Bathurst...I think I will try the button idea next...not sure how to make a button look like a link though, but I can figure that out if your solution works...

Thanks again for your time on this

tripwater 01-31-2006 02:25 PM

OK, I tried bathurst's idea using a button and here is my code so far



Code:

<js>
function doublecheck(comp)
        {
        alert(comp);
        if(confirm(\"You are about to remove a company and all associated departments, employees and events. This cannot be undone.\"))
                {
                document.location(\"../main/company.php?delete=comp\");
               
                return true;
                }
               
               
        else
                {                                       
                document.location(\"../main/company.php\");
                return false;
                }
        }
</js>


then my buttons look like this

Code:

<input type=button  onClick=\"return doublecheck(".$row["ID"].");\" value=Delete>

This gets no echo so even if I hit ok, it will not tell my remove code to work. SO this is getting the complete opposite results now. I alerted "comp" in the JS function and it does alert the companyID but I guess trying to pass a query string using

Code:

document.location(\"../main/company.php?delete=comp\");
does not work because my code

Code:

if (!empty($_GET["delete"]))
echo "what";

is not executing.

any ideas?

thanks again for your help

bathurst_guy 01-31-2006 09:32 PM

PHP Code:

<input type="button" onClick="return confirm('Are you sure?');" name="Delete" value="<?=$row['ID']?>">

Then for the php say
PHP Code:

if(isset($_POST[Delete])){
  
$sql = mysql_query("DELETE FROM table WHERE id = {$_POST[Delete]} LIMIT 1");
#.....check to see if it was deleted and confirm or error.....
}


tripwater 02-01-2006 10:58 AM

THanks for the reply...I am not sure what is going on now but I am getting the confirm box but no echo on the ok of the confirm box.


I added a form tag around the section that displys the current companies, added the code for the button like you posted and changed my php code to look for the post vars of the delete and I get no echo. Plus this solution has the value in the button set to the companyID which the buttons needs to say "Delete" not 4 or 2. So I am not sure I can do this...

I need for the button or link to say "Delete" but pass the ID value to the same page when clicked.

Here is my code now

Code:

//PHP code at the top of my page
if (isset($HTTP_POST_VARS["delete"]))               
echo "what";



Code:

//form code

<form name=comp_mod method=post action=\"../main/company.php\">
<table width=100% align=center border=0>
        <tr>
                <td align=center>
                        <b>Current Companies</b><br>
                        <span style=background-color:Yellow>If you delete a company, all of the departments as well as employees
                        associated with the company will also be removed.</span>
                </td>
        </tr>
</table>

<table align=center border=1 width=600>
        <tr>
                <td align=center nowrap=nowrap width=200>
                        <b>Action</b>
                </td>
                <td align=center nowrap=nowrap>
                        <b>Name</b>
                </td>
        </tr>";



for ($i = 0; $i < @mysql_num_rows($result); $i++)
{
$row        = @mysql_fetch_array($result);

$value .= "<tr>                                       
        <td width=200 align=center>
                <a href='../main/company.php?id=".$row["ID"]."'>Edit</a>
                &nbsp;&nbsp;&nbsp;
               
                <input type=button  name=delete onClick=\"return confirm('You are about to remove a company and all associated departments, employees and events. This cannot be undone.');\" value=".$row["ID"].">
               

                &nbsp;&nbsp;&nbsp;
        </td>
        <td>
                &nbsp;&nbsp;&nbsp;".$row["Name"]."                                                                                               
        </td>
</tr>";

}                                               
$value .= "</table></form>


Thank you again for your help


All times are GMT -5. The time now is 11:56 PM.

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.