Skip to Content Skip to Menu

cascaded drop down select with jquery ajax,php and mysql

9 years 8 months ago #259982 by globalgridfree
thanks off to figure out indexing :)

Please Log in or Create an account to join the conversation.

9 years 8 months ago - 9 years 8 months ago #260007 by globalgridfree
I noticed that setting up the condition on self, does work without a page reload.

I am wondering how to reduce the amount of selections available.

since the condition of self using if empty hide,works instant without reload, as soon as its selected the next row appears.

is there a query available to select the province from the country code of the country previously selected field? the database has the country codes and province codes,


i did create index for all 3 as follows
CREATE INDEX countries_full_name_idx
ON countries (name)

if a basic query can not handle this, could a combination of query and indexes work for something like this?

am i going the wrong direction on this? :)

here are a couple files i am referring too

index.php


<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include("connection.php");
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" />

</head>
<body>
<div id="container">
<div id="body">
<div id="dropdowns">
<div id="center" class="cascade">
<?php
$sql = "SELECT DISTINCT state FROM tbl_zip ORDER BY state ASC";
$query = mysqli_query($con, $sql);
?>
<label>State:
<select name="state" id = "state">
<option value="">Please Select</option>
<?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC )) { ?>
<option value="<?php echo $rs["state"]; ?>"><?php echo $rs["state"]; ?></option>
<?php } ?>
</select>
</label>
</div>


<div id="city" class="cascade"></div>
<div id="zip" class="cascade"></div>
</div>
</div>
</div>
<script src=" ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js ">
<script>
$(document).ready(function(){
$("select#state").change(function(){

var state = $("select#state option:selected").attr('value');
alert(state);
$("#city").html( "" );
$("#zip").html( "" );
if (state.length > 0 ) {
alert(state.length);
$.ajax({
type: "POST",
url: "fetch_state.php",
data: "state="+state,
cache: false,
beforeSend: function () {
$('#city').html('<img src="loader.gif" alt="" width="24" height="24">');
},
success: function(html) {
$("#city").html( html );
}
});
}
});
});
</script>
<script src=" ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js ">
<script>
$(document).ready(function(){
$("select#drop2").change(function(){

var state_id = $("select#drop2 option:selected").attr('value');
// alert(state_id);
if (state_id.length > 0 ) {
$.ajax({
type: "POST",
url: "fetch_city.php",
data: "city="+city,
cache: false,
beforeSend: function () {
$('#city').html('<img src="loader.gif" alt="" width="24" height="24">');
},
success: function(html) {
$("#city").html( html );
}
});
} else {
$("#city").html( "" );
}
});

});
</script>
</body>
</html>



fetch_state.php


<?php

include("connection.php");
var_dump($_POST);
$state = trim(mysqli_escape_string($con, $_POST["state"]));

$sql = "SELECT DISTINCT city FROM tbl_zip WHERE state = '".$state ."' ORDER BY city";
$count = mysqli_num_rows( mysqli_query($con, $sql) );
if ($count > 0 ) {
$query = mysqli_query($con, $sql);
?>
<label>City:
<select name="city" id="drop2">
<option value="">Please Select</option>
<?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC)) { ?>
<option value="<?php echo $rs["city"]; ?>"><?php echo $rs["city"]; ?></option>
<?php } ?>
</select>
</label>
<?php
}

?>
Last edit: 9 years 8 months ago by globalgridfree. Reason: add files

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48479
  • Thanks: 8282
  • Karma: 1443
9 years 8 months ago #260049 by krileon
Custom Value conditions will only update on page load. They're custom values so they've no idea where to get their value except PHP. Standard conditions are purely jQuery based and look for their input to test against. If that input isn't available in the edit display then the condition is just going to fail. For example if I am checking that Field A is Equal To 123 and Field A is not set to display on profile edit my condition will always fail as the condition can't find Field A for its value; this is a known scenario noted to be fixed already.

is there a query available to select the province from the country code of the country previously selected field?

If your database table is structure for it then sure, but again it's going to take a page reload for this to take affect as it's pulling a field value before profile edit display; so it makes the usage pretty useless unless that fields html reloads, which there is nothing implemented to do this. CB Query Field supports substitutions so you'd substitute in another value for example with '[cb_province]' (be sure to quote it as all substitutions are strings).

if a basic query can not handle this, could a combination of query and indexes work for something like this?

An index is just a way for the database to keep references of common requests. This means you need to optimize based entirely off the query you're using. Joomla 3.x helps with this a good bit as its debug mode can suggest indexes for you.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

9 years 8 months ago #260074 by globalgridfree
the biggest problem is when I get to city, the file is huge. and cant even be turned on .

A plugin will need to be created for this, I am not a programmer i am learning, I noticed there is a api document for the old version of CB for making plugins. is there one available for cb 2.0+ ?

my belief is this should be a core file, or available as a paid plugin considering a large % of online communities are based on location in some way.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48479
  • Thanks: 8282
  • Karma: 1443
9 years 8 months ago #260090 by krileon

the biggest problem is when I get to city, the file is huge. and cant even be turned on .

Correct, as you're requesting too much data at once. Ideally it should send an ajax call to the next fields fieldClass with the current fields value and update accordingly. For example when Country is selected it tells State the value and then State updates accordingly. A new plugin to provide this is planned, but I've no idea when. To workaround your issue for the time being you may want to consider just using a standard text field for city for now.

A plugin will need to be created for this, I am not a programmer i am learning, I noticed there is a api document for the old version of CB for making plugins. is there one available for cb 2.0+ ?

We do not have a CB 2.x API documentation at this time. It's being worked on, but for now your best resource is using existing plugins as examples.

my belief is this should be a core file, or available as a paid plugin considering a large % of online communities are based on location in some way.

It will be a professional subscriber plugin, but right now I need to finish my current tasks before I start piling on new ones so it's not happening anytime soon. Currently I'm rewriting CB Activity, next is CB GroupJice upgrade, and then can review my new projects list to determine the next most important task.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

9 years 8 months ago #260123 by globalgridfree
i sent you a message to your profile about this, Can you get back to me threw pms ?
thanks

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum