简体   繁体   中英

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.

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 href = "file.php?catfilter=category">aaa</a>

and on php side use a $_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. 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. 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.

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. Here is the link

http://api.jquery.com/jQuery.ajax/ to help you understand in detail

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

for example:

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

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'

without refreshing your page to load data you can use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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