0

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

索引.php

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

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

输入类型.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 文件上(这是我将使用所选选项的值的地方),但它似乎不起作用。我知道我的代码都搞砸了,对此我感到非常抱歉,但是如果您能提供帮助,我将不胜感激。

4

4 回答 4

1

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

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 
}
于 2012-07-30T12:45:36.057 回答
0

这个怎么样?给你的表单一个 id 属性并用它替换 formid :)

$(function() {
    $.ajax({
        type: "POST",
        url: "invtype.php",
        data: "getrevenuetype=true&selectedoption=" + $('#formid option:selected').val(),
        success: function(b){
            $("#revenueType").html(b)
        }
    })
})
于 2012-07-30T12:38:10.273 回答
0

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

$("#btn").click(function(){
  $.ajax({
        type: "POST",
        url: "server.php",
        data: "value=" + $('option:selected').val(),
        success: function(b){
            alert('Sent successfully!');
        }
    })
})
于 2012-07-30T12:41:04.983 回答
0
$('select[name="rename"] option:select').val()
于 2012-07-30T12:42:13.463 回答