Javascript 单击单选按钮后更新值,还需要会话变量中的值
我有两个单选按钮。一个用于已发货,另一个用于未发货。如果我单击第一个单选按钮,则金额100应添加到总价中,如果我再次单击未发货,则金额0应添加到旧总价中。更新后的值应在会话变量中 我的代码是:Javascript 单击单选按钮后更新值,还需要会话变量中的值,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有两个单选按钮。一个用于已发货,另一个用于未发货。如果我单击第一个单选按钮,则金额100应添加到总价中,如果我再次单击未发货,则金额0应添加到旧总价中。更新后的值应在会话变量中 我的代码是: <?php $_SESSION['fulltotalofferprice1']=$fulltotalofferprice; ?> <input name="shipping" type="radio" id="RadioGroup1_0" value="100" onclick="
<?php
$_SESSION['fulltotalofferprice1']=$fulltotalofferprice;
?>
<input name="shipping" type="radio" id="RadioGroup1_0" value="100" onclick="updateTotal(this.value)" />Shipped
<input type="radio" name="shipping" value="0" id="RadioGroup1_0" checked="checked" onclick="updateTotal(this.value);" />Not Shipped
<tr><td>total Offer price: </td><td><span id="total"></span></td></tr>
装船
未装运
总报价:
我的javascript是:
<script>
var total_without_shipping = '<?php echo $_SESSION['fulltotalofferprice1'];?>';
function updateTotal(shipping_cost) {
var total_cost = total_without_shipping + shipping_cost;
document.getElementById('total').innerHTML = total_cost;
}
</script>
无装运的var总价值=“”;
功能更新总计(运输成本){
var总成本=总成本(不含装运+装运成本);
document.getElementById('total')。innerHTML=总成本;
}
我还需要在会议期间更新价格。通过它,我可以在以后的页面中使用它。所以您可能需要这样的内容: 首先,让我们从收音机中删除
onclick
,删除id
,因为id
属性在文档中应该始终是唯一的:
<?php
$_SESSION['fulltotalofferprice1']=$fulltotalofferprice;
?>
<input name="shipping" type="radio" value="100"/>Shipped
<input type="radio" name="shipping" value="0" checked="checked"/>Not Shipped
<tr><td>total Offer price: </td><td><span id="total"></span></td></tr>
剩下的就看你了,祝你好运,永远不要停止学习。要更新PHP会话中的内容,你必须通过AJAX或发布整个会话来执行后端调用。页面上更新的价目表本身有什么问题?您提供的代码有什么问题?谢谢您的回复…它没有给出真实的计算结果…而是-如果没有它,您就没有$\u会话-但是在那之后,您的代码就没有意义了-您的第一行中的$fulltotalofferprice是从哪里获得的???感谢您的回复…$fulltotalofferprice来自数据库查询。
<script type="text/javascript">
//We don't want this to change, so lets keep this as a "const"
const originalTotal = '<?php echo $_SESSION['fulltotalofferprice1'];?>';
//We add a listener to changes in the radios
$( 'input[name="shipping"]' ).on('click change', function () {
//the changed/clicked radio
let that = $( this );
//if the radio is checked
if( that.is( ':checked' ) ) {
//Add the radio value to the original value
let total = originalTotal + that.val();
//Set the text of the span
$( '#total' ).text( total );
//update the session var with a ajax post
$.post('update-session-total-shipping.php', { total });
}
} );
</script>
//check if the total has a value
if( isset( $_POST[ 'total' ] ) )
{
//set as the new total
$_SESSION[ 'fulltotalofferprice1' ] = $_POST[ 'total' ];
}