繁体   English   中英

获取用户选择的期权的价值

[英]Getting the value of option chosen by the user

在我的表单和函数所在的索引文件中,名为:

index.php

$(function() {
    $.ajax({
        type: "POST",
        url: "invtype.php",
        data: "getrevenuetype=true",
        success: function(b){
            $("#revenueType").html(b)
        }
    })
})

<span name="revenueType" id="revenueType"></span><br/>

invtype.php

<?php

mysql_connect("","","") or die('Error: '.mysql_error());
mysql_select_db("dbname");

if(isset($_POST['getrevenuetype'])) {
    $sql3 = mysql_query("SELECT * FROM chartofaccount WHERE accountnumber >= 4000 and accountnumber <= 4999");
    $acct = '';
    $acctrow = mysql_num_rows($sql3);
    if($acctrow > 0) {

    echo 'Revenue Type:<select name="rename">';
        while($chartrow = mysql_fetch_array($sql3)) {
        $revenueaccountname = $chartrow['accountname'];

    $acct .= '<option value="$revenueaccountname"> '. $revenueaccountname .' </option>';

    }

    }
}

echo $acct;
echo '</select>';

我的问题是,如何获取或应该使用什么代码来获取用户选择的选项的值? 我获取值的目的是,我想将其放在另一个php中,在将数据插入MySQL时将用作var。 已经尝试过以下代码: $name = $_POST['rename']; 然后包含'invtype.php'; 在另一个php文件上(这是我将使用所选选项的值的位置),但它似乎不起作用。 我知道我的代码都搞砸了,对此我感到非常抱歉,但是如果您能帮助我,我将非常感谢。

您需要添加“例如,用于首次响应

echo $acct;
echo '</select>';
echo '<input type="submit id="submit" value="" />';

比添加js

$('#sumbit').click(function(){
 var name = $('select[name=rename]').val();

   $.ajax({
      type: "POST",
      url: "invtype.php",
      data: "rename=1&name="+name,
      success: function(b){
        $("#revenueType").html(b)
    }
   })

});

比更改invtype.php

if(isset($_POST['rename'])) {
 $name = $_POST['name'];

 //work with base 
}

这个怎么样? 给您的表单一个id属性,并将其替换为:)

$(function() {
    $.ajax({
        type: "POST",
        url: "invtype.php",
        data: "getrevenuetype=true&selectedoption=" + $('#formid option:selected').val(),
        success: function(b){
            $("#revenueType").html(b)
        }
    })
})

select标记添加到页面并由用户选择一个option ,您可以通过单击按钮发送所选值,如下所示:

$("#btn").click(function(){
  $.ajax({
        type: "POST",
        url: "server.php",
        data: "value=" + $('option:selected').val(),
        success: function(b){
            alert('Sent successfully!');
        }
    })
})
$('select[name="rename"] option:select').val()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM