调用使用AJAX / JavaScript的PHP函数 [英] Calling a php function using ajax/javascript

查看:103
本文介绍了调用使用AJAX / JavaScript的PHP函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定家伙,我知道这个问题已经被问过,但我很新的PHP和JavaScript和甚至从未听说过阿贾克斯,直到我开始寻找这个问题的答案,以便不明白previous答案。

我创建了一个网站,基本上是一帮在SQL数据库的视频,它显示在一个时间一个视频,我想有一个下一个和previous视频按钮。

不过,我不能让过去的这个AJAX的事情,所以我的问题就更简单了。我已经看过这个问题/答案,并认为这pretty的多少概括了我要问:

<一个href="http://stackoverflow.com/questions/1280767/how-do-i-run-php-$c$c-when-a-user-clicks-on-a-link">How做我运行PHP code,当用户点击一个链接?

我抄了确切的code,

 &LT;脚本类型=文/ JavaScript的&GT;
       函数DoSomething的(){
       $获得(backend.php);
          返回false;
      }
     &LT; / SCRIPT&GT;

&LT; A HREF =#的onclick =DoSomething的();&gt;点击我&LT;!/ a取代;
 

在我的 backend.php 文件我已经从字面上刚刚&LT;?PHP的回声你好&GT; 只是为了测试它,所以我的理解是,当我点击的JavaScript onClick事件被触发时的链接,再调用 backend.php 文件,该文件说要打印你好的网页。然而,当我点击它什么都不做的链接。

最后,显然即时通讯将需要得到更多的复杂与我的PHP函数和调用变量和所有的东西,但我想搞清楚这些事情为自己的大部分,所以我学习。但是即时通讯卡在此位。此外,同时我在这里,我会问另一件事,我想回馈给该网站的用户回答我的问题,但我真的只能不够好HTML和CSS来回答其他人的问题,任何建议能够找到这里简单的问题,所以我可以回答一些。

在此先感谢:)

解决方案

它什么都不做监守你不这样做,结果什么。我的猜测是,在你把这个例子中,它做了一些工作,并没有显示任何东西给用户。所以,如果你只是有你想在服务器上运行的一些东西没有任何输出返回给用户,你可以简单地做到这一点,这是可行的。

这是jQuery的 获得()文件示例

你做了什么:

  

例如:请求test.php的网页,却忽略了返回的结果

  $得到(test.php的);
 

你想做什么:

  

示例:从请求test.php的(HTML或XML,这取决于返回)通知了结果

  $。获得(test.php的功能(数据){
    警报(数据加载:+数据);
});
 

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.

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.

However I cant get past this ajax thing so my question is even simpler. 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?

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. 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. 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.

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

What you do:

Example: Request the test.php page, but ignore the return results.

$.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).

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

这篇关于调用使用AJAX / JavaScript的PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆