将Javascript变量显示为PHP变量
In my form action there's a url: www.url.com/?quantity=$quantity
And in the form there's a select box where customers choose the quantity.
<form method="post" name="jform" action="www.url.com/?quantity=$quantity">
<select class="font_12" id="quantity" name="quantity">
<option value="10">10 PCs</option>
<option value="25">25 PCs</option>
<option value="50">50 PCs</option>
<option value="99">99 PCs</option>
</select>
I am trying to get the value in the select box using ajax, and then display into the action form url. I did a alert and it works, I am getting the value of the select box. But I don't know how to put this vaue into the PHP varaible $quantity?
Here's my Ajax code:
$('#quantity').on('change', function() {
var val = $(this).val();
if(val != '') {
$.get('index.php', {'quantity' : val}, function(resp) {
alert(val);
});
}
});
Actually I want it to change the php variable right away when the quantity in the select box change before submitting the form.
Any help?
在我的表单操作中有一个网址: www.url.com/?quantity=$quantity strong> p>
在表单中有一个选择框,供客户选择数量。 p>
我正在尝试使用ajax在选择框中获取值 ,然后显示到动作表单url。 我做了一个警报并且它有效,我得到了选择框的值。
但是我不知道如何将这个vaue放入PHP变量 $ quantity strong>? p>
这是我的Ajax代码: p>
实际上我希望它在提交表单之前当选择框中的数量发生变化时立即更改php变量 。 p>
有任何帮助吗? p>
div>
&lt; form method =“post”name =“jform”action =“www.url.com/?quantity=$quantity”&gt;
&lt; select class =“font_12”id =“quantity”name =“quantity”&gt;
&lt; option value =“ 10“&gt; 10台电脑&lt; /选项&gt;
&lt;选项值=”25“&gt; 25台电脑&lt; /选项&gt;
&lt;选项值=”50“&gt; 50台电脑&lt; / option&gt;
&lt; 选项值=“99”&gt; 99台&lt; /选项&gt;
&lt; / select&gt;
code> pre>
$('#quantity')。on('change',function(){
var val = $(this) .val();
if(val!=''){
$ .get('index.php',{'quantity':val},function(resp){
alert(val);
} );
}
});
code> pre>
Use $_GET
If your URL is ?quantity=###
then just use $_GET['quantity']
in your PHP code.
To change the action attribute on the form when you change the quantity you can just put the following inside your onchange event:
$('form[name="jform"]').attr('action','http://url.com/?quantity=' + val);