Reputation: 3
In my index file where the form and function are located, named:
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>';
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. already tried this code: $name = $_POST['rename'];
then included '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. 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.
Upvotes: 0
Views: 151
Reputation: 12101
you need to add , for example, ' for first time response
echo $acct;
echo '</select>';
echo '<input type="submit id="submit" value="" />';
than add 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
if(isset($_POST['rename'])) {
$name = $_POST['name'];
//work with base
}
Upvotes: 1
Reputation: 1164
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:
$("#btn").click(function(){
$.ajax({
type: "POST",
url: "server.php",
data: "value=" + $('option:selected').val(),
success: function(b){
alert('Sent successfully!');
}
})
})
Upvotes: 0
Reputation: 10469
How's this? Give your form an id attribute and replace the formid with it :)
$(function() {
$.ajax({
type: "POST",
url: "invtype.php",
data: "getrevenuetype=true&selectedoption=" + $('#formid option:selected').val(),
success: function(b){
$("#revenueType").html(b)
}
})
})
Upvotes: 0