Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I do not use submit button

I want the selected value from the user send to the test1.php page
but when the user select option nothing happen in the page
and the value not send it to the test1.php page
Is there any mistake in my code or I missed something ?
please help

What I have tried:

test2.php
HTML
<?php
include_once 'database.php';
?>

<p> Filter By Category</p>
<select id="category" class="category">
	<?php	$query = mysqli_query($conn,"select * from category ");
		while($category = mysqli_fetch_array($query)) {
			echo "<option value='" . $category['cat_name'] . "'>" . $category['cat_name'] . "</option> ";
		}
	?>
</select>

<script>
$(function() {
	$('.category').change(function() {
		$.ajax({
			method: "POST",
			url: "test1.php",
			data: { category1: $(this).val() },
			success: function(data){
				console.log(data);
			}
		});
	});
});
</script>
test1.php
HTML
<?php
include_once 'database.php';

if (isset($_POST['category1'])) {
	$category = $_POST['category1'];
	echo $category;
}
?>
Posted
Updated 30-Mar-20 13:31pm
v3

1 solution

In general
What you are going to need to do is to use the debugger which is built into most browsers. You should be concerned with two tabs on it; network and console.

The network tab is going to be used to look at the network activity when you make a change in your select element. You can look to see what URL is being accessed, the data being sent, and the response that is received.

The console tab you are going to look to see if there are any errors in your javascript.

Reference:
Chrome DevTools  |  Tools for Web Developers  |  Google Developers[^]


Now for your code...
As you are not do an actual form submission, it is not going to change pages with what you have.
IF your code works as it is written, it is going to simple send the selected value to Test1, which will then return it verbatim.
And then Test2 is going to display that value in the javascript console.
And that's it

Also... Test1 has an include database reference, which is not used in the code
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900