admin管理员组

文章数量:1295117

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

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>”
?>
Share Improve this question edited Apr 28, 2013 at 19:14 Lee Taylor 7,99416 gold badges37 silver badges53 bronze badges asked Apr 28, 2013 at 19:13 HumphreyHumphrey 2,8173 gold badges30 silver badges39 bronze badges 3
  • 1 @Lee Taylor edit is appreciated – Humphrey Commented Apr 28, 2013 at 19:20
  • What exactly is your problem? Is your function not executed when klicking on the link? – akluth Commented Apr 28, 2013 at 19:23
  • @akluth yes it does not – Humphrey Commented Apr 28, 2013 at 19:29
Add a ment  | 

2 Answers 2

Reset to default 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.

I found a pretty good solution. However, It adds a little extra to your code. I do not remend 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>

本文标签: How can I integrate javascript onclick function with phpStack Overflow