如何将php调用到wordpress页面ajax中 [英] How to call php into wordpress page ajax

查看:71
本文介绍了如何将php调用到wordpress页面ajax中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一些页面,其中将包含来自数据库的一些分类内容.一个已经进行了排序的php,我需要通过按钮适当地调用它,并且我需要将php的输出写到已经在页面上的div中.有可能吗?

I am making some page which will have some sorted content from databse. A have already made sorting php and i need to call it propably by button, and i need taht php output to be written to some div which is already on page. is it possible?

sorting.php

sorting.php

<?php
mysql_connect("localhost", "login", "pass") or die(mysql_error());
mysql_select_db("database");

$result = mysql_query("SELECT * FROM table ORDER BY added ASC");

while($row = mysql_fetch_array($result))
  {
  echo "<div class='something'>";
  echo "<a href='$row[link]'>";
  echo "<img src='$row[img]' height='125' width='125'/>";
  echo "<p>$row[name]</p></a></div>";
  echo "<br>";
  }
?> 

每种类型的排序或过滤器将由不同的php处理.这是制作这种页面的好方法吗?还是有一些更简便更好的方法?还有一件事.选择不同类型的过滤器时,是否可能会有一些过渡?我想要这样的东西: quicksand

Every type of sort or filter will be handled by different php. Is this good way how to make this kind of page? or is there some easier and better way? And one more thing. Is it possible to have some transitions when different type of filter is selected? I would like something like this: quicksand

推荐答案

如果您可以在页面上包含jQuery,它就有一个简写的方法可以做到这一点:

If you can include jQuery on your page, it has a shorthand method for doing exactly that: .load(). Your final javascript might look something like this:

$(document).ready(function() {

    $("#searchButton").bind('click', function() {
        $("#resultsDiv").load(
            'http://mysite.com/sorting.php',
            { // parameters, in object literal format
               sortDir: 'asc',
               sortCol: 'lastname'
               // etc.
            }
        );
    });

});

在其中将mysite.com,searchButton和resultsDiv替换为您的网站/标记的真实信息.您还可以使用jquery选择器从页面元素获取排序参数,如下所示: $(#sortDir").val()-假定有文本框,文本区域或选择框.假设您的表单使用复选框和单选按钮,则还有其他方法.要回答您的其他问题:

Where you substitute mysite.com, searchButton, and resultsDiv with the real information for your site/markup. You can also get the sort params from page elements with jquery selectors like so: $("#sortDir").val() - assumes a text box, text area or select box. There are other ways to retrieve checkboxes and radio buttons, assuming your form uses them. To answer your other questions:

1)每种类型的排序或过滤器都将由不同的php处理.这是制作这种页面的好方法吗?还是有一些更简便,更好的方法?

1) Every type of sort or filter will be handled by different php. Is this good way how to make this kind of page? or is there some easier and better way?

对于少数人来说,这并不是没有道理的.不接受服务器上的任何参数是确保您永远不允许恶意输入的一种方法.当您需要更改表名或所选列并必须修改每个文件时,它往往变得笨拙.您还需要意识到,如果允许过滤器和排序的组合,那么您在谈论支持它们的交集.如果您有3个过滤器和4种分类,那就是12种组合.如果要添加其他排序和其他过滤器,则必须添加4个新的过滤器页面和5个新的过滤器页面-每次情况都会变糟.

It's not unreasonable for a small number. Not accepting any arguments on the server is one way to make sure you don't ever allow malicious input. It tends to get unwieldy when you need to change a table name or the columns you're selecting and have to modify every file. You also need to realize that if you allow combinations of filters and sorts, you're talking about supporting the intersection of them. If you have 3 filters and 4 sorts, that's 12 combinations. If you want to add another sort and another filter, you have to add 4 new pages for filters and 5 new pages for sorts - and it gets worse every time.

大多数人会选择使所有数据都进入同一页面,而只有该页面负责验证用户输入,制定查询并返回结果.这种范例通常称为为不重复自己而干燥.但是,如果您接受用户输入,则需要非常小心唐'不信任用户输入!),并且不允许例如 SQL注入

Most people would go the route of having all data go to the same page and have only that page be responsible for validating user input, formulating the query, and returning the results. This paradigm commonly called DRY for Don't Repeat Yourself. But if you are accepting user input, you need to be very careful about validating it (on the server side! Don't trust user input!) and not allowing e.g. a SQL injection attack.

2)还有一件事.选择不同类型的过滤器时,是否可能会有一些过渡?我想要这样的东西:流沙

2) And one more thing. Is it possible to have some transitions when different type of filter is selected? I would like something like this: quicksand

那个特殊的动画非常复杂.他们在每个页面上显示的图标与以编程方式自定义缩小,爆炸,移动等图标之间存在差异.我可以详细介绍如何自己做...

That particular animation is very complex. They are running a difference between the icons to be shown for each page and programmatically customizing which icons to shrink out, explode in, move etc. etc. I could go into more detail about how to do it yourself...

但是你知道吗?它是一个插件,与jQuery 1.3+兼容,并在MIT&GPLv2,因此将其合并到您的站点中应该没有问题.检查 docs 并自己​​刺一下.我看不出有什么本质上与您要返回的标记类型排序不兼容.我确实注意到他们的示例使用 li 标记进行排序,而您的示例返回使用的是 div .但这不应该成为主要的挂断,如果是的话,只需返回 li s即可.当您在使用它时遇到问题时,请发布另一个问题,对其进行适当标记,并准备发布您在这方面尝试过的所有内容.

But your know what? It's a plugin, compatible with jQuery 1.3+ and licensed under MIT & GPLv2, so you should have no problems incorporating it into your site. Check the docs and take a stab at it yourself. I don't see anything inherently incompatible with sorting the type of markup you're returning. I do notice that their example is using li tags for sorting and your example return is using div. But that shouldn't be a major hangup, and if it is, just return lis instead. When you run into problems using it, post another question, tag it appropriately and be prepared to post everything you've tried in that regard.

这篇关于如何将php调用到wordpress页面ajax中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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