将Javascript .append()变量分配给PHP变量

时间:2014-07-14 09:47:59

标签: javascript php jquery html

如何将以下Javascipt变量'money'作为PHP变量存储在同一页面上。

JS

$(document).ready(function(){

var money = 0;
$(".target").change(function () {
var optionSelected = $("option:selected", this);
money = optionSelected.attr("data-price");
alert(money);
$( ".overlay-content" ).append(money);
 // show popup when selecting a country from the drop-down
$('.overlay-bg').show().css({'height' : docHeight}); 
$('.overlay-content').css({'top': scrollTop+20+'px'});
});
});

HTML

<select id="customer_country" name="customer_country"
                    class="validate[required] input_styling target"
                    style="background: #FFFFFF;">
                <option value="">Please Select a Country</option>
                <option value="Afghanistan" data-price="29.95">Afghanistan</option>
                <option value="Åland Islands" data-price="29.95">Åland Islands</option>
                <option value="Albania" data-price="29.95">Albania</option>
                <option value="Algeria" data-price="29.95">Algeria</option>
                <option value="American Samoa" data-price="29.95">American Samoa</option>
                <option value="Andorra" data-price="14.95">Andorra</option>
                <option value="Angola" data-price="29.95">Angola</option>
</select>

HTML - 叠加内容(弹出式广告 - DIV)

<div class="overlay-bg">
      <div class="overlay-content">
          <p>You have selected a country of residence outside of the United Kingdom.</p>
          <p>Please note we only accept credit card payments from UK customers.</p>
          <p>If you are a customer from outside the UK, please click the
          Paypal icon to be taken through to Paypal checkout to complete your order.</p>
          <td width="185" align="center" valign="middle">
            <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
            <input type="hidden" name="cmd" value="_xclick" />
            <input type="hidden" name="business" value="" />
            <input type="hidden" name="item_name" value="" />
            <input type="hidden" name="return" value="" />
            <input type="hidden" name="currency_code" value="GBP" />
            <input type="hidden" name="image_url" value="" />
            <input type="hidden" name="item_name" value="<?=$r['order_title_1']?>" />
            <input type="hidden" name="item_name" value="<?=$r['order_title_2']?>" />
            <input type="hidden" name="item_name" value="<?=$r['order_title_3']?>" />
            <input type="hidden" name="shipping" value="<?=$shipping?>" />
            <input type="hidden" name="amount" value="<?=$r['order_total']?>" />
            <input type="image" src=""
                   name="submit" alt="Make payments with PayPal - it's fast, free and secure!" />
            </form>
          <p>However, if you are a UK customer and simply made an error,
          please click below to return to the checkout page.</p>
          <p>Thank you</p>
          <button class="close-btn">Close</button>
        </div>
        </div>

目前我已设法将data-price值作为警报输出并附加到弹出式DIV中的<p>Thank you</p>元素。这纯粹是为了测试是否传递了正确的价值。

相反,我想将money JS变量存储为PHP变量,即$shipping。我试着按如下方式发布变量:

$.post('pay_cart1.php',{'data-price':money},function(result){ alert (result) }

然后使用$ _REQUEST作为PHP检索值,即

$shipping =$_REQUEST['data-price'];

但是,警报显示我服务器上随机CSS文件的内容,$_REQUEST变量数组为空并返回NULL /空值。

有人能协助吗?

2 个答案:

答案 0 :(得分:0)

试试这个,做一个 var_dump($ _ POST)

$.ajax({
    url : "pay_cart1.php",
    type: "POST",
    data : { data-price : money },
    success: function(data, textStatus, jqXHR)
    {
       //if success
    },
    error: function (jqXHR, textStatus, errorThrown)
    {
       //if error
    }
});

答案 1 :(得分:0)

请看一下this,因为你显然不明白php和js是如何工作的,这会让事情变得更容易一些。

然后,您可以在此处执行的操作,而不是仅向set that data-price value into shipping hidden input值发送数据到服务器,例如:

$("input[name='shipping']").val(money); 

这样您的隐藏输入将根据需要设置值。这就是你在客户端用js做的事情。