如果可能的话,如何通过ajax调用设置php数组?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如果可能的话,如何通过ajax调用设置php数组?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2169字,纯文字阅读大概需要4分钟。
内容图文
![如果可能的话,如何通过ajax调用设置php数组?](/upload/InfoBanner/zyjiaocheng/653/51f6345ae6634e88b4bbcee8b471a729.jpg)
好的,所以我正在尝试从ajax请求中检索数据,并且可能的话设置一个php数组…我什至不确定这是否可能考虑一个是客户端而一个是服务器端…所以这是我的代码
在html页面上
<?php foreach ($products as $product): ?>
<li>
<h3><span><?php print $product["price"]; ?></span> (4) <?php print $product["name"]; ?> </h3>
<div class="container">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" width="42"><span class="qty"><?php print $product["quanitity"]; ?></span></td>
<td width="180">
........
我想遍历该数组中的所有产品,但是要获得这些产品,我需要像这样进行ajax调用
$.ajax({
type: 'post',
url: "/shop_systems/index.php?route=module/cart/get_all_products",
dataType: 'json',
data: {current : null, previous : these_ids, quantity : 1, multiple : true},
success: function (data) {
我在想是否有一种简单的方法可以做到这一点….我在想一个解决方案是在ajax调用的成功部分中编写html,但是我会有很多的append语句…任何更干净的方法将不胜感激
解决方法:
在PHP中使用cURL
PS:我建议您不要像以前那样混合使用html和php.不必要地很难调试和维护.
您可以使用以下代码(未经测试,但应该可以使用):
注意:将网址设为绝对网址,否则将无法正常工作
$post_data = array(
'current' => 'null',
'previous' => 'null',
'quantity' => '1',
'multiple' => 'true'
);
// Init curl thing
$ch = curl_init();
// Setup curl options
curl_setopt_array($ch, array(
CURLOPT_URL => '/shop_systems/index.php?route=module/cart/get_all_products',
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE, // Return content as string
CURLOPT_FOLLOWLOCATION => TRUE, // Follow redirects
CURLOPT_AUTOREFERER => TRUE, // Set referer field on redirect
CURLOPT_VERBOSE => TRUE,
CURLOPT_COOKIEJAR => '/tmp/curl_php_cookies',
CURLOPT_COOKIEFILE => '/tmp/curl_php_cookies',
CURLOPT_STDERR => fopen('/tmp/curl_php_log', 'w'),
CURLOPT_POSTFIELDS => $post_data
));
// Excecute request
$product = json_decode(curl_exec($ch));
$html = '';
foreach ($products as $product)
{
$html .= <<< HTML
<li>
<h3>
<span>{$product["price"]}</span>
(4) {$product["name"]}
</h3>
<div class="container">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" width="42"><span class="qty">{$product["quanitity"]}</span></td>
<td width="180">
HTML;
}
?>
内容总结
以上是互联网集市为您收集整理的如果可能的话,如何通过ajax调用设置php数组?全部内容,希望文章能够帮你解决如果可能的话,如何通过ajax调用设置php数组?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。