javascript - 运费计算器不工作

标签 javascript php wordpress function shipping

我正在使用这个网站的运费计算器 MY Work Site .我在 wordpress 的根目录下添加了以下代码

<?php
$zip = "";
$weight = "";
$qty = "";

if (isset($_GET['z']))
{
$zip = $_GET['z'];
}

if (isset($_GET['w']))
{
$weight = $_GET['w'];
}

if (isset($_GET['q']))
{
$qty = $_GET['q'];
}

$site_url = "http://www.marketumbrellasite.com/GetRate.aspx?w=".$weight."&q=".$qty."&    z=".$zip;

$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $site_url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

ob_start();
curl_exec($ch);
curl_close($ch);
$result = ob_get_contents();
ob_end_clean();

if($result != "")
{   
echo $result;
} else {
echo "An error occurred.<br>Please try again later.";
}
?>

然后将以下代码添加到我的 functions.php

<?php
add_action('wp_head', 'head_scripts');
function head_scripts() {
echo '<div id="comm100-button-121"></div>
<script type="text/javascript">// <![CDATA[
var Comm100API = Comm100API || new Object;
Comm100API.chat_buttons = Comm100API.chat_buttons || [];
var comm100_chatButton = new Object;
comm100_chatButton.code_plan = 121;
comm100_chatButton.div_id = \'comm100-button-121\';
Comm100API.chat_buttons.push(comm100_chatButton);
Comm100API.site_id = 124523;
Comm100API.main_code_plan = 121;
var comm100_lc = document.createElement(\'script\');
comm100_lc.type = \'text/javascript\';
comm100_lc.async = true;
comm100_lc.src = \'https://chatserver.comm100.com/livechat.ashx?siteId=\' +     Comm100API.site_id;
var comm100_s = document.getElementsByTagName(\'script\')[0];
comm100_s.parentNode.insertBefore(comm100_lc, comm100_s);
// ]]></script>';
echo ' <script src="'. get_stylesheet_directory_uri() . '/js/UPSRates.js"   type="text/javascript"></script>';
}


/* Shipping Calculator Shortcode [shipping-calculator weight="#"]
----------------------------------------------------------------------------------------*/
add_shortcode('shipping-calculator', 'ag_shipping_calculator');
function ag_shipping_calculator( $atts ) {

extract( shortcode_atts( array(
  'weight' => ''
  ), $atts ) );

return '<a id="calc" style="font-weight:bold;color:#C20000;    cursor:pointer;">Calculate Shipping</a>
  <div id="calcShipping" style="display:none; width:250px;">
  Zip Code: <input type="text" id="zip" name="zip" maxlength="5" style="width:82px;"     /><br />
Quantity: &nbsp;<input type="text" id="qty" name="qty" value="1" maxlength="3"     style="width:32px;" /><br />          
<input type="hidden" id="weight" name="weight" value="'.$weight.'"/>
<button type="button" onclick="loadRates()">Calculate</button> <span id="ajaxloader"     style="display:none;"><img src="/img/ajax-loader.gif" alt="Loading..."     /></span>
<br />We charge no sales tax nor handling fees. We guarantee the internet\'s lowest     out-the-door price on this item.
<br />
<script>
jQuery(document).ready(function() {
jQuery("#calc").click(function () {
    jQuery("#calcShipping").toggle("fast");
});
});
</script>
<br />          
</div>
<div id="shipRateDiv"></div>
';
}

然后这个 js 在我的主题中的 js 文件夹中

function loadRates() {
document.getElementById("ajaxloader").style.display = 'inline';
var zip = document.getElementById("zip").value;
var weight = document.getElementById("weight").value;
var qty = document.getElementById("qty").value;
var url = "/GetRate.php?z="+zip+"&w="+weight+"&q="+qty;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4) {
        document.getElementById("shipRateDiv").innerHTML = xmlhttp.responseText+"    <br><br>";
        document.getElementById("ajaxloader").style.display = 'none';
     jQuery("#calcShipping").hide("slow");
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();         
}

然后我在页面的订购按钮下添加了以下简码

[shipping-calculator weight="#"]

当我点击它时,要计算的表格即将出现,当我尝试这个邮政编码“85298”时。它的结果是找不到该页面。我遵循了与此 Site 相同的事情。这是有效的。我不知道我在哪里犯了错误。请有人帮忙。谢谢!!

最佳答案

请注意,您的 Url 是相对的,因此 /GetRate.php 将被解析为绝对 Url http://yoursite/GetRate.php(因为/在开始)

如果您希望绝对 Url 为(例如)http://yoursite/portambrella/GetRate.php,您应该将相对 url 更改为 /portambrella/GetRate.php

看看这个:http://www.webreference.com/html/tutorial2/3.html

关于javascript - 运费计算器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24218920/

相关文章:

php - 在 CentOS 5.5 上为 php 5.3 安装 ldap 时出错

php - 通过 SQL 检索库存中所有没有图像的 WooCommerce 产品

php - WooCommerce 按国家或地区限制产品下载

javascript - 如何在此 slider 上访问时添加随机图像?

javascript - 使用 for 循环通过 array.length 向后循环 javascript 数组

php - Shopware 6 中是否有订单状态更改事件?

php - 如何比较查询中的 MySQL int 值而不获取它

javascript - 将表单设置为 $pristine,但保持当前值

javascript - 是否有任何规范说明什么仍然被视为有效的 JSONP?

php - 如何删除/删除标题中的 WordPress feed url?