Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Pretty simple confirm box - Or so I thought?!
Old 12-12-2008, 03:43 PM Pretty simple confirm box - Or so I thought?!
Novice Talker

Posts: 9
Trades: 0
Not sure what I've done wrong here but this box isn't working:

Code:
function confirmBox() {
var r=confirm("Are you sure you want to delete this event?");

if (r==true)
{     
document.location.href='adminrequests.php?result=true';

}
else
{         
document.location.href='adminrequests.php?result=false';

} 
}
I've changed the parts in the if statements to create alert boxes with the r variables value and that works, so I've pretty much whittled it down to the "document.location.href='adminrequests.php?result= false';" bit not working.
But I have no idea why? Is there something wrong with the syntax?

Any input would be much appreciated. Thanks.
Eminee is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-12-2008, 03:56 PM Re: Pretty simple confirm box - Or so I thought?!
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Code:
window.location.href =
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-12-2008, 04:12 PM Re: Pretty simple confirm box - Or so I thought?!
Novice Talker

Posts: 9
Trades: 0
Thanks Chris, just tried your suggestion but no joy...
Eminee is offline
Reply With Quote
View Public Profile
 
Old 12-12-2008, 04:21 PM Re: Pretty simple confirm box - Or so I thought?!
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Ok. What actually is happening? or not as the case maybe.

does the php page get called?
do the parameters get passed?
does the php code get the parameters?
does the script fail with an error?

etc etc.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-12-2008, 05:14 PM Re: Pretty simple confirm box - Or so I thought?!
Novice Talker

Posts: 9
Trades: 0
There are no errors, the javascript gets called by an onclick button and gets as far as determining whether the user has clicked ok or cancel, the only thing it doesn't do is change the URL... it just reloads the page.
Eminee is offline
Reply With Quote
View Public Profile
 
Old 12-12-2008, 05:49 PM Re: Pretty simple confirm box - Or so I thought?!
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
I take it's the same page then.

is your PHP code using $_POST to grab the values?
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-13-2008, 08:48 AM Re: Pretty simple confirm box - Or so I thought?!
Sleeping Troll's Avatar
Ultra Talker

Posts: 351
Name: Butch Begy
Trades: 0
Yes, it would help to see the 'adminrequests.php' code.
Sleeping Troll is offline
Reply With Quote
View Public Profile
 
Old 12-13-2008, 03:07 PM Re: Pretty simple confirm box - Or so I thought?!
Novice Talker

Posts: 9
Trades: 0
I figured the code from admin requests wouldn't be needed because the url isn't even changing to 'adminrequests.php?result=true' so I assumed it was something wrong with the javascript...

but this is the bit that grabs the values.. I used GET because I've been taught that its the proper way to get the value from the URL??

PHP Code:
$r=$_GET['result'];

if(isset(
$_POST['deleteButton'])) {
  if(
$r==true) {
    
$bookingid=$_POST['bookingid'];
    
$sql="DELETE FROM booking WHERE bookingID=".$bookingid."";
    
$result mysql_query($sql) or die(mysql_error());
    echo 
"<p>Booking ".$_POST['bookingid']." has been deleted";
  }

Thanks for your help ^_^
Eminee is offline
Reply With Quote
View Public Profile
 
Old 12-13-2008, 03:55 PM Re: Pretty simple confirm box - Or so I thought?!
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Sure, GET is the method to collect information from querystrings.

However when your code then promptly goes on to use to use POST ($_POST['bookingid']) in an attempt to get the record to delete, when you haven't actually sent any form values, it's a little less than surprising that nothing happens

You have to send ALL the values in the querystring when requesting an action via the URI.
The other vitally important point is to ABSOLUTELY NOT use the $_GET values directly in your query.
You MUST "sanitise" the values to prevent SQL injection attacks, if your code expects an integer value ALWAYS cast the retrieved value into an integer type before using it in a query, ALWAYS clean up string values to remove SQL commands that could be injected into the query by crackers "doctoring" URIs to find weaknesses in systems.
read through http://www.webmaster-talk.com/php-fo...injection.html for more.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-13-2008, 04:22 PM Re: Pretty simple confirm box - Or so I thought?!
Novice Talker

Posts: 9
Trades: 0
Okay, correct me if I'm totally wrong because I'm a bit confused now.. but my variable bookingid is a hidden field in a form containing the button (which the javascript function is run from onclick). The field is defined directly from an sql select statement from my database.. so I assumed it was safe from crackers and such?? Pasted in the part of the code with the form in just to show you.. sorry its a bit messy at the moment.


PHP Code:

<table width=100%><tr>
<th>ID</th><th width=75px>Event</th><th width=200px>Description</th><th>Date</th><th>Time</th><th>Contact Name</th><th>E-mail</th><th>Tel No.</th><th>Operations</th></tr>
<?php
$sql 
'SELECT * FROM booking WHERE accepted=0';
$result=mysql_query($sql) or die(mysql_error());
while(
$row mysql_fetch_array($result))
{
echo 
"<tr><td>".$row['bookingID']."</td><td>".$row['name']."</td><td>".$row['description']."</td><td>".$row['d']."/".$row['m']."/".$row['y']."</td><td>".$row['timeSlot']."</td><td>".$row['contactName']."</td><td><a href=mailto:".$row['contactEmail'].">Email Contact</a></td><td>".$row['contactTel']."</td><td>";
echo 
"<form action='adminrequests.php' method='post'><input type='submit' name='confirmButton' value='Confirm'><input type='submit' name='editButton' value='Edit'><input type='submit' name='deleteButton' value='Delete' onclick='confirmBox()'><input type='hidden' name='bookingid' value=".$row['bookingID']."></form>";        
}
?>                        
</table>
Can I not use POST & GET at the same time? Is the fact that the url does not change at all not significant?
It may help to tell you that before I added the if statement with $_GET['result']; in, it worked fine and I could delete the entry. I just wanted a confirm box on it.
Eminee is offline
Reply With Quote
View Public Profile
 
Old 12-13-2008, 04:48 PM Re: Pretty simple confirm box - Or so I thought?!
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Can I not use POST & GET at the same time? Is the fact that the url does not change at all not significant?
You can, BUT if a form is not actually submitted POST will be empty.

to send querystring values and form values at the same time the querystring parameters go in the form action attribute and on confirmation the script will have to call the form submit method (document.formname.submit() ) instead of setting the document location.
.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-13-2008, 05:25 PM Re: Pretty simple confirm box - Or so I thought?!
Novice Talker

Posts: 9
Trades: 0
Oh I see so you are saying that I'm basically directing my browsers to two places at once? Sorry, I think I understand you now!

So how can I get round this without using a url to get the true or false value? Hmm >.<
Eminee is offline
Reply With Quote
View Public Profile
 
Old 12-13-2008, 06:08 PM Re: Pretty simple confirm box - Or so I thought?!
Novice Talker

Posts: 9
Trades: 0
Hmm I understand that I can return true or false with the javascript function but I'm not sure how I can call that value from the button further down the page...because its not going to be $_POST['onclick']; or anything silly is it. :S
Eminee is offline
Reply With Quote
View Public Profile
 
Old 12-13-2008, 07:20 PM Re: Pretty simple confirm box - Or so I thought?!
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
You only submit the form when the confirmation comes back as true, if it's false you do nothing or reload the page.

set the form action attribute to "adminrequests.php?result=true" then $_GET["result"] will be valid and so all the other form fields will have a value.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-14-2008, 11:14 AM Re: Pretty simple confirm box - Or so I thought?!
Novice Talker

Posts: 9
Trades: 0
Right so.. I understand what you are describing I think and I changed the url of the form so that result can be used. When I click delete this works but the problem I have now is that even if I press cancel on the confirm box it still deletes the entry.

I figured the only thing I'd need to do in the javascript now is return true or false, because the understanding I have is that the form wouldn't submit if it returned false??

Code:
function confirmBox() {
var r=confirm("Are you sure you want to delete this entry?");
if (r==true)
  {
  return true;
  }
else
  {
  return false;
  }
}
so this is what I've got in the javascript now.
The only problem is now that the function seems to break after the confirm box because if I put alert boxes to show the value of r at any time after the confirm box, it doesn't work. Do you have any idea why?
Getting so frustrated that something that seemed so simple is causing me so much trouble!
Eminee is offline
Reply With Quote
View Public Profile
 
Old 12-14-2008, 05:48 PM Re: Pretty simple confirm box - Or so I thought?!
Novice Talker

Posts: 9
Trades: 0
Okay so I restructured the javascript function to:

Code:
function confirmBox() {

if (confirm("Are you sure you want to delete this entry?")==true)
  {
  return true;
  }
else
  {
  return false;
  }
}
and now it works! Thanks so much for your help.

Typically I've come accross a new problem now though, don't suppose you've ever heard of a javascript function only working when theres an alert box in it have you?
Eminee is offline
Reply With Quote
View Public Profile
 
Old 12-15-2008, 03:56 AM Re: Pretty simple confirm box - Or so I thought?!
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Code:
if (confirm("Are you sure you want to delete this entry?")==true) 
{
  document.formname.submit()
  }
else
  {
  window.reload()
  }
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Pretty simple confirm box - Or so I thought?!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.59674 seconds with 12 queries