相关疑难解决方法(0)

如何在单击按钮上调用PHP函数

我是PHP的新手,刚刚开始学习这门语言的基础知识.

我创建了一个名为functioncalling.php的页面,其中包含两个按钮:Submit和Insert.作为PHP的初学者,我想测试单击按钮时执行的功能.我希望输出出现在同一页面上.所以我创建了两个函数,每个按钮一个.functioncalling.php的源代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <body>
        <form action="functioncalling.php">
            <input type="text" name="txt" />
            <input type="submit" name="insert" value="insert" onclick="insert()" />
            <input type="submit" name="select" value="select" onclick="select()" />
        </form>

        <?php
            function select(){
               echo "The select function is called.";
            }
            function insert(){
               echo "The insert function is called.";
            }
        ?>
Run Code Online (Sandbox Code Playgroud)

这里的问题是在点击任何按钮后我没有得到任何输出.

任何人都可以告诉我我哪里错了?最早的回复将受到高度赞赏.先感谢您.

html php forms button

138
推荐指数
9
解决办法
90万
查看次数

POST方法在npm http-server上抛出错误405

我试图在本地http服务器上发出一个Ajax请求来运行php文件.我Error 405: method not allowed在浏览器控制台中找到了一个.

我试过一些类似问题的答案无济于事.我正在使用npm http-server来托管这个.我已经尝试在http服务器上启用CORS,但这并没有解决问题.

我可以将我的问题缩小到以下代码(使用此处给出的答案).

/test.html:

<html>
    <head>
        <title>Button</title>
        <meta charset="utf-8">
        <!-- jQuery -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>  
    </head>
    <body>
        <button type="button">Click Me</button>
        <p></p>
        <script type="text/javascript">
            $(document).ready(function(){
                $("button").click(function(){
                    $.ajax({
                        method: 'POST',
                        url: 'echo.php',
                        success: function(data) {
                            alert(data);
                            $("p").text(data);

                        }
                    });
                });
            });
        </script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

/echo.php:

<?php
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
    header('Access-Control-Allow-Headers: Content-Type, Accept');
    echo "HELLO WORLD";
?>
Run Code Online (Sandbox Code Playgroud)

我使用http-serverfrom 启动服务器npm.当我尝试发出POST请求(即单击按钮)时,我在服务器日志中收到以下错误:

[...] "POST /echo.php" Error (404): "Not found"
Run Code Online (Sandbox Code Playgroud)

echo.php …

javascript ajax jquery node.js npm

5
推荐指数
1
解决办法
3942
查看次数

标签 统计

ajax ×1

button ×1

forms ×1

html ×1

javascript ×1

jquery ×1

node.js ×1

npm ×1

php ×1