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.
//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 !
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;
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.