0

I am having a problem with onclick function integrating it with php .Can someone please help

Below is my codes

<html>
<head>
<title></title>
<script language="Javascript">
function delete()
{
    val del=confirm("Do you wanto delete the player?");
    if(del==true)
    {
        alert("player Deleted");
    }
    else
    {
        alert("record not deleted");
    }
    return del;
}
</script>
</head>
<?php 
Echo “
<a href='delete.php?player_id=$player_id' onclick='delete()'>Delete</a>”
?>
3
  • 1
    @Lee Taylor edit is appreciated Commented Apr 28, 2013 at 19:20
  • What exactly is your problem? Is your function not executed when klicking on the link? Commented Apr 28, 2013 at 19:23
  • @akluth yes it does not Commented Apr 28, 2013 at 19:29

2 Answers 2

3

the most important thing: do not use the javascript keyword delete (JavaScript delete operator help) as function name! Your function (and thus onclick) won't work because of this! Change it to something appropriate like deleteRecord and you could use php only to output the ID, like:

<html>
    <head>
        <title></title>
        <script type="text/javascript">
            function deleteRecord()
            {
                if (confirm("Do you wanto delete the player?")) {
                    alert("player Deleted");
                }
                else {
                    alert("Record not deleted");
                }
            }
        </script>
    </head>
    <body>
        <a href="delete.php?player_id=<?php echo $player_id ?>" onclick='deleteRecord();'>Delete record?</a>
    </body>
</html>

kind regards, P.

Sign up to request clarification or add additional context in comments.

5 Comments

This worked Like a charm so now how can I stop it from goin to the php page for delete ? It does everything but also it goes to the delete page wat I do not Like
it still deletes even if u cancel
You could use for example jQuery to prevent the link: import jQuery (for example directly from google CDN) and then use event.preventDefault() in the else part. More info about preventDefault can be found here. Example: import jQuery: <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> code modification: else { alert("Record not deleted"); event.preventDefault(); }
oK BUT THE SCHOOL REQUIREMENTS SAID JAVA SCRIPT STRICTLY
In this case use PHP more extensively and maybe change the complete flow and outlook of the page.
2

I found a pretty good solution. However, It adds a little extra to your code. I do not recommend using this method too much. I am a new PHP developer, so there are probably somethings this method can and cannot do.

also I cannot seem to prevent this method from loading a new page, so the best way is to add your php to the beginning of the file. This is also a better solution for using AJAX (my opinion)

if the current file is index.php...

make a form tag element


'<form action="index.php" method="post">'

'<input type="submit" name="helloWorld" value="hello World">'

'</form>'

here you will display the content of what you are trying for, with a click


'<article>'

    '<?php echo myFunction(); ?>'

'</article>'

this next php markup can go anywhere as long as it it is in the same file... Unless you know how to work around. Best place is before the HTML.


'<?php '
    'function myFunciton() {'
       'if (isset($_POST[helloWorld])) {'
          'echo "hello world"; ... your methods'
       '}'
     '}'

or you could directly access it without using a function

<body>
    <form action="index.php" method="post">
       <input type="submit" name="hello" value="hello">
       <input type="submit" name="goodBye" value="goodBye">
    </form>
    <article>
       <?php
           if (isset($_POST[hello])) {
               echo "This happens if you click hello";
           }
           if (isset($_POST[goodBye])) {
               echo "This happens if you click goodBye";
           }
        ?>
     </article>
</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.