views:

450

answers:

3

i have tried:

<?php include("delete.php") ?>
<?php 

   ....
   ....
   ....

if($result=mysql_query($sql))
                {

                    echo "<table><th>Id</th><th>Name</th><th>Description</th><th>Unit Price</th>";
                    while($row = mysql_fetch_array($result))
                    {
                        echo "<tr><td>".$row['Id']."</td><td>".$row['Name']."</td><td>".$row['Description']."</td><td>".$row['UnitPrice']."</td> 
                        <td><a href='delproduct($row[Id])' onclick = 'return MsgOkCancel()'>Delete</a></td></tr>";
                        echo "<br/>";
                    }
                }
?>

following javascript is in the same page:

<script type="text/javascript" language="javascript">
            function MsgOkCancel() {
                                    if (confirm("Are You Sure You Want to Delete?"))
                                     { return true }
                                    else
                                    {return false}
                                   }
        </script> 

where delproduct is a javascript function in delete.php written like:

<script type="javascript">
function delproduct(Id)
{
    alert('Id '+ Id);
}
<script>

** after ** clicking Delete a okcancel message-box appear asking conformation

** but ** after clicking 'ok' it should execute statements inside delproduct function but it doesn't

it gives error like:

Object Not Found :The requested URL was not found on this server.

what would be the problem?

pls help,

thanks

A: 

What about this one: PHP:

<a href="javascript:void(0);" onclick=\"delproduct({$row[Id]})\">

JS:

function delproduct(Id){
    if(MsgOkCancel()) alert('Id '+ Id);
}
Stefan K
`javascript:void(0)` is evil and your PHP has syntax errors in it.
David Dorward
@David: I know this is considered 'evil', but do you know why exactly? I never really understood why (or I forgot).
fireeyedboy
It isn't progressive. It isn't unobtrusive. It doesn't give a clue as to what the link does in the status bar.
David Dorward
+3  A: 

A URI without a scheme (such as http:) is treated as a relative URI.

You appear to be looking for javascript: (which should never be used for anything other than creating bookmarklets).

What you should be doing is something along the lines of:

onclick="if (MsgOkCancel()) { delproduct($row[Id]); return false; } else {  return false; }"

However, you should have something that works in the href, but since this appears to be making a significant change on the server, you should be using POST not GET, so a link is the wrong tool.

What you probably should have is:

<form action="/delete" method="post" onsubmit="return delete(this);">
    <input type="hidden" name="id" value="<?php echo htmlspecialchars($row[Id]); ?>">
    <input type="submit" value="Delete">
</form>

Combined with:

function delete(form) {
    if (confirm("Are You Sure You Want to Delete?")) {
        delproduct(form.elements.id.value);
    }
    return false;
}

Better yet, get rid of the onsubmit attribute and assign the event using JavaScript.

David Dorward
+1 Especially for the progressive enhancement tip and the POST advice.
fireeyedboy
A: 

I think you need a different setup.

First of all, if you are going to call javascript functions in an href attribute, you need to prepend it with javascript: like so href="javascript:delproduct(...)". But calling javascript from an href attribute is not recommended. That attribute is intended for urls.

I would advice you to create a function that displays the messagebox and based on the action of the user, calls the delproduct function. Something like:

function confirmDelProduct( id )
{
    if( msgOkCancel() )
    {
        delproduct( id );
    }
    // return false is meant to stop the href url from being called
    return false;
}

And in your html:

<a href="#" onclick="return confirmDelProduct(' . $row[ 'id' ] . ')"> ... etc
fireeyedboy