使用 PHP 循环更改 Jquery.Append
我想创建一个 PHP 循环来获取数据库表中的所有 id,并在 .append
函数中使用它们。基本上我想要发生的是对于通过数据库的每个循环,我希望 jquery.append 运行并将其 #div 标签更改为来自数据库中的字段 div-id
例如;
$('#div-id').append('HELLO CONTENT');
我知道我可以使用 .each
,但这并不能解决 PHP 部分。我可以在 PHP 上做一些工作,但这并不能解决 Jquery 部分。基本上我是这样看的,他们正在创建自己的循环,而不是一起工作。
I am wanting to create a PHP loop to get all of the ids in a database table and them utilize them in an .append
function. Basically what I want to happen is for every loop through the database, I want the jquery.append to run and change its #div labeling to be from the field div-id in the database
For example;
$('#div-id').append('HELLO CONTENT');
I know I can use an .each
, but that doesn't take care of the PHP part. I can do a while on the PHP, but that doesn't take care of the Jquery part. Basically how I see it, they are creating their own loops and not working together.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 PHP 页面输出的
标记中执行此操作。我假设标准 MySQL 应该很容易将其适应另一个数据库。
应该输出类似这样的内容:
...所以你正在使用 PHP 进行循环,并且只输出 JS 行,这将逐行执行。
或者,您可以在 PHP 中循环
$result
并创建一个大数组,然后使用json_encode()
它并在 JS 中循环它。你的选择。You can do it in a
<script>
tag output by the PHP page. I am assuming standard MySQL, should be fairly easy to adapt this to another database.Should output something like:
...so you're doing the loop with PHP, and just outputting lines of JS, which will execute one after the other.
Alternatively, you could loop
$result
in PHP and create one large array, thenjson_encode()
it and loop it in JS. Your choice.你不能像这样组合 PHP 和 JavaScript。 PHP 在服务器上运行,一旦完成就会生成一个(很可能)HTML 页面,并将其发送回请求浏览器。然后 HTML 被渲染到您看到的网页中,然后执行每个 JavaScript。至此,PHP早已执行完毕。
You cannot combine PHP and JavaScript like this. PHP is run on the server and once it's finished is has generated a (most likely) HTML page that it sends back to the requesting browser. Over there the HTML is then rendered into the webpage you see and every JavaScript is executed afterwards. At this point, PHP has long finished executing.