如何传递变量从JavaScript到PHP? [英] How can I pass variables from JavaScript to PHP?

查看:156
本文介绍了如何传递变量从JavaScript到PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery来计算价格。这里是一个小片段我的JavaScript的:

I am using jQuery to calculate prices. Here is a small snippet of my JavaScript:

// Prices: radio
var curLam = "gloss";
$("input[name=lamination]").click(function(){
	var gloss = 5;
	var matt = 6;
	if ($(this).val() == 'gloss' && curLam != 'gloss'){
		$('#prices span').text(parseInt($('#prices span').text()) + gloss - matt);
		curLam = 'gloss';
		console.log('1');
	}
	if ($(this).val() == 'matt' && curLam != 'matt'){
		$('#prices span').text(parseInt($('#prices span').text()) - gloss + matt);
		curLam = 'matt';
		console.log('2');
	}
		$("#prices span").each(function(){
var $priceValue = $(this);
   	} )

});

这将检查,看是否有雾或光泽已被选中,然后将其插入价格进入范围价格 DIV 标记。

This checks to see if a matte or gloss finish has been selected and then it inserts the price into the span within the prices div tag.

我需要知道如何对价值分配到,然后可以传递到PHP我的购物车的变量。

I need to know how to assign that price value into a variable which can then be passed on to PHP for my shopping cart.

推荐答案

一般来说,你不应该选择价格返回到服务器。 HTML表单可以很容易伪造。它是更安全发送用户的选择返回到服务器,也知道应该花多少钱。

Generally speaking, you shouldn't return the selected price to the server. HTML forms can be easily faked. It is far safer to send the user's choice back to the server, which also knows how much it should cost.

基本上JavaScript验证(此实质上是)很方便,但不应该被信任。你已经有一个输入选择完成。只需发送回服务器。这样,你的网站仍然可以工作(或更好的工作),如果用户已禁用JavaScript。

Basically Javascript validation (which this essentially is) is convenient but shouldn't be trusted. You already have an input for selecting the finish. Just send that back to the server. That way your site will still work (or work better) if the user has Javascript disabled.

这篇关于如何传递变量从JavaScript到PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆