stublackett Posted June 20, 2008 Share Posted June 20, 2008 Hi, Not quite sure where to put this I've got a piece of JavaScript to prompt the User "Delete This Item" With the options OK & CANCEL I've got the link echo'ed out in the PHP, But how do I get the JavaScript window to prompt whilst being echoe'd in the PHP?? The code I tried was : echo " || <A HREF='javascript:onClick=alert("Not an active link")\"delete_news.php?id=$id\">Delete News Item</a>"; But thats erroring out So how do I actually get the PHP to echo the JavaScript Alert? The JavaScript is just : <A HREF='javascript:onClick=alert("Not an active link")\"delete_news.php?id=$id\">Delete News Item</a> Which I'm going to modify to match Quote Link to comment Share on other sites More sharing options...
lemmin Posted June 20, 2008 Share Posted June 20, 2008 If you echo just the javascript it will show the message as it is parsed in the source. echo "<script language=\"Javascript\">alert("Not an active link");</script>"; If you are trying to do more of what your code implies, you probably want something like this: echo " || <A HREF=\"javascript://\" onClick=\"alert('Not an active link');self.location='delete_news.php?id=$id';\">Delete News Item</a>"; That will send the alert message and, upon clicking ok, redirect to your delete_news.php file. It doesn't really make sense with the message you have in the alert, but that is what it looks like you want. Quote Link to comment Share on other sites More sharing options...
stublackett Posted June 20, 2008 Author Share Posted June 20, 2008 Thanks Lemmin With regards to the Error message, I'm going to be changing that Quote Link to comment Share on other sites More sharing options...
stublackett Posted June 23, 2008 Author Share Posted June 23, 2008 Just a followup to this..... I've created some JavaScript, Which is located in the <head> of the document, How do I stop it from loading? and get it to load when the user clicks the link : delete_news.php?id=$id JavaScript Code is : <script type="text/javascript"> <!-- var answer = confirm ("Delete This News Item?") if (!answer) window.location="delete_news.php?id=$id" // --> </script> and I'm using the PHP Code of : <?php $result = mysql_query("SELECT * FROM $db_table1"); while($myrow = mysql_fetch_assoc($result)) {//begin of loop $title = $myrow['title']; $description = $myrow['description']; $id = $myrow['id']; //now print the results: echo "<h2>$title</h2>"; echo "<div id='description'>$description</div>"; echo "<br>"; echo "<hr>"; // Now print the options to (Read,Edit & Delete the news) echo "<b>Modification Options...</b>"; echo "<br><br>"; echo "<a href=\"editnews.php?id=$id\">Edit News Item</a>"; echo " || <a href=\"javascript://\" onClick=\"alert('Delete This News Item?');self.location='delete_news.php?id=$id';\">Delete News Item</a>"; echo "<hr>"; }//End While loop ?> Somewhere I need to edit that <a href=\"javascript://\"> so that it pairs to the confirmation box I have created in the HEAD of the document Thanks in advance Quote Link to comment Share on other sites More sharing options...
waynew Posted June 23, 2008 Share Posted June 23, 2008 Java and JavaScript are two completely different languages. Quote Link to comment Share on other sites More sharing options...
stublackett Posted June 23, 2008 Author Share Posted June 23, 2008 Thanks for that ??? Granted my topic title says Java and the script involved is JavaScript As you can see from my post there are numerous mentions of JAVASCRIPT....... Quote Link to comment Share on other sites More sharing options...
waynew Posted June 23, 2008 Share Posted June 23, 2008 Sorry, didn't mean to seem like such an ass. It's just that people could read the title, say to themselves "Hey, I know Java" and then find out that you were talking about JavaScript. It's kind of like somebody saying "Hey I have a PHP problem," when really they have an asp problem. Quote Link to comment Share on other sites More sharing options...
lemmin Posted June 23, 2008 Share Posted June 23, 2008 You just make it into a function and call it when the user clicks the link. <script type="text/javascript"> <!-- function deleteIt() { var answer = confirm ("Delete This News Item?") if (!answer) window.location="delete_news.php?id=$id" } // --> </script> echo " || <a href=\"javascript://\" onClick=\"deleteIt()\">Delete News Item</a>"; Quote Link to comment Share on other sites More sharing options...
stublackett Posted June 23, 2008 Author Share Posted June 23, 2008 Thanks Lemmin, That Prompt works fine and dandy My next issue though is that $id is not cascading out to the URL Link, Any ideas why?? Key code areas are : <script type="text/javascript"> <!-- function deleteitem() { var answer = confirm ("Delete This News Item?") if (!answer) self.location="delete_news.php?id=$id" } // --> </script> <?php $result = mysql_query("SELECT * FROM $db_table1"); while($myrow = mysql_fetch_assoc($result)) {//begin of loop $title = $myrow['title']; $description = $myrow['description']; $id = $myrow['id']; //now print the results: echo "<h2>$title</h2>"; echo "<div id='description'>$description</div>"; echo "<br>"; echo "<hr>"; // Now print the options to (Read,Edit & Delete the news) echo "<b>Modification Options...</b>"; echo "<br><br>"; echo "<a href=\"editnews.php?id=$id\">Edit News Item</a>"; echo " || <a href=\"javascript://\" onClick=\"deleteitem()\">Delete News Item</a>"; echo "<hr>"; }//End While loop ?> Quote Link to comment Share on other sites More sharing options...
lemmin Posted June 23, 2008 Share Posted June 23, 2008 I'm not sure what you mean. Do you mean that this line: echo "<a href=\"editnews.php?id=$id\">Edit News Item</a>"; is generating something like this: <a href="editnews.php?id=">Edit News Item</a> ? If so, I would assume that your id fields are null. Quote Link to comment Share on other sites More sharing options...
stublackett Posted June 24, 2008 Author Share Posted June 24, 2008 I mean the line of code in the JavaScript self.location="delete_news.php?id=$id" The id=$id is not being picked up But on the line edit_news.php?id=$id is actually picking that URL up If you confirm the deletion of the item the URL goes to : /delete_news.php?id=$id Obviously that $id needs to be from the DB i.e id=1 So that when the delete script is ran it knows that the ID of the item to delete is 1 Hope that makes sense... Quote Link to comment Share on other sites More sharing options...
lemmin Posted June 24, 2008 Share Posted June 24, 2008 Are you generating the Javascript with PHP or just hardcoding it in? The $id variable is in your PHP code so you have to print it out from the PHP. Assuming that you hardcoded it, you probably want to change it to something like this: <script type="text/javascript"> <!-- function deleteitem() { var answer = confirm ("Delete This News Item?") if (!answer) self.location="delete_news.php?id=<?echo $id ?>" } // --> </script> And, of course, you have to have the $id variable set before you print the Javascript part. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.