【问题标题】:Buttons that affect a function - PHP影响功能的按钮 - PHP
【发布时间】:2017-01-09 00:11:55
【问题描述】:

场景: 我试图让我的页面的一部分持续运行一个功能,然后单击上述按钮时该功能会受到影响。 按钮调用 main/constant 函数中的函数;大师($选定)。

这是我尝试过的。 index.php:

<? session_start();
require("inc.ini.php");

?>
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
<meta charset="utf-8">
<title>Buttons to funtions</title>
</head>

<body>
    <main>
        <header>
                <form method="post"  action="<?=$_SERVER['php_self']?>">
                    <button type="submit" name="change" value="insert, <?$selected = "func1";?>">Func 1</button>
                    <button type="submit" name="change" value="insert, <?$selected = "func2";?>">Func 2</button>
                </form>
        <!--
        I want to press a button and when it is pressed it calls the function selected and gives it the value of which the button represents.
        -->
        </header>
        <figure>
            <?
            if($_POST['change']){
                echo $selected;
                master($selected);
            }
            ?>
        </figure>
    </main>
</body>
</html>

inc.ini.php:

<? session_start();

function master($selected){
    if($selected == "func1"){
        function func1(){
            echo "func 1";
        }
    }
    if($selected == "func2"){
        function func2(){
            echo "func 2";
        }
    }
}
?>

另一个问题。我需要做这些 if 语句吗,$selector 可以直接跳转到新函数吗?

【问题讨论】:

  • 你似乎需要 javascript 和 ajax...或者重命名提交按钮。

标签: php function button


【解决方案1】:

Php 是一种服务器端语言,这意味着需要向服务器发送请求才能使您的代码运行。

当一个表单提交时,它的所有子表单都会被发送,因此无法区分哪个被点击了。

话虽如此:

<?php
    session_start();
    require("inc.ini.php");
?>
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
<meta charset="utf-8">
<title>Buttons to funtions</title>
</head>

<body>
    <main>
        <header>
                <form method="post"  action="<?=$_SERVER['php_self']?>">
                    <button type="submit" name="change" value="Func1">Func 1</button>
                </form>
                <form method="post"  action="<?=$_SERVER['php_self']?>">
                    <button type="submit" name="change" value="Func2">Func 2</button>
                </form>
        </header>
        <figure>
            <?php
                if (isset($_POST['change'])) {
                    echo $_POST['change'];
                    master($_POST['change']);
                }
            ?>
        </figure>
    </main>
</body>
</html>

如果您使用 2 种形式,那么您会得到相同名称的不同值。

查看您的 inc.ini.php 文件,您似乎正在根据输入的输入定义函数。我建议不要这样做,但如果你愿意这样做,那就可以了。

如果您需要更多帮助,请在此帖子中添加评论。

希望这会有所帮助。

【讨论】:

  • 好的,你对我想做的事情有什么建议?基本上,我制作了一个学习 PHP 的网站,它基于将我创建的游戏库加载到一个页面中。
  • 这取决于。您希望从这段代码中得到的实际结果是什么?你要这个做什么?
  • 我只想制作一个新游戏(通过按钮选择的游戏)来加载到图形中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-26
  • 2017-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-25
相关资源
最近更新 更多