John Davidson

html - Can I pass a value of an option to the same file in php with Ajax?

0 comments
Message:


I am making a room reservation system in PHP. I'm new to Ajax and PHP. I want to send the value of a selected option to the PHP section of my file which is at the top so that I can run a query with it. I tried using Ajax but it seems like it's not working.


if(isset($_POST['value'])){
$room_id = $_POST['value'];
echo "<script>alert('$room_id')</script>";
}

This is the part of my PHP code.


<select required class="form-select form-select-lg" name="room" id="room">
<option selected disabled value="">Select a room</option>
<?php
foreach($options as $option){
?>
<option value=<?php echo $option['room_id'];?> ><?php echo $option['room_name']?></option>
<?php
}
?>
</select>
<script type="text/javascript">
$('#room').on('change', function(){
var value = this.value;
$.ajax({
url: '',
type: 'POST',
data: {
'value': value
},

success: function(data){
// alert(data);
}
});
})
</script>

The alert(data) part of the code works just fine. It pops up an alert with the value of the selected option but the alert in the php section of my code does not output anything. Is there anything that I'm doing wrong?



My Answer:
"Value received: " . $room_id;

Rate this post

5 of 5 based on 7509 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us