JQUERY onchange()获得选择选项并传递给变量以插入到Mysql数据库中

时间:2014-07-11 13:34:55

标签: javascript php jquery mysql

我正在尝试从下拉菜单向下菜单中获取所选值,根据选择创建一个值作为变量,并将变量的值插入到MYSQL表中。

这是我目前的代码:

HTML / PHP

<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">Afghanistan</option>
                <option value="Åland Islands">Åland Islands</option>
                <option value="Albania">Albania</option>
                <option value="Algeria">Algeria</option>
                <option value="American Samoa">American Samoa</option>
                <option value="Andorra" selected>Andorra</option>
...

</select>

<?PHP 

$shipping = */ post javascript variable here */

$sql = mysql_query('UPDATE orders
SET shipping=$shipping
WHERE order_id='$r[order_id']');

?>

<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>

JQUERY

$(document).ready(function(){
    // show popup when selecting a country from the drop-down
    $( ".target" ).change(function() {
     if($(this).val() != "United Kingdom") {
        var docHeight = $(document).height();
        var scrollTop = $(window).scrollTop(); 
        $('.overlay-bg').show().css({'height' : docHeight}); 
        $('.overlay-content').css({'top': scrollTop+20+'px'});
     }
    });

代码将执行以下操作:

  • 获取所选选项的值
  • 如果选项不是英国 - 将应用以下两个条件之一

条件1

如果选择的选项是阿尔巴尼亚,阿尔及利亚,阿富汗 -  将创建一个值为£15.00

的变量

条件2

如果选择的选项是美属萨摩亚,奥兰群岛 -  将创建一个值为£10.00

的变量
  • 然后使用基于给定order_id的UPDATE语句将此值输入MYSQL表。

修改

是否有可能回显javascript值然后由服务器存储为PHP变量?

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

免责声明:这不是一个完整的答案,因为你没有提供足够的信息甚至给出一个。我建议对你的javascript进行一些修复,并为不同的国家/地区提供检查。

$(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专家所以这可能是也可能不完全正确。

这样的事情:从HERE

拉出来
<?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);
?>

<强>的问题: 这里没有order_id,我可以看到。

答案 1 :(得分:0)

根据你可以获得反对的钱来存储价格data attribute

HTML

<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>

JS

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);
});

DEMO

相关问题