简体   繁体   English

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

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

In my index file where the form and function are located, named: 在我的表单和函数所在的索引文件中,名为:

index.php 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 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>';

My question is how will I get or what code should I put on to get the value of the option selected by the user? 我的问题是,如何获取或应该使用什么代码来获取用户选择的选项的值? My purpose in getting the value is that I want to put it in another php where it will be used as var in inserting data into MySQL. 我获取值的目的是,我想将其放在另一个php中,在将数据插入MySQL时将用作var。 already tried this code: $name = $_POST['rename']; 已经尝试过以下代码: $name = $_POST['rename']; then included 'invtype.php'; 然后包含'invtype.php'; on the other php file (this is where I will use the value of option selected) but it doesn't seem to work. 在另一个php文件上(这是我将使用所选选项的值的位置),但它似乎不起作用。 I know my code is all messed up and I'm really sorry about it, but guys if you can help I would really appreciate it. 我知道我的代码都搞砸了,对此我感到非常抱歉,但是如果您能帮助我,我将非常感谢。

you need to add , for example, ' for first time response 您需要添加“例如,用于首次响应

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

than add js 比添加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)
    }
   })

});

than change invtype.php 比更改invtype.php

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

 //work with base 
}

How's this? 这个怎么样? Give your form an id attribute and replace the formid with it :) 给您的表单一个id属性,并将其替换为:)

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

After the select tag is added to the page and an option is selected by the user, you can then send the selected value with a button click like this: 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