Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在点击按钮时运行PHP代码?_Php_Jquery_Html - Fatal编程技术网

如何在点击按钮时运行PHP代码?

如何在点击按钮时运行PHP代码?,php,jquery,html,Php,Jquery,Html,我正试图为学校项目建立一个网站。我希望schoolResponse()函数在单击Submit按钮后发生。如果我删除jQuery函数,它会工作,但在加载页面时会显示一个默认值 这是我的密码: <!DOCTYPE html> <html> <head> <title>NHG</title> <meta charset="UTF-8"/> <meta name="viewport" content

我正试图为学校项目建立一个网站。我希望schoolResponse()函数在单击Submit按钮后发生。如果我删除jQuery函数,它会工作,但在加载页面时会显示一个默认值

这是我的密码:

<!DOCTYPE html>
<html>
  <head>
    <title>NHG</title>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <link type="text/css" rel="stylesheet" href="css/normalize.css"/>
    <link type="text/css" rel="stylesheet" href="css/style.css"/>
    <link type="text/css" rel="stylesheet" href="css/resposive.css"/>
  </head>
  <body>
    <header>
            <div id="main-head">
              <a href="#"><h2 id="main-heading">sKoolBook</h2></a>
            </div>
    </header>
    <section>
      <div id="container">
        <div id="wrapper">
          <form method="POST">
            <select name="school">
              <option value="none" name="none">Please Select a School...</option>
              <option value="NHG">New Horizon Gurukul</option>
            </select>
            <input type="submit" class="button"/>
            <?php
              error_reporting(0);
              $school = $_POST['school'];
              function schoolResponse() {
                if ($_POST['school'] == 'none'){ 
                  echo 'nothing';
                } else {
                  echo 'something';
                }
              }
            ?>
            <script>
              $('.button').click(
                function(
                  <?php schoolResponse(); ?>
                );
              );
            </script>
          </form> 
        </div>
      </div>
    </section>
    <footer>
    </footer>
  </body>
</html>

NHG
请选择一所学校。。。
新视野古鲁库
$('.button')。单击(
作用(
);
);

你不能这样做。这里有一个解决方案:

         <?php
          error_reporting(0);
          $school = $_POST['school'];
          function schoolResponse() {
            if ($_POST['school'] == 'none'){ 
              echo 'nothing';
            } else {
              echo 'something';
            }
          }

          if($_POST['school']) schoolResponse();

        ?>

但这是一个非常基本的示例。

您应该使用jQuery/AJAX来完成这项工作

<script>
                  $('.button').click(
                    $.ajax({
                       url: 'yoururl.php',
                       data: $("form").serialize(), //Better to put an ID to  the form or something
                       success: function(data){
                          //DO something with your data
                     }

        })
                  );
                </script>

服务器端和客户端语言不交互或共享变量。PHP在服务器上运行,生成HTML和JavaScript,最终在另一台完全不同的计算机上运行。我知道,我只是要求一个解决方案,在单击submit按钮时运行schoolResponse()函数。只是想知道那么多。为此,您需要将页面重新提交到服务器,以便它使用新值(例如,使用表单)再次运行,或者对单独的脚本发出AJAX调用,并在需要的任何地方注入值。
error\u reporting(0)禁用错误报告。你可能是指
错误报告(E_ALL)。带有错误报告(E\U ALL);这样会更好。。。刚才在问题中使用了部分代码。。。可能是@Shreyas Sreenivasa需要错误报告(0),我不知道,但是谢谢你的评论,最好在表单上加个ID什么的,你的意思是在表单上加个ID然后在那里键入吗?我应该如何处理我的数据?更新:你在谈论哪些数据?php中的表单?最好在表单中添加一个ID,以便在视图中有更多表单的情况下,确保提交所需的表单。
<script>
                  $('.button').click(
                    $.ajax({
                       url: 'yoururl.php',
                       data: $("form").serialize(), //Better to put an ID to  the form or something
                       success: function(data){
                          //DO something with your data
                     }

        })
                  );
                </script>
$school = $_POST['school'];
          function schoolResponse() {
            if ($_POST['school'] == 'none'){ 
              echo 'nothing';
            } else {
              echo 'something';
            }
          }