I have a form that has some text fields and 2 drop-down lists, from the first drop-down list I need to fetch the selected value and store it in a PHP variable so I can use that variable to fetch another set of data into the 2nd drop-down. I do not want to use the submit button. I have tried using ajax, I am getting the value as chosen in the console, but not able to fetch the same into a PHP variable.
我有一个包含一些文本字段和2个下拉列表的表单,从第一个下拉列表中我需要获取所选值并将其存储在PHP变量中,以便我可以使用该变量将另一组数据提取到第二次下拉。我不想使用提交按钮。我已经尝试使用ajax,我在控制台中获得了所选的值,但是无法将其提取到PHP变量中。
//1st Drop-down
<dl>
<dt>Project ID</dt>
<dd>
<!-- Function to select the dropdown value -->
<script type="text/javascript" src="<?php echo
url_for('/public/js/jquery.js'); ?>"></script>
<script type="text/javascript">
function fetch_select(val)
{
console.log(val);
$.ajax({
type: "POST",
url: "/flat_booking_again/adminsAccess.php",
data: {
get_option:val
}, success: function(response){
console.log(response);
document.getElementById("new_select").innerHTML=response;
//alert(get_option);
}
});
}
</script>
<select name = "project_id" style ="width:150px" onchange="fetch_select(this.value);">
<option>
<?php
$result = find_projects_without_admins();
foreach($result as $row) { ?>
<option value="<?php echo $row['project_id']; ?>" selected="selected"><?php echo $row['project_id']; ?></option>
<?php }?>
</option>
</select>
</dd>
</dl>
<dl>
//I need to use the fetched data from above to populate this drop-down
<dt>Block ID</dt>
<dd>
<select id="new_select" name="block_id" style ="width:150px">
<option>
<?php
echo $_POST['get_option'] . "hellllllllllllo"; //Not able to fetch value
if(isset($_POST['get_option'])){
$projectId = $_POST['get_option'];
$result = find_blocks_without_admins($projectId);
foreach($result as $row) { ?>
<option value="<?php echo $row['block_id']; ?>" selected="selected"><?php echo $row['block_id']; ?></option>
<?php }
}
?>
</option>
</select>
I am really new to PHP and AJAX sp pardon me for any silly mistakes. I know this may sound like a repeated question, but I am stuck with this bug for long, any help is really appreciated. Thanks !
我是PHP和AJAX的新手,请原谅我任何愚蠢的错误。我知道这可能听起来像一个重复的问题,但我长期坚持这个错误,任何帮助真的很感激。谢谢 !
1 个解决方案
#1
-1
Change your code with this.
用这个改变你的代码。
<dl>
<dt>Project ID</dt>
<dd>
<!-- Function to select the dropdown value -->
<script type="text/javascript" src="<?php echo url_for('/public/js/jquery.js'); ?>"></script>
<script type="text/javascript">
function fetch_select(val)
{
console.log(val);
$.ajax({
type: "POST",
url: "/flat_booking_again/adminsAccess.php",
data: {
get_option:val
}, success: function(response){
console.log(response);
document.getElementById("new_select").innerHTML=response;
//alert(get_option);
}
});
}
</script>
<select name = "project_id" style ="width:150px" onchange="fetch_select(this.value);">
<option>
<?php
$result = find_projects_without_admins();
foreach($result as $row) { ?>
<option value="<?php echo $row['project_id']; ?>" selected="selected"><?php echo $row['project_id']; ?></option>
<?php }?>
</option>
</select>
</dd>
</dl>
<dl>
//I need to use the fetched data from above to populate this drop-down
<dt>Block ID</dt>
<dd>
<select id="new_select" name="block_id" style ="width:150px"></select>
<?php
//echo $_POST['get_option'] . "hellllllllllllo"; //Not able to fetch value
if(isset($_POST['get_option'])){
$projectId = $_POST['get_option'];
$result = find_blocks_without_admins($projectId);
$html = '';
foreach($result as $row) { ?>
$html.="<option value='".$row['block_id']."' selected='selected'>".$row['block_id']."</option>";
}
echo $html;
}
?>
#1
-1
Change your code with this.
用这个改变你的代码。
<dl>
<dt>Project ID</dt>
<dd>
<!-- Function to select the dropdown value -->
<script type="text/javascript" src="<?php echo url_for('/public/js/jquery.js'); ?>"></script>
<script type="text/javascript">
function fetch_select(val)
{
console.log(val);
$.ajax({
type: "POST",
url: "/flat_booking_again/adminsAccess.php",
data: {
get_option:val
}, success: function(response){
console.log(response);
document.getElementById("new_select").innerHTML=response;
//alert(get_option);
}
});
}
</script>
<select name = "project_id" style ="width:150px" onchange="fetch_select(this.value);">
<option>
<?php
$result = find_projects_without_admins();
foreach($result as $row) { ?>
<option value="<?php echo $row['project_id']; ?>" selected="selected"><?php echo $row['project_id']; ?></option>
<?php }?>
</option>
</select>
</dd>
</dl>
<dl>
//I need to use the fetched data from above to populate this drop-down
<dt>Block ID</dt>
<dd>
<select id="new_select" name="block_id" style ="width:150px"></select>
<?php
//echo $_POST['get_option'] . "hellllllllllllo"; //Not able to fetch value
if(isset($_POST['get_option'])){
$projectId = $_POST['get_option'];
$result = find_blocks_without_admins($projectId);
$html = '';
foreach($result as $row) { ?>
$html.="<option value='".$row['block_id']."' selected='selected'>".$row['block_id']."</option>";
}
echo $html;
}
?>