[英]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.