简体   繁体   English

使用Ajax和Javascript从html按钮运行php File

[英]Run php File from html button with Ajax and Javascript

I've got a php function that i would like to run from a Html button I've got. 我有一个PHP函数,我想从一个HTML按钮运行。 The php function is on a separate file than the Html button. php函数位于与Html按钮不同的文件中。

So I've managed to do this with: 所以我设法做到这一点:

<input type="submit" name="test" id="bt1" value="RUN" /><br/>

But that is not what i want to do. 但这不是我想要做的。 I want to call the php function with: 我想用以下方法调用php函数:

<button id="bt1" class="button button5">< Random </button>

as there are other actions coupled with the button that need to run at the same time. 因为还有其他操作需要与按钮同时运行。

Side note: My php function gets info from a json file. 旁注:我的php函数从json文件获取信息。

I've got 3 files. 我有3个档案。

File 1 (AF.php) this is where the button is File 2(Display.php) This is where my jquery/javascript/ajax scripts are File 3(Names.php) This is where the php function is. 文件1(AF.php),这是按钮;文件2(Display.php),这是我的jquery / javascript / ajax脚本;文件3(Names.php),这是php函数所在的位置。 I want to use button from File 1 to run php function from File 3. 我想使用文件1中的按钮来运行文件3中的php函数。

So here is what I've tried so far: 所以这是到目前为止我尝试过的:

File 1(AF.php) 文件1(AF.php)

 button id="bt1" class="button button5">< R30K</button>

File 2(Display.php) 文件2(Display.php)

$(document).ready(function () {
            $("#bt1").on("click", (function () {
                getName("Names.php");
            })
        });
  function getName(Names.php) {
        $.ajax({
            url:"Names.php" + Names.php,
            success:function (Names.php) {
                $("#res" + Names.php).html(result)
            }
        });

File 3(Names.php) 文件3(Names.php)

<?php

    function group1(){
        $jsondata = file_get_contents("Clients.json");
        $json = json_decode($jsondata, true);
        $output = '<ul>';
        foreach($json['Reps']as $reps){
            $output .='<h4>' .$reps['Client']."<h4>";
            $output .= "<li>".$reps['repCode']."</li>";
        }

        $element = $json['Reps'][array_rand($json['Reps'])];
        echo $element['Client'];
        echo " ";
        echo $element['repCode'];
    }

    if(array_key_exists('bt1',$_POST)){
        group1();
    }

    ?>

Desired outcome would be that I can run my function from File 3 with that Html buttom from File 1. 期望的结果是,我可以使用文件1中的HTML按钮运行文件3中的函数。

You're help is greatly appreciated. 非常感谢您的帮助。

Your HTML with that button and your jQuery code must be in the same document that is loaded by your browser, either by putting them in the same file or using php include/require. 带有该按钮的HTML和jQuery代码必须位于浏览器加载的同一文档中,方法是将它们放在同一文件中,或者使用php include / require。

That jQuery code will not run because you put "Names.php" everywhere (Why?). 该jQuery代码将无法运行,因为您将“ Names.php”放在各处(为什么?)。 Try this: 尝试这个:

$( function(){
    $("#bt1").click(function() {
        $.ajax({
            url: 'Names.php',
            type: 'get',
            success: function(data) {
                $("#res").html(data);
            },
        });
    });
});

This will put the ajax response string into any element with id="res" . 这会将ajax响应字符串放入id="res"任何元素中。

Code was tested and works. 代码已经过测试并可以正常工作。

Edit: 编辑:

I just noticed that you are using $_POST in your php script to check if the button was clicked, so you will have to modify the jQuery ajax function: 我只是注意到您在php脚本中使用$_POST来检查按钮是否被单击,因此您将不得不修改jQuery ajax函数:

Change type: 'get', to type: 'post', 更改type: 'get', type: 'post',

Add data: 'bt1=1', after 'type: 'post' . 'type: 'post'之后添加data: 'bt1=1', 'type: 'post'

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

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