jQuery:加载 PHP 代码片段
我使用此代码将不同页面的内容加载到主页中:
$('#access a').click(function(event) {
event.preventDefault();
var url = $(this).attr('href');
$('#content').fadeOut(500, function() {
$('#content').load(url, function() {
$('#content').fadeIn(500);
});
});
});
我希望能够将 的输出加载到单击 #mybutton 时相同的容器。
我怎样才能做到这一点?
我怀疑它必须是非常基本的东西,但我不确定它是如何完成的。我将不胜感激专家的建议!
更新:这是我尝试运行的 php 代码:
<?php printf( __( 'Search Results for: %s', 'twentyten' ), '<span>' . get_search_query() . '</span>' ); ?>
<?php if ( have_posts() ) : ?>
<?php
/* Run the loop for the search to output the results.
* If you want to overload this in a child theme then include a file
* called loop-search.php and that will be used instead.
*/
get_template_part( 'loop', 'search' );
?>
<?php else : ?>
<div id="post-0" class="post no-results not-found">
<h2 class="entry-title"><?php _e( 'Nothing Found', 'twentyten' ); ?></h2>
<div class="entry-content">
<p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'twentyten' ); ?></p>
</div><!-- .entry-content -->
</div><!-- #post-0 -->
<?php endif; ?>
I use this code to load the content from a different page into the main page:
$('#access a').click(function(event) {
event.preventDefault();
var url = $(this).attr('href');
$('#content').fadeOut(500, function() {
$('#content').load(url, function() {
$('#content').fadeIn(500);
});
});
});
I want to be able to load the output of <?php /*my PHP code */ ?>
into the same container when #mybutton is clicked.
How can I do that?
I suspect it has to be something very basic, but I'm not sure how it's done. I would appreciate expertly advice!
UPDATE: here's the bit of php code I'm trying to run:
<?php printf( __( 'Search Results for: %s', 'twentyten' ), '<span>' . get_search_query() . '</span>' ); ?>
<?php if ( have_posts() ) : ?>
<?php
/* Run the loop for the search to output the results.
* If you want to overload this in a child theme then include a file
* called loop-search.php and that will be used instead.
*/
get_template_part( 'loop', 'search' );
?>
<?php else : ?>
<div id="post-0" class="post no-results not-found">
<h2 class="entry-title"><?php _e( 'Nothing Found', 'twentyten' ); ?></h2>
<div class="entry-content">
<p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'twentyten' ); ?></p>
</div><!-- .entry-content -->
</div><!-- #post-0 -->
<?php endif; ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果我理解正确的话,可能比你想象的更简单。
首先,用隐藏的
包装 PHP 输出:
然后只需在单击按钮时将这些内容复制到另一个容器:
如果我没有正确理解,请更好地解释,我会尝试找到另一种方式解决这个问题。
If I understand correctly, it might be more simple than what you think.
First, wrap the PHP output with hidden
<div>
:Then just copy those contents to the other container on button click:
If I didn't understand correctly please explain better and I'll try to find other way around this.
PHP将在服务器端执行,因此如果您想加载PHP片段,那么您需要请求服务器将PHP片段的输出提供给您。为此,您需要使用 AJAX。检查 这个
就可以使用 jQuery
.load
,.ajax
、.post
来执行此操作PHP will be executed in the server side, so if you want to load a PHP snippet, then you need to request the server to give the output of the PHP snippet to you. For this you need to use AJAX. Check this
You can use the jQuery
.load
,.ajax
,.post
to do thisjQuery 执行 DOM 和 AJAX,但不执行 PHP,因此您必须调用脚本返回 HTML 输出并将其显示在 #content 框中。
或者,你可以做一个 hack-around(真的不安全,因为任何人都可以从控制台更改你的 javascript)
您可以有一个 php 文件,其中包含 eval 函数 并返回执行,并且然后你可以动态调用它。像这样,
你的 php 将包含
< 行中的内容? eval($_GET['代码']); ?>
我只是回答你的问题,我不建议使用它,因为任何人都可以注入各种不安全的代码。但是,如果您打算只自己使用它,那么可能值得一试。
祝你好运!
jQuery does DOM and AJAX, it doesn't execute PHP, so you would have to call a script return the HTML output and display it in your #content box.
Alternatively, you could do a hack-around (really unsafe stuff, because anybody can change your javascript from the console)
You could have a php file which has the eval function and returns the execution, and then you can call it dynamically. Like this,
And your php would contain something in the lines of
<? eval($_GET['code']); ?>
I was just answering your question, I do not recommend using it because anybody could inject all kinds of unsecure code. However, If you plan using it only yourself, it might be worth a shot.
Good luck!
您提出问题的方式意味着您尚未完全理解网页的客户端-服务器性质以及代码各部分的运行顺序。
您的问题的解决方案是一种称为 Ajax 的技术。使用 JQuery 实现起来非常简单。
简而言之,它允许您从 Javascript 代码中对新的 PHP 调用(或任何其他服务器端代码)进行新的调用,而无需重新加载整个页面;来自 PHP 调用的响应将提供给您的 Javascript,而不是简单地作为新页面加载到浏览器窗口中。然后 JavaScript 代码就可以随意使用;通常,这意味着将其作为新的内容块插入到现有页面中——这基本上就是您所要求的。
事情远不止这些;更多我可以在这里简短回答解释:我建议你调查一下。 Google 搜索“Jquery PHP Ajax 教程”或类似内容,我相信您会找到一些有用的提示。
[编辑] 根据请求,举个例子:
在您的 Javascript 代码中,您可以有如下内容:
显然,您需要在页面中拥有一个 id 为“my_button”的按钮和一个 id 为“my_container_div”的元素。此 JQuery 代码将向按钮添加一个单击事件,这样当您单击它时,就会运行 Ajax 代码。
Ajax 代码调用您单独的 PHP 程序,当该程序返回一些内容(无论该内容是什么)时,它将显示到容器 div 中。
PHP 程序与生成原始页面的主 PHP 程序完全分开,并且无法访问其任何变量(当然会话数据除外)。您可以让此 PHP 代码返回您喜欢的任何内容。通常它是一段 HTML 代码或一段文本。通常它采用 JSON 代码的形式,它是一个可以转换为 Javascript 变量的数组。这允许您在 PHP 和 Javascript 环境之间共享数据。
正如我所说,这是一个非常大的话题。还有更多内容,但您需要自己进行调查。我希望这对您有所帮助。
注意:我已经测试了上面的代码。有用。显然,您还需要一个能够生成一些输出的 PHP 程序。
The way you've asked your question implies that you haven't fully understood the client-server nature of a web page, and the order in which parts of the code are run.
The solution to your problem is a technique called Ajax. It is extremely simple to do using JQuery.
In short, it allows you to make a fresh call to a new PHP call (or any other server-side code) from within your Javascript code, without having to reload the whole page; the response that comes from the PHP call is given to your Javascript instead of simply being loaded into the browser window as a new page. The Javascript code is then free to use it however it likes; often this means inserting it into the existing page as a new block of content -- which is basically what you're asking for.
There's a lot more to it than that; more I can explain in a short answer here: I suggest you investigate it. Google for "Jquery PHP Ajax tutorials", or similar, and I'm sure you'll come up with some helpful tips.
[EDIT] As request, an example:
In your Javascript code, you could have something like the following:
Obviously, you would need to have a button with id of 'my_button' and an element with id of 'my_container_div' in the page. This JQuery code will add a click event to the button such that when you click on it, the Ajax code is run.
The Ajax code calls your separate PHP program, and when that program returns some content (whatever that content happens to be), it will be displayed into the container div.
The PHP program is entirely separate from your main PHP program that generated the original page, and does not have access to any of its variables (other than session data of course). You can have this PHP code return anything you like. Usually it would be a snippet of HTML code, or a chunk of text. Often it is in the form of JSON code, which is an array that can be converted into Javascript variables. This allows you to share data between your PHP and Javascript environments.
As I said, this is a very large topic. There's lots more to it, but you'll need to investigate it for yourself. I hope this has helped get you started though.
NB: I have tested the above code. It works. You do obviously also need a PHP program that will generate some output.
编辑:
//这应该将您的 php 返回的任何内容放入#content,但它不会每次都重新运行 php,它将与页面的其余部分一起呈现/
EDIT:
//this should put whatever your php returns into #content, BUT it won't re-run the php every time, it'll be rendered with the rest of the page/