Jump to content

Using Java to confirm a PHP Command


stublackett

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

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
         						
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.