简体   繁体   English

jQuery在单击按钮时返回PHP脚本

[英]jQuery return PHP script on button click

Currently, I have a HTML page that includes a PHP script. 目前,我有一个包含PHP脚本的HTML页面。 Once the user signs in, with the php script included, the data is automatically returned to the page, as follows: 用户登录后(包括php脚本),数据将自动返回到页面,如下所示:

 <section class="content">
 <?php include("api/qnams_all.php"); ?>
 </section>

That script includes a query that returns a table with an ID called #example1. 该脚本包含一个查询,该查询返回ID为#example1的表。

Using that ID, I am to use jQuery DataTables to format the data from that script: 使用该ID,我将使用jQuery DataTables格式化该脚本中的数据:

 $('#example1').DataTable({
 {
   "dataType": "json",  
   "iDisplayLength": 25,
   "bDestroy": true,
   "stateSave": true
 });

Now, what I would like to do, is check if a button was clicked via jQuery, and if so, run a different php script. 现在,我想做的是检查是否通过jQuery单击了按钮,如果是,请运行其他php脚本。

So in that section tag above, I want to do something like this: 因此,在上面的那部分标签中,我想做这样的事情:

 <section class="content">
 <button type="button" id="buttonSearch">Search</button>
 <script type="text/javascript">
   if('#buttonSearch').click(function()  
   {
     // return <?php include("api/differentScript.php"); ?>
   }
   else
   {
     // return <?php include("api/qnams_all.php"); ?>
   });
 </script>
 </section>

I am sure the syntax is probably wrong in the JavaScript. 我确定JavaScript中的语法可能错误。 I was trying to show an example of what I'm trying to do. 我试图展示一个我想做的事的例子。

Can this be done? 能做到吗?

I would use an ajax call to the page. 我将使用ajax调用页面。 Listen to whats been requested in PHP and return it. 收听PHP要求的内容并返回。 Then add the result of this to a HTML object on page. 然后将其结果添加到页面上的HTML对象。

I would approach it similar to this; 我会这样处理:

<?php 

if(isset($_POST['script1'])){
    include("api/differentScript.php");
    exit;
}

if(isset($_POST['script2'])){
    include("api/qnams_all.php");
    exit;
}

?>

<div id='output'></div>


<script type="text/javascript">

    $("#buttonSearch").on('click', function(){
        $.ajax({
            type:"POST",
            url: "/page_location/",
            data: "script1=true",
            success: function(result){
                $("#output").html(result);
            }
        });
    });

    $("#buttonSearch2").on('click', function(){
        $.ajax({
            type:"POST",
            url: "/page_location/",
            data: "script2=true",
            success: function(result){
                $("#output").html(result);
            }
        });
    });

</script>

I'm not sure how data tables works, if you were to store the data in a variable you could send it with the post data by changing it to: 我不确定数据表的工作方式,如果要将数据存储在变量中,则可以通过将其更改为以下内容将其与发布数据一起发送:

data: {script1: true, post_data_1: var}, 

Unfortunately, what you are trying to do won't work without sending something back to your web server. 不幸的是,如果您没有将某些内容发送回Web服务器,则您尝试执行的操作将无法工作。 PHP is a server side language, so once you are in a browser using Jquery, you cannot execute anymore PHP on that page. PHP是服务器端语言,因此一旦您使用Jquery在浏览器中,就无法在该页面上执行PHP。

You have two options, assuming you don't want to send both options downstream with the initial request: you can redirect on click: 假设您不想在初始请求中向下游发送这两个选项,则有两个选项:您可以在单击时重定向:

<script type="text/javascript">
if('#buttonSearch').click(function()  
{
    window.location.href = "/api/differentScript.php";
}
else
{
   window.location.href = "/api/qnams_all.php";
});
</script>

Or, looking at your URL and noting that you are using an API, you can make an asynchronous call using AJAX: 或者,查看您的URL并指出您正在使用API​​,则可以使用AJAX进行异步调用:

<script type="text/javascript">
$('#buttonSearch').click(function()  
{
   $.ajax({
       dataType: 'json',
       url: "/api/differentScript.php",
       data: {key: 'value'},
       success: function(repsonse) {
       var responseObject = JSON.parse(response);
       //Do something with response object on page
   },
   error (message) {
       //handle errors
   }
});
</script>

If 'differentScript.php' ends in 如果'differentScript.php'结尾于

return json_encode($results);

And $results is a PHP object or array, then the $ results是一个PHP对象或数组,然后

JSON.parse(response)

Will turn your PHP array into a jquery object. 将把您的PHP数组变成一个jquery对象。

You cannot include a PHP script like that within Javascript. 您不能在Javascript中包含类似的PHP脚本。 You will have to make an Ajax call for each condition & then do the necessary things you want via the PHP script. 您将必须为每个条件进行Ajax调用,然后通过PHP脚本执行所需的必要操作。 If you "include" the PHP scripts as you have mentioned above, they will directly be included when your page loads, not on click of any button. 如果您如上所述那样“包括” PHP脚本,则它们将在页面加载时直接包括在内,而不是单击任何按钮。 Plus this is not the right way to do it. 另外,这不是正确的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM