简体   繁体   English

从jQuery运行PHP脚本

[英]Run php script from jquery

I have a really weird question... Don't know if it possible to make, However although i know how to do it via old get urls and refreshing page, I want to try the new way with jquery to call the file and post or 'get' data via jquery but without using a form. 我有一个很奇怪的问题...不知道是否有可能,但是,尽管我知道如何通过旧的获取url和刷新页面来做到这一点,但我想尝试使用jquery的新方法来调用文件并发布或通过jquery“获取”数据,但不使用表单。

I will be using a <a> link, if you go to my website http://www.jonathansconstruction.com/gallery.php and you see the second gallery menu where it says view all, that will filter the category to view and hopefully show it in a div like it is showing at the moment, one div per category i used to use 我将使用一个<a>链接,如果您转到我的网站http://www.jonathansconstruction.com/gallery.php,并且您看到第二个图库菜单,其中显示“查看全部”,则该菜单将过滤要查看和查看的类别。希望像现在显示的那样在div中显示它,我以前使用的每个类别一个div

<a href = "file.php?catfilter=category">aaa</a>

and on php side use a $_GET[]; 在php端使用$_GET[]; , so i will like to know a different way to pass the values to set category filter, and also, be able to menu "current" with jquery. ,因此我想知道一种不同的方法来传递值以设置类别过滤器,并且还可以使用jquery菜单“ current”。 By the way, the Menu will also be generated dynamically depending on the number of categories on the database.. 顺便说一句,菜单也将根据数据库中类别的数量动态生成。

Will also like to add a "twitter/fb" style pagination(load more data on scroll down or when user clicks show more) but will also like the divs to be grouped by category getting from the database. 还希望添加“ twitter / fb”样式分页(向下滚动或当用户单击时显示更多,以加载更多数据),但也希望将div按类别分组以从数据库中获取。 That's the whole idea, I do know how to setup the output and do a lot of the php side to group the items together, however, I'm not too sure about the new pagination style (used to use the one with pages to click next) and also how to run/post data to php without form and without refreshing. 这就是整个想法,我确实知道如何设置输出并进行很多php方面的工作,以将各项组合在一起,但是,我不太确定新的分页样式(用于将带有页面的样式用于单击接下来),以及如何在不使用表格和刷新的情况下将数据运行/发布到php。

Hope this was clear enough, if not let me know. 希望这足够清楚,如果不能让我知道。 Any help/idea on how to do it is appreciated. 任何帮助/想法如何做到的,不胜感激。 Thanks a lot 非常感谢

To load data dynamically and that too without refreshing your page, for that you can use Jquery $ajax() method. 要动态加载数据,并且也无需刷新页面,可以使用Jquery $ ajax()方法。 Here is the link 链接在这里

http://api.jquery.com/jQuery.ajax/ to help you understand in detail http://api.jquery.com/jQuery.ajax/可帮助您详细了解

you can also pass parameters to the url/page you want to execute, and whatever is the result of your php file, you can set that to wherever you want 您还可以将参数传递到要执行的url /页面,无论php文件的结果如何,您都可以将其设置为所需的任何位置

for example: 例如:

$.ajax({
  type: "POST",
  url: "file.php",
  data: { catfilter : "category" }
}).done(function( msg ) {
  $("#container").html(msg)
});

file.php file.php

<?php
if(isset($_POST['catfilter']))
{
   //your code here
   echo "your code here";
}
?>

calling above jquery ajax method will set the output of file.php to html element with id= 'container' 在上面的jquery ajax方法中调用将把file.php的输出设置为id ='container'的html元素

without refreshing your page to load data you can use jQuery.Ajax 无需刷新页面即可加载数据,您可以使用jQuery.Ajax


$.ajax(     
{   
    type: "POST"    
    url:page,
    data: {......}
    beforeSend: function(xhr) {
        $('#load').empty().html('Loading... ');
    },
    success: function(data) {
        $('#load').empty().html(data);
    },
    dataType: "html"    
});

the result will be displayed in div load and get from the url:page 结果将显示在div load并从url:page

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

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