Javascript JQUERY onchange()获取所选选项并传递给要插入Mysql数据库的变量
我试图从下拉菜单中获取所选值,根据所选内容创建一个值作为变量,并将该变量的值插入MYSQL表中 以下是我当前的代码: HTML/PHP 该代码将执行以下操作: 获取所选选项的值 如果该选项不是英国-将应用以下两个条件之一 条件1 如果选择阿尔巴尼亚、阿尔及利亚、阿富汗- 将创建一个值为15.00英镑的变量 条件2 如果选择的选项是美属萨摩亚、阿兰群岛- 将创建一个值为10.00英镑的变量 然后,根据给定的order\u id,使用UPDATE语句将该值输入MYSQL表。 编辑 是否可以回显javascript值,然后服务器将其存储为PHP变量 任何帮助都将不胜感激 免责声明:这不是一个完整的答案,因为您没有提供足够的信息。我建议对您的javascript进行一些修复,并提供一种针对不同国家进行检查的方法Javascript JQUERY onchange()获取所选选项并传递给要插入Mysql数据库的变量,javascript,php,jquery,mysql,Javascript,Php,Jquery,Mysql,我试图从下拉菜单中获取所选值,根据所选内容创建一个值作为变量,并将该变量的值插入MYSQL表中 以下是我当前的代码: HTML/PHP 该代码将执行以下操作: 获取所选选项的值 如果该选项不是英国-将应用以下两个条件之一 条件1 如果选择阿尔巴尼亚、阿尔及利亚、阿富汗- 将创建一个值为15.00英镑的变量 条件2 如果选择的选项是美属萨摩亚、阿兰群岛- 将创建一个值为10.00英镑的变量 然后,根据给定的order\u id,使用UPDATE语句将该值输入MYSQL表。 编辑 是否可以回显jav
$(function() { // function in closure - should be where you instantiate event listeners
// show popup when selecting a country from the drop-down
$(".target").on('change', function() { // correct / updated syntax for 'change' function
//finding the selected one...
var opt = $(this).find(':selected').text(), // finds the selected options text
money = 0;
switch(opt){ // simple switch on the option where you set money
case "Albania":
case "Algeria":
case "Afghanistan":
money = 15.00;
break;
case "American Samoa":
case "Aland Islands":
money = 10.00
break;
case "United Kingdom":
break;
default:
break;
}
});
});
编辑:决定写更多:
发送数据:
您需要发出ajax请求,将数据发送回服务器端,以便运行INSERT。传递货币作为参数
您可以尝试以下方法:
$.post('whatever-file-name.php', {
moneySent: JSON.stringify(money)}, function(res){
alert(res);
});
插入数据:
我不是php专家,所以这可能完全正确,也可能不完全正确
类似这样的:从
问题:
这里没有我可以看到的订单id。将价格存储在数据属性中,基于此,您可以获得country的钱
HTML
是否创建了下拉列表dynamicaly@Bala-这是一个静态列表-这里到底是什么问题?@dreamweiver-我上面的问题已经列出了。具体查询什么?可以使用属性选择器检索所选值。在此之后,只需使用您的值创建您的var..谢谢-我如何将其发布到服务器以作为PHP变量进行发布和存储,而不是将其显示为警报?抱歉,我不知道任何服务器端语言
$(function() { // function in closure - should be where you instantiate event listeners
// show popup when selecting a country from the drop-down
$(".target").on('change', function() { // correct / updated syntax for 'change' function
//finding the selected one...
var opt = $(this).find(':selected').text(), // finds the selected options text
money = 0;
switch(opt){ // simple switch on the option where you set money
case "Albania":
case "Algeria":
case "Afghanistan":
money = 15.00;
break;
case "American Samoa":
case "Aland Islands":
money = 10.00
break;
case "United Kingdom":
break;
default:
break;
}
});
});
$.post('whatever-file-name.php', {
moneySent: JSON.stringify(money)}, function(res){
alert(res);
});
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"INSERT INTO some-table (id, order_id, money)
VALUES ('1', ?? ,".$_POST['moneySent'].")");
mysqli_close($con);
?>
<select id="customer_country" name="customer_country" class="validate[required] input_styling target" style="background: #FFFFFF;">
<option value="" data-price="0">Please Select a Country</option>
<option value="Afghanistan" data-price="15">Afghanistan</option>
<option value="Åland Islands" data-price="10">Åland Islands</option>
<option value="Albania" data-price="15">Albania</option>
<option value="Algeria" data-price="15">Algeria</option>
<option value="American Samoa" data-price="10">American Samoa</option>
<option value="Andorra" data-price="0" selected>Andorra</option>
</select>
var money = 0;
$(".target").change(function () {
// this.value; to get the selected country value
var optionSelected = $("option:selected", this);
money = optionSelected.data("price");
alert(money);
});