简体   繁体   English

使用ajax / javascript调用php函数

[英]Calling a php function using ajax/javascript

Ok guys I know this question has been asked before but I am very new to PHP and JavaScript and hadn't even heard of ajax until i started looking for an answer to this question so do not understand previous answers. 好的我知道这个问题之前已被问过,但我是PHP和JavaScript的新手,甚至没有听说过ajax,直到我开始寻找这个问题的答案,所以不理解以前的答案。

I am creating a site that essentially is a bunch of videos in a SQL database, it shows one video at a time, I would like to have a next and previous video buttons. 我正在创建一个基本上是SQL数据库中的一堆视频的网站,它一次显示一个视频,我希望有一个下一个和上一个视频按钮。

However I cant get past this ajax thing so my question is even simpler. 但是,我无法通过这个ajax事情,所以我的问题更简单。 I have looked at this question/answer and think it pretty much sums up what im asking: 我看过这个问题/答案并认为它几乎总结了我的要求:

How do I run PHP code when a user clicks on a link? 当用户点击链接时如何运行PHP代码?

I have copied that exact code, 我复制了那些确切的代码,

    <script type="text/javascript">
       function doSomething() {
       $.get("backend.php");
          return false;
      }
     </script>

<a href="#" onclick="doSomething();">Click Me!</a>

And in my backend.php file i have literally just got <?php echo "Hello" ?> just to test it and therefore my understanding is that when i click the link the javascript onClick event is trigged which in turn calls the backend.php file, which says to print "Hello" to the page. 在我的backend.php文件中,我确实只有<?php echo "Hello" ?>只是为了测试它,因此我的理解是,当我点击链接时,javascript onClick事件被触发,而后者又调用backend.php文件,表示向页面打印“Hello”。 However when i click the link it does nothing. 但是当我点击链接时它什么也没做。

Eventually obviously im going to need to get a lot more complex with my php functions and calling variables and all that stuff but i like to figure things out for myself for the most part so i learn. 最终显然我需要通过我的php函数和调用变量以及所有这些东西来获得更复杂的东西,但我喜欢在大多数情况下为自己解决问题,所以我学习。 However im stuck on this bit. 但是我坚持这一点。 Also whilst im here i will ask another thing, I want to 'give back' to the users of the site for answering my questions but I can only really well enough in HTML and CSS to answer other peoples questions, any advice on being able to find the simpler questions on here so i can answer some. 此外,我在这里,我会问另一件事,我想“回馈”网站的用户回答我的问题,但我只能在HTML和CSS中做得很好,以回答其他人的问题,任何建议,能够在这里找到更简单的问题所以我可以回答一些问题。

Thanks in advance :) 提前致谢 :)

It does nothing becuase you don't do anything with the result. 它没有任何结果,因为你不对结果做任何事情。 My guess is that in the example you took, it does some work and doesn't show anything to the user. 我的猜测是,在你拍摄的例子中,它做了一些工作并且没有向用户显示任何内容。 So if you just had some stuff you wanted to run on the server without returning any output to the user, you could simply do that, and it would work. 因此,如果您只是想要在服务器上运行某些内容而不向用户返回任何输出,那么您可以简单地执行此操作,并且它可以正常工作。

Example from jQuery's .get() documentation jQuery的.get()文档示例

What you do: 你做什么:

Example: Request the test.php page, but ignore the return results. 示例:请求test.php页面,但忽略返回结果。

$.get("test.php");

What you want to do: 你想做什么:

Example: Alert out the results from requesting test.php (HTML or XML, depending on what was returned). 示例:提示请求test.php(HTML或XML,具体取决于返回的内容)的结果。

$.get("test.php", function(data){
    alert("Data Loaded: " + data);
});

Take a look at the .get() documentation . 看一下.get()文档 You're using it incorrectly. 您使用不当。

You should be passing data (optional) and handling the data that gets returned, at a minimum: 您应该传递数据(可选)并处理返回的数据,至少:

$.get("backend.php",
    {
        // data passed to backend.php goes here in 
        //
        // name: value
        //
        // format. OR you can leave it blank.
    }, function(data) {
        // data is the return value of backend.php
        // process data here
    }
);

If you pass data, you can retrieve it on backend.php using $_GET . 如果传递数据,则可以使用$_GETbackend.php上检索它。 In this case: 在这种情况下:

$_GET['name'];
$.get("test.php", { name: "John", time: "2pm" }, function(data) {
   alert("Data Loaded: " + data);
 });

http://api.jquery.com/jQuery.get/ http://api.jquery.com/jQuery.get/

This would alert the data. 这会提醒数据。 right now that function only returns false. 现在该函数只返回false。

$.get('backend.php', function(data) {
  alert(data);
});

Your code will not print to the page the way you have it set up; 您的代码不会按照您设置的方式打印到页面; you're part of the way there, in that you have called the page, but the response needs to be handled somehow. 你是那种方式的一部分,因为你已经调用了页面,但响应需要以某种方式处理。 If you open up the developer tools in Chrome, you can click on the Network tab and see the request and response to verify that what you coded is actually working, but now you need to put the response somewhere. 如果您在Chrome中打开开发人员工具,则可以单击“网络”选项卡,查看请求和响应,以验证您编码的内容是否真正有效,但现在您需要将响应放在某处。

By passing a function as the second variable into $.get, you can make your request show up on the page. 通过将函数作为第二个变量传递到$ .get,您可以在页面上显示您的请求。 Try something like this: 尝试这样的事情:

$.get("backend.php", function (data) { $('body').append(data); } );

Your code is not handling with that data. 您的代码未处理该数据。 So instead, you should use following code : 因此,您应该使用以下代码:

$.get("backend.php", function(response) {
    alert(response);
  })

Or, to show that data on UI, assign it to any html element. 或者,要在UI上显示该数据,请将其分配给任何html元素。

For more understanding , please visit : jQuery.get() link 有关更多信息,请访问: jQuery.get()链接

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

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