如何将 javascript onclick 功能与 php 集成?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16266704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:59:06  来源:igfitidea点击:

How can I integrate javascript onclick function with php?

phpjavascript

提问by Humphrey

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

我在将 onclick 功能与 php 集成时遇到问题。有人可以帮忙吗

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

回答by pasty

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 deleteRecordand you could use php only to output the ID, like:

最重要的是:不要使用javascript关键字deleteJavaScript delete operator help)作为函数名!因此,您的功能(以及 onclick)将无法工作!将其更改为适当的内容,例如deleteRecord,您可以仅使用 php 来输出 ID,例如:

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

亲切的问候,P。

回答by joel cox

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.

我找到了一个很好的解决方案。但是,它为您的代码添加了一些额外的内容。我不建议过多地使用这种方法。我是一个新的 PHP 开发人员,所以这个方法可能有一些可以做和不能做的事情。

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)

我似乎也无法阻止此方法加载新页面,因此最好的方法是将您的 php 添加到文件的开头。这也是使用 AJAX 的更好解决方案(我认为)

if the current file is index.php...

如果当前文件是 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 标记可以放在任何地方,只要它在同一个文件中......除非你知道如何解决。最好的地方是在 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>