Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > Client Side Scripting
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 08-09-2006, 11:33 PM
  #1
scatteredbomb
Myrmidon (Level 12)
 
Join Date: Dec 2004
Location: New Mexico, USA
Posts: 176
iTrader: (0)
scatteredbomb is on a distinguished road
using JavaScript confirm box with php

I have a list of links in my admin panel. The links are filled in through php and mysql.

Here's the basic part of the code in the table

Code:
$query = "SELECT * FROM tracking ORDER BY clicks DESC"; 
$result = mysql_query($query); 
?>

<p>
<table border="0" style="border: 1px solid white; color: white; font-size: 10pt;" bgcolor="black" width="100%" cellspacing="0" cellpadding="0">
	<tr>
		<td width="43" align="center">ID</td>
		<td width="44" align="center">URL</td>
		<td width="225" align="center">Description</td>
		<td width="60" align="center">Clicks</td>
		
		<td width="59" align="center">Edit</td>
		
		<td width="59" align="center">Delete</td>
	</tr>
<?	
$rowcolor = "000000";
while ($row = mysql_fetch_array($result)) {  ?>
*i put the javascript code here*
<?
if ($rowcolor == "000000"){
$rowcolor = "222222";
} else {
$rowcolor = "000000";
} ?>
	<tr style="background-color: <? echo $rowcolor; ?>">
		<td align="center" width="43" bgcolor="222222"><? echo $row['id']; ?></td>
		<td width="44" align="center" ><a href="<? echo $row['url']; ?>" class="news" target="_blank">link</a></td>
		<td width="225" align="center"><? echo $row['descript']; ?></td>
		<td width="60" align="center" bordercolor="black"><a href="links.php?act=trackreset&id=<? echo $row['id']; ?>" class="submain"><? echo $row['clicks']; ?></a></td>
		
		<td width="59" align="center"><a href="links.php?act=trackdetails&id=<? echo $row['id']; ?>" class="news">edit</a></td>
		
		<td width="59" align="center"> <a href="javascript:confirmation()">delete</a> </td>
	</tr>
	<? } ?>
</table>

<? } ?>
Alright, you'll see the last column is for "delete". What i'd like is for a javascript alert box to come up when that link is clicked saying "Delete entry #? " I would like the entry number to replace that #. So i don't really do too much with Javascript so I'm not to sure how to use it. But I found this little batch of code to do a confirm box.

Code:
<script type="text/javascript">
<!--
function confirmation() {
	var answer = confirm("Delete entry <? echo $row['id']; ?> ?")
	if (answer){
		alert("Entry Deleted")
		window.location = "links.php?act=trackdelete&id=<? echo $row['id']; ?>";
	}
	else{
		alert("No action taken")
	}
}
//-->
</script>
Alright, so I've edited that code a with a couple php statements to get the ID number to show up there (those in red). But that didn't work because each time I click the link it doesn't bring up the right ID number. It brings up the latest entry, so if I have 12 entries, no matter which delete link i click, it will always say "Delete entry 12? ". I tried moving the entire javascript function underneath the while statement (where i left the green comment for this post), but that still didn't work.

Can anyone tell me why thats happening? What do i need to do to make that javascript work within the while loop so each confirmation() javascript box will carry that entries individual number.
__________________
My Site - MySpace - MyBlog
scatteredbomb is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-11-2006, 04:37 PM
  #2
rockford
Novice (Level 1)
 
Join Date: Sep 2006
Posts: 1
iTrader: (0)
rockford is an unknown quantity at this point
Returning the right id

Hi!

I'm working on a similar code, only I'm using an Oracle database, and I also came accross this very same issue. Here's how I solved the problem:

I put a form inside the while statement and assigned the entry number to a hidden parameter. The button in the first column submits the form to the page that I've set in the action prameter:

$s = OCIParse($c, "select * from person order by id");
OCIExecute($s, OCI_DEFAULT);
while (OCIFetch($s)) {
echo "
<form method='post' action='show_persons.php'>
<input type='hidden' name='person_id' value=" . ociresult($s, "ID") . ">
<tr>
<td width=40 align=center>
<input type='image' src='btn_delete.gif' name='btn_delete' title='Delete' border='0' width='11' height='11'>&nbsp;
</td>
<td width=40 align=center>" . ociresult($s, "ID") . "</td>
<td width=90>" . ociresult($s, "NAME") . "</td>
</tr>
</form>";
}


(At first I put the form tag outside the while statement, but this only resulted in the problem you're describing, because the person_id was assigned a new value each time the while statement produced a new row.)
rockford is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-11-2006, 06:49 PM
  #3
scoutt
Mister Admin to you
 
scoutt's Avatar
 
Join Date: Jul 2001
Posts: 33,142
iTrader: (0)
scoutt is a glorious beacon of lightscoutt is a glorious beacon of lightscoutt is a glorious beacon of lightscoutt is a glorious beacon of lightscoutt is a glorious beacon of light
scatteredbomb, what you need to do has nothing to do with php, well it does but not how you are doing it.

What you want to do it send the id to the javascript, no form with hidden elements needed. Do this

<a href="javascript:confirmation(<? echo $row['id']; ?>)">delete</a>

Then in the javascript, which is NOT in the php code but in the header with all the other javascript will be like this

Code:
<script type="text/javascript">
<!--
function confirmation(ID) {
	var answer = confirm("Delete entry "+ID+" ?")
	if (answer){
		alert("Entry Deleted")
		window.location = "links.php?act=trackdelete&id="+ID;
	}
	else{
		alert("No action taken")
	}
}
//-->
</script>
but, wouldn't you want the name of the object you are deleteing iunstead of the ID? I mean you could send both and use the id but also use the name to show the user.
__________________
Have a Script or Snippet you want to share?

WWW Standards: HTML 4.01,
HTML 5, CSS2.1, CSS3, XHTML 1.0
PHP Standards: PHP Standards
scoutt is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-14-2006, 01:45 AM
  #4
scatteredbomb
Myrmidon (Level 12)
 
Join Date: Dec 2004
Location: New Mexico, USA
Posts: 176
iTrader: (0)
scatteredbomb is on a distinguished road
Thanks for the info scoutt. How would send both the ID and name?
__________________
My Site - MySpace - MyBlog
scatteredbomb is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-14-2006, 07:44 AM
  #5
scoutt
Mister Admin to you
 
scoutt's Avatar
 
Join Date: Jul 2001
Posts: 33,142
iTrader: (0)
scoutt is a glorious beacon of lightscoutt is a glorious beacon of lightscoutt is a glorious beacon of lightscoutt is a glorious beacon of lightscoutt is a glorious beacon of light
easy,

the link would be this:

<a href="javascript:confirmation(<? echo $row['id'].",'{$row['name']}'"; ?>)">delete</a>

and the javascript would be this
Code:
<script type="text/javascript">
<!--
function confirmation(ID,Name) {
	var answer = confirm("Delete entry "+Name+" ?")
	if (answer){
		alert("Entry Deleted")
		window.location = "links.php?act=trackdelete&id="+ID;
	}
	else{
		alert("No action taken")
	}
}
//-->
</script>
__________________
Have a Script or Snippet you want to share?

WWW Standards: HTML 4.01,
HTML 5, CSS2.1, CSS3, XHTML 1.0
PHP Standards: PHP Standards
scoutt is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-15-2006, 11:12 AM
  #6
scatteredbomb
Myrmidon (Level 12)
 
Join Date: Dec 2004
Location: New Mexico, USA
Posts: 176
iTrader: (0)
scatteredbomb is on a distinguished road
Awesome! Thanks so much. I'm still pretty new to JavaScript, thats why i wasn't sure what (or how many) variables could be passed through ' ( ) ' . Thanks again.
__________________
My Site - MySpace - MyBlog
scatteredbomb is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-19-2008, 08:12 AM
  #7
bishounen
Novice (Level 1)
 
Join Date: Sep 2008
Posts: 2
iTrader: (0)
bishounen is an unknown quantity at this point
Scoutt, that is a great solution, thanks! It was more or less the only page i was able to google on the subject (combine javascript + php). I had the exact same problem, wanted to prompt a confirmation to delete an entry from the database.

I kind of went nuts though over why your code didn't work at first as it looked so solid, but it kept messing up in both IE and Firefox. Then i changed <a href=""> into <a href=> (without the quotes) and it worked fine.

Because of the "" i guess the php code didn't get parsed becase the link would literally be:

javascript:confirmation(<? echo $row['id']; ?>)

Anyway, now it works great, thanks a lot!

For everyone visiting this page by googling the same problem I did: remove the " ", apparently this hinders the php code in some cases.
bishounen is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-19-2008, 08:45 AM
  #8
bishounen
Novice (Level 1)
 
Join Date: Sep 2008
Posts: 2
iTrader: (0)
bishounen is an unknown quantity at this point
I know this thread is like two years old but I'm just gonna post this question anyway. The code above for sending two values doesn't work but I can't get it to work either and I've been at it for like half an hour.

The best I came up with is this:

<a href=javascript:confirmation(<?php echo $row['personID'].",'{$row['Name']}'"; ?>)>

And no matter what I do it wo't work. The problem is that a name is of course usually two Words, like Wilco Lensink, and after the first the code stops so what i get is:

javascript:confirmation(50,'Wilco

Does anyone know how to work around this cause it's driving me crazy. Not that sending the name is so important but it would be nice if it COULD be done.. :|

Thanks in advance!
bishounen is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-19-2008, 09:03 AM
  #9
scoutt
Mister Admin to you
 
scoutt's Avatar
 
Join Date: Jul 2001
Posts: 33,142
iTrader: (0)
scoutt is a glorious beacon of lightscoutt is a glorious beacon of lightscoutt is a glorious beacon of lightscoutt is a glorious beacon of lightscoutt is a glorious beacon of light
you are missing quotes and you also want it like this

<a href="javascript:confirmation(<?php echo $row['personID'].",'{$row['Name']}'"; ?>)">
__________________
Have a Script or Snippet you want to share?

WWW Standards: HTML 4.01,
HTML 5, CSS2.1, CSS3, XHTML 1.0
PHP Standards: PHP Standards
scoutt is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-19-2008, 09:26 AM
  #10
<head>&nbsp;</head>
 
's Avatar
 
Join Date: Jul 2004
Posts: 3,640
iTrader: (1)
 has much to be proud of has much to be proud of has much to be proud of has much to be proud of has much to be proud of has much to be proud of has much to be proud of has much to be proud of
How about a version that degrades and doesn't need a separate js function ?

PHP Code:
echo '<a href="delete.php?personID='.$row['personID'].'&amp;name='.$row['Name'].'" onclick="return !confirm(\'are you sure you wish to delete this record\');">delete</a>'
__________________
I may have opened the door, but you entered of your own free will
urls removed by choice
 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 02-14-2010, 07:52 PM
  #11
amansingh630
Novice (Level 1)
 
Join Date: Feb 2010
Posts: 1
iTrader: (0)
amansingh630 is an unknown quantity at this point
Re this script

Hi, we made same script which on this page work fine, but how we can set the function to pick the ID wich we click by mouse,


Any help will be appricate,
amansingh630 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-30-2010, 06:31 AM
  #12
sherrischmidt75
Novice (Level 1)
 
Join Date: Sep 2010
Posts: 3
iTrader: (0)
sherrischmidt75 is an unknown quantity at this point
how can i make php alert pop ups

I have a php page with two hyperlinks. I one to send email to certain addresses and the other to also send emails to another address and also delete their records from the database. I don't want a case where the user accidentally clicks on the link takes the wrong action.

i thought about javascript confirm function. so i tried combining the javascript confirm function and php but i don't seem to get the two working. i need the results from the confirm function which is either true or false so i can take appropriate action.

Is there an alternative?does php provide similar alert functions? i really need help.
sherrischmidt75 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-30-2010, 07:13 AM
  #13
scoutt
Mister Admin to you
 
scoutt's Avatar
 
Join Date: Jul 2001
Posts: 33,142
iTrader: (0)
scoutt is a glorious beacon of lightscoutt is a glorious beacon of lightscoutt is a glorious beacon of lightscoutt is a glorious beacon of lightscoutt is a glorious beacon of light
sherrischmidt75, please start your own post and we will help you a lot faster. Please do not hijack somebody else "old" thread.

php does not have alert functions, you have to use javascript.
__________________
Have a Script or Snippet you want to share?

WWW Standards: HTML 4.01,
HTML 5, CSS2.1, CSS3, XHTML 1.0
PHP Standards: PHP Standards
scoutt is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-07-2010, 03:32 PM
  #14
stephanielarry
Aspirant (Level 2)
 
Join Date: Oct 2010
Location: California
Posts: 17
iTrader: (0)
stephanielarry is an unknown quantity at this point
I have a php page with two hyperlinks. I'm one to send emails to specific addresses and others to send emails to another address, and also delete their records from the database. I do not want a case where the user accidentally clicks the link is wrong.
stephanielarry is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-30-2011, 10:15 AM
  #15
Mr_Purist
Novice (Level 1)
 
Join Date: Sep 2011
Posts: 1
iTrader: (0)
Mr_Purist is an unknown quantity at this point
Scott, you're my man !

I've been googling for about an hours or more for a solution like this one.. Nice and clean and robust.. Two thumbs up.. Can you believe I've registered to this forum just to tell you this ??
Quote:
Originally Posted by scoutt View Post
easy,

the link would be this:

<a href="javascript:confirmation(<? echo $row['id'].",'{$row['name']}'"; ?>)">delete</a>

and the javascript would be this
Code:
<script type="text/javascript">
<!--
function confirmation(ID,Name) {
	var answer = confirm("Delete entry "+Name+" ?")
	if (answer){
		alert("Entry Deleted")
		window.location = "links.php?act=trackdelete&id="+ID;
	}
	else{
		alert("No action taken")
	}
}
//-->
</script>
Mr_Purist is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Reply
KEEP TABS
SPONSORS
 
Boxedart

 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 02:55 AM.

   

Mascot team created by Drawshop.com | Web Hosting

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.