如何在按钮单击时调用php函数 [英] how to call a php function on button click

查看:104
本文介绍了如何在按钮单击时调用php函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是两个文件

Calling.php

Calling.php

<html>
<body>
<form action="Called.php" method="get">
    <input type="button" name="B1" value="B1">
    <input type="button" name="B2" value="B2">
    <input type="Submit" name="Submit1"/>

    <!-- <a href="http://www.google.com?act=google">Google</a>
    <a href="http://www.yahoo.com?act=yahoo">yahoo</a>
     -->
</form>     
</body>
</html>

和Called.php

And Called.php

<?php
if(isset($_GET("Submit1")))
{   
        echo("<script>location.href = 'http://stackoverflow.com';</script>");
}
if(isset($_GET["B1"]))
{
    echo("<script>location.href = 'http://google.com/';</script>");
    exit();
} 
if(isset($_GET["B2"]))

 - List item

{
    echo "<meta http-equiv='refresh' content='0;url=http://www.yahoo.com'>";
    exit(); 
}
?>

当我单击按钮"B1"和"B2"时,页面将闪烁,但是现在重定向和第三个提交"按钮将重定向到新页面,在那里我得到的输出为"Called.php".

When i click the buttons "B1" and "B2", page will blink but now where redirect and third one "Submit" button will redirect to new page and there i am getting the out put as "Called.php".

请花几秒钟的时间来学习这个php新手.

Please spend few seconds for this php beginner.

推荐答案

您不能直接进行操作,因为单击按钮是客户端活动,而PHP是服务器端.如果您使所有输入都提交,则用户单击的输入将作为$ _GET数组的一部分提交,但仅当用户单击其中的一个但不提交表单(例如,按文本中的Enter键)时,该输入才起作用输入.

You can't directly because the button click is a client side activity and PHP is server side. If you make all the inputs submit then the one the user clicked will be submitted as part of the $_GET array but that only works if the user clicks one of them and doesn't submit the form by, say, hitting Enter in a text input.

您可以将AJAX事件附加到按钮上,并使它们触发PHP脚本来运行要运行的功能,但这有其自身的问题.

You could attach AJAX events to the button and have them trigger off a PHP script to run the function you want to run, but that has its own set of issues.

我应该注意,至少可以说,您的重定向方法相当笨拙.您可以只使用header()进行重定向,这比通过回显javascript来解决这些麻烦要干净得多.

I should note that your method of redirecting is rather inelegant to say the least. You can just use header() to do the redirection, it would be much cleaner than all this messing around with echoing out javascript.

这篇关于如何在按钮单击时调用php函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆