I am trying to get a country, state, city dependent selection boxes to work(once country is picked then state will populate with the related states then state is picked etc...) but I think there is something wrong with my jquery or ajax because my state box won't populate.
If you could help find where the code goes wrong I would appreciate it. Also I would like to know how the ajax posts back to the shippingaddress.php page.(is it in the success callback?) Do the select boxes need to be contained in a form or will they post with out it?
Thank you in advance for any help!
shippingaddress.php
<div class="select-boxes">
<form action="" method="post">
<?php
//Get all country data
$query = $conn->query("SELECT * FROM countries WHERE status = 1 ORDER BY
country_name ASC");
//Count total number of rows
$rowCount = $query->num_rows;
?>
<select name="country_id" id="country">
<option value="">Select Country</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option
value="'.$row['country_id'].'">'.$row['country_name'].'</option>';
}
}else{
echo '<option value="">Country not available</option>';
}
?>
</select>
<select name="state" id="state">
<option value="">Select country first</option>
</select>
<select name="city" id="city">
<option value="">Select state first</option>
</select>
</form>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#country').on('change',function(){
var countryID = $(this).val();
if(countryID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'country_id='+countryID,
success:function(html){
$('#state').html(html);
$('#city').html('<option value="">Select state first</option>');
}
});
}else{
$('#state').html('<option value="">Select country first</option>');
$('#city').html('<option value="">Select state first</option>');
}
});
$('#state').on('change',function(){
var stateID = $(this).val();
if(stateID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'state_id='+stateID,
success:function(html){
$('#city').html(html);
}
});
}else{
$('#city').html('<option value="">Select state first</option>');
}
});
});
</script>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
ajaxData.php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "secure_login";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST["country_id"]) && !empty($_POST["country_id"])){
//Get all state data
$query = $conn->query("SELECT * FROM states WHERE country_id =
".$_POST['country_id']." AND status = 1 ORDER BY state_name ASC");
//Count total number of rows
$rowCount = $query->num_rows;
//Display states list
if($rowCount > 0){
echo '<option value="">Select state</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['state_id'].'">'.$row['state_name'].'</option>';
}
}else{
echo '<option value="">State not available</option>';
}
}
if(isset($_POST["state_id"]) && !empty($_POST["state_id"])){
//Get all city data
$query = $conn->query("SELECT * FROM cities WHERE state_id =
".$_POST['state_id']." AND status = 1 ORDER BY city_name ASC");
//Count total number of rows
$rowCount = $query->num_rows;
//Display cities list
if($rowCount > 0){
echo '<option value="">Select city</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['city_id'].'">'.$row['city_name'].'</option>';
}
}else{
echo '<option value="">City not available</option>';
}
}
?>
Related
My code based on a snippet from codexworld.com
but I have a single database (coin_database) with following columns:
id|coin_name|coin_country|value
1 |MapleLeaf|Canada |5dollars
2 |Panda |China |10yuan
I would like to build 3 stage form where firstly you chose Country, than type of coins only from this country and after that available value (1oz, 5 dollars etc.).
I have 2 files:
index.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#country').on('change',function(){
var countryID = $(this).val();
if(countryID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'country='+countryID,
success:function(html){
$('#state').html(html);
$('#city').html('<option value="">Select state first</option>');
}
});
}else{
$('#state').html('<option value="">Select country first</option>');
$('#city').html('<option value="">Select state first</option>');
}
});
$('#state').on('change',function(){
var stateID = $(this).val();
if(stateID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'name='+stateID,
success:function(html){
$('#city').html(html);
}
});
}else{
$('#city').html('<option value="">Select state first</option>');
}
});
});
</script>
<?php
//Include the database configuration file
include 'db_conn.php';
//Fetch all the country data
$query = $conn->query("SELECT DISTINCT coin_country FROM coin_database ORDER BY coin_country ASC");
//Count total number of rows
$rowCount = $query->num_rows;
?>
<select id="country">
<option value="">Country</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['coin_country'].'">'.$row['coin_country'].'</option>';
}
}else{
echo '<option value="">Country not available</option>';
}
?>
</select>
<select id="state">
<option value="">Coin</option>
</select>
<select id="city">
<option value="">Value</option>
</select>
ajaxData.php
<?php
//Include the database configuration file
include 'db_conn.php';
if(!empty($_POST["country"])){
//Fetch all state data
$query = $conn->query("SELECT DISTINCT coin_name, coin_country, value, id FROM `coin_database` WHERE `coin_country` = 'Canada' ORDER BY `coin_name` DESC ");
//Count total number of rows
$rowCount = $query->num_rows;
//State option list
if($rowCount > 0){
echo '<option value="">Coin</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['id'].'">'.$row['coin_name'].'</option>';
}
}else{
echo '<option value="">Error</option>';
}
}
elseif(!empty($_POST["name"])){
//Fetch all city data
$query = $conn->query("SELECT DISTINCT `value` FROM `coin_database` WHERE `coin_name` = 'MapleLeaf' ORDER BY `id` DESC ");
//Count total number of rows
$rowCount = $query->num_rows;
//City option list
if($rowCount > 0){
echo '<option value="">Value</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['id'].'">'.$row['value'].'</option>';
}
}else{
echo '<option value="">Value not available</option>';
}
}
?>
It works well with not dynamic values (Canada, MapleLeaf) but I get an error when I try to put there variable.
I have a PHP code that retrieve data from MYSQL database and includes these retrieved data into a dropdown list where 2 dropdown list exist, the user select from the first one and based on his selection the second one will display the related data.
tbl_country:
country_id
country_name
tbl_state:
state_id
state_name
country_id
can anyone tell me where the error in the code?
code 1:
<?php
function load_country(){
$connect = mysqli_connect("localhost","****","***","test");
$output = '';
$sql = "select * from tbl_country";
$result = mysqli_query($connect,$sql);
// var_dump($result);
while ($row = mysqli_fetch_array($result)) {
$output.='<option value="'.$row["country_id"].'">'.$row["country_name"].'</option>';
}
return $output;
}
?>
<html>
<head>
<title>dropdown test</title>
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#country').change(function(){
var country_id = $(this).val();
$.ajax({
url:"dropdown_fetch.php",
method:"POST",
data:{countryId:country_id},
datatype:"text",
success:function(data){
$('#state').html(data);
}
});
});
});
</script>
</head>
<body>
<p>select owner
<select name="country" id="country">
<option value="">Select country</option>
<?php echo load_country(); ?>
</select></p>
<p>select state
<select name="state" id="state">
<option value="">Select state</option>
</select></p>
</body>
</html>
code 2:
<?php
$connect= mysqli_connect("localhost","****","****","test");
$output = '';
$sql = "select * from state where country_id = '".$_POST["countryId"]."' ORDER BY country_name";
$result = mysqli_query($connect,$sql);
//var_dump($_POST["ownerID"]);
$output='<option value="">Select state</option>';
while ($row = mysqli_fetch_array($result) {
$output.='<option value="'.$row["state_id"].'">'.$row["state_name"].'</option>';
}
echo $output;
?>
Here is a syntax error in your while loop.
Fix the line in code 2:
while ($row = mysqli_fetch_array($result)
to
while ($row = mysqli_fetch_array($result))
I'm trying to get a list of the "states" after I select the "country".
The list of countries appear correctly, but when I select the country the states don't actually show up in dropdown selection form.
I'm using ajax to send the data to ajaxData.php from index.php, but I can't figure out what I'm doing wrong.
Any help would be appreciated.
This is index.php
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#country').on('change',function(){
var countryID = $(this).val();
if(countryID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'marka_id='+countryID,
success:function(html){
$('#state').html(html);
$('#city').html('<option value="">Select state first</option>');
}
});
}else{
$('#state').html('<option value="">Select country first</option>');
$('#city').html('<option value="">Select state first</option>');
}
});
$('#state').on('change',function(){
var stateID = $(this).val();
if(stateID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'modeli_id='+stateID,
success:function(html){
$('#state').html(html);
}
});
}else{
'');
}
});
});
</script>
<?php
//Include database configuration file
include('dbConfig.php');
//Get all country data
$query = $db->query("SELECT * FROM marka WHERE `status` = 1 ORDER BY marka_name ASC");
//Count total number of rows
$rowCount = $query->num_rows;
?>
<select name="country" id="country">
<option value="">SelectMarken </option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['marka_id'].'">'.$row['marka_name'].'</option>';
}
}else{
echo '<option value="">Country doesnt exist</option>';
}
?>
</select>
<select name="state" id="state">
<option value="">Select state</option>
</select>
and this is : ajaxData.php
<?php
//Include database configuration file
include('dbConfig.php');
if(isset($_POST["marka_id"]) && !empty($_POST["marka_id"])){
//Get all state data
$query = $db->query("SELECT * FROM modeli WHERE `marka_id` = ".$_POST['marka_id']." AND `status` = 1 ORDER BY modeli_name ASC");
//Count total number of rows
$rowCount = $query->num_rows;
//Display states list
if($rowCount > 0){
echo '<option value="">Select modelin</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['modeli_id'].'">'.$row['modeli_name'].'</option>';
}
}else{
echo '<option value="">Modeli doesnt exist</option>';
}
}
?>
[UPDATED CODE] I forgot to post it immediately, so now this is what I have and it works.
ajaxData.php
<?php
//Include database configuration file
include('dbConfig.php');
if(isset($_POST["marka_id"]) && !empty($_POST["marka_id"])){
//Get all state data
$query = $db->query("SELECT * FROM modeli WHERE `marka_id` = ".$_POST['marka_id']." AND `status` = 1 ORDER BY modeli_name ASC");
//Count total number of rows
$rowCount = $query->num_rows;
//Display states list
if($rowCount > 0){
$optionHTML = '<option value="">Select modelin</option>';
while($row = $query->fetch_assoc()){
$optionHTML .= '<option value="'.$row['modeli_id'].'">'.$row['modeli_name'].'</option>';
}
echo $optionHTML;
}else{
echo '<option value="">Modeli doesnt exist</option>';
}
}
?>
index.php
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#country').on('change',function(){
var countryID = $(this).val();
if(countryID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'marka_id='+countryID,
success:function(html){
$('#state').html(html);
$('#city').html('<option value="">Select state first</option>');
}
});
}else{
$('#state').html('<option value="">Select country first</option>');
}
});
});
</script>
<?php
//Include database configuration file
include('dbConfig.php');
//Get all country data
$query = $db->query("SELECT * FROM marka WHERE `status` = 1 ORDER BY marka_name ASC");
//Count total number of rows
$rowCount = $query->num_rows;
?>
<select name="country" id="country">
<option value="">SelectMarken </option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['marka_id'].'">'.$row['marka_name'].'</option>';
}
}else{
echo '<option value="">Ss</option>';
}
?>
</select>
<select name="state" id="state">
<option value="">Select state</option>
</select>
The mistake was at the #state function, I had another part of code which was waiting data for cities once the state was selected.
So I had to make 'else' turn nothing for it to work.
Thanks guys.
I want to add part search into a website powered by WordPress. I have currently achieved the function, but I have trouble of integrating it into WordPress. I tried several ways but the dynamic dependent select box still not working.
I followed this tutorial: Dynamic Dependent Select Box using jQuery, Ajax and PHP
Below are my code which works well outside WordPress.
index.php
<head>
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="js/ajax-ps.js"></script>
</head>
<body>
<form class="select-boxes" action="ps-result.php" method="POST">
<?php
include('dbConfig.php');
$query = $db->query("SELECT * FROM ps_manufact WHERE status = 1 ORDER BY manufact_name ASC");
$rowCount = $query->num_rows;
?>
<select name="manufacturer" id="manufact" class="col-md-2 col-sm-2 col-xs-10" onchange="manufactText(this)">
<option value="">Select Manufacturer</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['manufact_id'].'">'.$row['manufact_name'].'</option>';
}
}else{
echo '<option value="">Manufacturer Not Available</option>';
}
?>
</select>
<input id="manufacturer_text" type="hidden" name="manufacturer_text" value=""/>
<script type="text/javascript">
function manufactText(ddl) {
document.getElementById('manufacturer_text').value = ddl.options[ddl.selectedIndex].text;
}
</script>
<select name="type" id="type" class="col-md-2 col-sm-2 col-xs-10" onchange="typeText(this)">
<option value="">Select Manufacturer First</option>
</select>
<input id="type_text" type="hidden" name="type_text" value=""/>
<script type="text/javascript">
function typeText(ddl) {
document.getElementById('type_text').value = ddl.options[ddl.selectedIndex].text;
}
</script>
<select name="year" id="year" class="col-md-2 col-sm-2 col-xs-10" onchange="yearText(this)">
<option value="">Select Type First</option>
</select>
<input id="year_text" type="hidden" name="year_text" value=""/>
<script type="text/javascript">
function yearText(ddl) {
document.getElementById('year_text').value = ddl.options[ddl.selectedIndex].text;
}
</script>
<select name="model" id="model" class="col-md-2 col-sm-2 col-xs-10" onchange="modelText(this)">
<option value="">Select Year First</option>
</select>
<input id="model_text" type="hidden" name="model_text" value=""/>
<script type="text/javascript">
function modelText(ddl) {
document.getElementById('model_text').value = ddl.options[ddl.selectedIndex].text;
}
</script>
<input type="submit" name="search" id="search" class="col-md-2 col-sm-2 col-xs-10" value="Search">
</form>
</body>
ajax-ps.js
$(document).ready(function(){
$('#manufact').on('change',function(){
var manufactID = $(this).val();
if(manufactID){
$.ajax({
cache: false,
type:'POST',
url:'ajax-data.php',
data:'manufact_id='+manufactID,
success:function(type_data){
$('#type').html(type_data);
$('#year').html('<option value="">Select Type First</option>');
}
});
}else{
$('#type').html('<option value="">Select Manufact First</option>');
$('#year').html('<option value="">Select Type First</option>');
}
});
$('#type').on('change',function(){
var typeID = $(this).val();
if(typeID){
$.ajax({
cache: false,
type:'POST',
url:'ajax-data.php',
data:'type_id='+typeID,
success:function(year_data){
$('#year').html(year_data);
$('#model').html('<option value="">Select Year First</option>');
}
});
}else{
$('#year').html('<option value="">Select Type First</option>');
$('#model').html('<option value="">Select Year First</option>');
}
});
$('#year').on('change',function(){
var yearID = $(this).val();
if(yearID){
$.ajax({
cache: false,
type:'POST',
url:'ajax-data.php',
data:'year_id='+yearID,
success:function(model_data){
$('#model').html(model_data);
}
});
}else{
$('#model').html('<option value="">Select Year First</option>');
}
});
});
ajax-data.php
include('dbConfig.php');
if(isset($_POST["manufact_id"]) && !empty($_POST["manufact_id"])){
$query = $db->query("SELECT * FROM ps_type WHERE manufact_id = ".$_POST['manufact_id']." AND status = 1 ORDER BY type_name ASC");
$rowCount = $query->num_rows;
if($rowCount > 0){
echo '<option value="">Select Type</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['type_id'].'">'.$row['type_name'].'</option>';
}
}else{
echo '<option value="">Type Not Available</option>';
}
}
if(isset($_POST["type_id"]) && !empty($_POST["type_id"])){
$query = $db->query("SELECT * FROM ps_year WHERE type_id = ".$_POST['type_id']." AND status = 1 ORDER BY year_name ASC");
$rowCount = $query->num_rows;
if($rowCount > 0){
echo '<option value="">Select Year</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['year_id'].'">'.$row['year_name'].'</option>';
}
}else{
echo '<option value="">Year Not Available</option>';
}
}
if(isset($_POST["year_id"]) && !empty($_POST["year_id"])){
$query = $db->query("SELECT * FROM ps_model WHERE year_id = ".$_POST['year_id']." AND status = 1 ORDER BY model_name ASC");
$rowCount = $query->num_rows;
if($rowCount > 0){
echo '<option value="">Select Model</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['model_id'].'">'.$row['model_name'].'</option>';
}
}else{
echo '<option value="">Model Not Available</option>';
}
}
Now the problem is, when select the first box, the second one becomes empty, nothing is returned from the database:
Capture - After select the first box
Really appreicate Christos Lytras to help me solve the previous problem.
I have a new problem with action="ps-result.php" in the line <form class="select-boxes" action="ps-result.php" method="POST">.
ps-result.php
<?php
if (isset($_POST['search'])) {
$clauses = array();
if (isset($_POST['manufacturer_text']) && !empty($_POST['manufacturer_text'])) {
$clauses[] = "`manufacturer` = '{$_POST['manufacturer_text']}'";
}
if (isset($_POST['type_text']) && !empty($_POST['type_text'])) {
$clauses[] = "`type` = '{$_POST['type_text']}'";
}
if (isset($_POST['year_text']) && !empty($_POST['year_text'])) {
$clauses[] = "`year` = '{$_POST['year_text']}'";
}
if (isset($_POST['model_text']) && !empty($_POST['model_text'])) {
$clauses[] = "`model` = '{$_POST['model_text']}'";
}
$where = !empty( $clauses ) ? ' where '.implode(' and ',$clauses ) : '';
$sql = "SELECT * FROM `wp_products` ". $where;
$result = filterTable($sql);
}
else {
$sql = "SELECT * FROM `wp_products` WHERE `manufacturer`=''";
$result = filterTable($sql);
}
function filterTable($sql) {
$con = mysqli_connect("localhost", "root", "root", "i2235990_wp2");
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$filter_Result = mysqli_query($con, $sql);
return $filter_Result;
}
?>
<?php get_header(); ?>
<div class="container">
...
</div>
<?php get_footer(); ?>
Now when I click Search, it returns
Fatal error: Call to undefined function get_header() in /Applications/MAMP/htdocs/wordpress/wp-content/themes/myTheme/inc/ps-result.php on line 42.
The proper way to do this, is to create a wordpress shortcode and then use that shortcode wherever you want, page or post, but if you want to create something more specific, then you should create a small wordpress plugin. I won't get into this, but it’s really not a big deal to create a simple wordpress plugin having such functionality. I’ll go through the steps on how you can have this working in wordpress with the files and the code you already have.
I assume you already have the tables of your example created. My tables are like this:
wp_citytours_dynsel_cities
wp_citytours_dynsel_states
wp_citytours_dynsel_countries
I have imported some data and I have all those tables filled with proper data. I can provide some sql files if you like, but I assume you already have your tables filled with the proper data for each table.
My test theme root directory is:
/wp-content/themes/citytours/
I have created the directory under my theme root directory and I have included all the code files there, so we have 3 files:
/wp-content/themes/citytours/dynsel/index-partial.php
<?php
//Include database configuration file
include('dbConfig.php');
//Get all country data
$query = $db->query("SELECT * FROM wp_citytours_dynsel_countries WHERE status = 1 ORDER BY country_name ASC");
//Count total number of rows
$rowCount = $query->num_rows;
?>
<select name="country" id="country">
<option value="">Select Country</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['country_id'].'">'.$row['country_name'].'</option>';
}
}else{
echo '<option value="">Country not available</option>';
}
?>
</select>
<select name="state" id="state">
<option value="">Select country first</option>
</select>
<select name="city" id="city">
<option value="">Select state first</option>
</select>
<script type="text/javascript">
jQuery(function($) {
$('#country').on('change',function(){
var countryID = $(this).val();
if(countryID){
$.ajax({
type:'POST',
url:'<?php echo home_url('wp-content/themes/citytours/dynsel/ajaxData.php') ?>',
data:'country_id='+countryID,
success:function(html){
$('#state').html(html);
$('#city').html('<option value="">Select state first</option>');
}
});
}else{
$('#state').html('<option value="">Select country first</option>');
$('#city').html('<option value="">Select state first</option>');
}
});
$('#state').on('change',function(){
var stateID = $(this).val();
if(stateID){
$.ajax({
type:'POST',
url:'<?php echo home_url('wp-content/themes/citytours/dynsel/ajaxData.php') ?>',
data:'state_id='+stateID,
success:function(html){
$('#city').html(html);
}
});
}else{
$('#city').html('<option value="">Select state first</option>');
}
});
});
</script>
/wp-content/themes/citytours/dynsel/dbConfig.php
<?php
//db details
$dbHost = 'localhost';
$dbUsername = 'xxxx';
$dbPassword = 'xxxx';
$dbName = 'xxxx';
//Connect and select the database
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
?>
/wp-content/themes/citytours/dynsel/ajaxData.php
<?php
//Include database configuration file
include('dbConfig.php');
if(isset($_POST["country_id"]) && !empty($_POST["country_id"])){
//Get all state data
$query = $db->query("SELECT * FROM wp_citytours_dynsel_states WHERE country_id = ".$_POST['country_id']." AND status = 1 ORDER BY state_name ASC");
//Count total number of rows
$rowCount = $query->num_rows;
//Display states list
if($rowCount > 0){
echo '<option value="">Select state</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['state_id'].'">'.$row['state_name'].'</option>';
}
}else{
echo '<option value="">State not available</option>';
}
}
if(isset($_POST["state_id"]) && !empty($_POST["state_id"])){
//Get all city data
$query = $db->query("SELECT * FROM wp_citytours_dynsel_cities WHERE state_id = ".$_POST['state_id']." AND status = 1 ORDER BY city_name ASC");
//Count total number of rows
$rowCount = $query->num_rows;
//Display cities list
if($rowCount > 0){
echo '<option value="">Select city</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['city_id'].'">'.$row['city_name'].'</option>';
}
}else{
echo '<option value="">City not available</option>';
}
}
?>
As you can see, the index-partial.php now contains only needed code, without <body>, <head> and script files included. Wordpress already includes jQuery to the application for most themes, but you should always check that.
Now, you can add the functionality wherever you want, even at the theme index.php file, but always with caution. I have used theme's single post template file, which is single-post.php. I have included the example code, under the main post body inside a div. I just include the index-partial.php like this:
<div class="<?php echo esc_attr( $content_class ); ?>">
<div class="box_style_1">
...
</div><!-- end box_style_1 -->
<div class="box_style_1">
<?php include(__DIR__.'/dynsel/index-partial.php'); ?>
</div>
</div><!-- End col-md-9-->
I also have used the wordpress home_url function, to have a proper url rooted for the ajaxData.php file like this:
url:'<?php echo home_url('wp-content/themes/citytours/dynsel/ajaxData.php') ?>'
Now, if you have followed all these steps, then you should have your code example working at under each post. You can now included it wherever you want by using that line of code <?php include(__DIR__.'/dynsel/index-partial.php'); ?>.
Please let me know if that worked for you.
I have 2 select options, Make and Model.. I have data in my DB with a bunch of different vehicles and would like the model field to update based on the make selection..
** UPDATED QUESTION WITH EXTERNAL PHP FILE FOR AJAX CALL **
So I made this change, I have dataAJax.php -
if(isset($_POST["make"]) && !empty($_POST["make"])){
$query = $db->query("SELECT DISTINCT Make, Model FROM inv WHERE Make = ".$_POST['Make']."");
$rowCount = $query->num_rows;
if($rowCount > 0){
echo '<option value="">Select model</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['Model'].'">'.$row['Model'].'</option>';
}
}else{
echo '<option value="">Model not available</option>';
}
}
Index.php -
$query = $conn->query("SELECT DISTINCT Make FROM inv");
$rowCount = $query->num_rows;
?>
<select name="make" id="make">
<option value="">Select Make</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['Make'].'">'.$row['Make'].'</option>';
}
}
?>
</select>
<select name="model" id="model">
<option value="">Select Make first</option>
</select>
And here is my ajax call -
$(document).ready(function(){
$('#make').on('change',function(){
var make = $(this).val();
if(make){
$.ajax({
type:'POST',
url:'dataAjax.php',
data:'make='+make,
success:function(html){
$('#model').html(html);
}
});
}else{
$('#make').html('<option value="">Select Model First</option>');
}
});
});
The problem I have is it doesn't populate the model field it just gets the else statement " Model Not Available"
I figured out my problem it was with my sql query in dataAjax.php -
$make = $_POST['make'];
$query = $conn->query("SELECT * FROM inv WHERE Make = '$make'");
$rowCount = $query->num_rows;
if($rowCount > 0){
echo '<option value="">Select model</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['Model'].'">'.$row['Model'].'</option>';
}
}else{
echo '<option value="">Model not available</option>';
}