ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript – 运费计算器不工作

2019-08-30 20:31:17  阅读:314  来源: 互联网

标签:shipping javascript php function wordpress


我正在这个网站上运输计算器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

标签:shipping,javascript,php,function,wordpress
来源: https://codeday.me/bug/20190830/1770223.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有