Forum for discussing general topics related to Couch.
6 posts Page 1 of 1
Hello, found on a website this script and i wondering if there is a way to integrate in Couch CMS.. or to create something like that.

Exemple:
Dropdown selection: USA-> New York-> Albany

The page redirects you to:
https://www.yoursitename.com/usa/new-york/albany


index.php
Code: Select all
<?php
//index.php
$connect = mysqli_connect("localhost", "root", "", "testing");
$country = '';
$query = "SELECT country FROM country_state_city GROUP BY country ORDER BY country ASC";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{
$country .= '<option value="'.$row["country"].'">'.$row["country"].'</option>';
}
?>
<!DOCTYPE html>
<html>
<head>
  <title> Dynamic Dependent Select Box using JQuery Ajax with PHP</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
  <br /><br />
  <div class="container" style="width:600px;">
   <h2 align="center">Dynamic Dependent Select Box using JQuery Ajax with PHP</h2><br /><br />
   <select name="country" id="country" class="form-control action">
    <option value="">Select Country</option>
    <?php echo $country; ?>
   </select>
   <br />
   <select name="state" id="state" class="form-control action">
    <option value="">Select State</option>
   </select>
   <br />
   <select name="city" id="city" class="form-control">
    <option value="">Select City</option>
   </select>
  </div>
</body>
</html>

<script>
$(document).ready(function(){
$('.action').change(function(){
  if($(this).val() != '')
  {
   var action = $(this).attr("id");
   var query = $(this).val();
   var result = '';
   if(action == "country")
   {
    result = 'state';
   }
   else
   {
    result = 'city';
   }
   $.ajax({
    url:"fetch.php",
    method:"POST",
    data:{action:action, query:query},
    success:function(data){
     $('#'+result).html(data);
    }
   })
  }
});
});
$(document).ready(function(){
  $('#city').on('change',function()
  {
      var cityId = $(this).val();

      window.location.href = $('#country').val() + "/" + $('#state').val() + "/" + $(this).val();
  });
});
</script>


fetch.php

Code: Select all
<?php
//fetch.php
if(isset($_POST["action"]))
{
$connect = mysqli_connect("localhost", "root", "", "testing");
$output = '';
if($_POST["action"] == "country")
{
  $query = "SELECT state FROM country_state_city WHERE country = '".$_POST["query"]."' GROUP BY state";
  $result = mysqli_query($connect, $query);
  $output .= '<option value="">Select State</option>';
  while($row = mysqli_fetch_array($result))
  {
   $output .= '<option value="'.$row["state"].'">'.$row["state"].'</option>';
  }
}
if($_POST["action"] == "state")
{
  $query = "SELECT city FROM country_state_city WHERE state = '".$_POST["query"]."'";
  $result = mysqli_query($connect, $query);
  $output .= '<option value="">Select City</option>';
  while($row = mysqli_fetch_array($result))
  {
   $output .= '<option value="'.$row["city"].'">'.$row["city"].'</option>';
  }
}
echo $output;
}
?>


Database

Code: Select all
--
-- Database: `testing`
--

-- --------------------------------------------------------

--
-- Table structure for table `country_state_city`
--

CREATE TABLE IF NOT EXISTS `country_state_city` (
  `id` int(11) NOT NULL,
  `country` varchar(250) NOT NULL,
  `state` varchar(250) NOT NULL,
  `city` varchar(250) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `country_state_city`
--

INSERT INTO `country_state_city` (`id`, `country`, `state`, `city`) VALUES
(1, 'USA', 'New York', 'New York city'),
(2, 'USA', 'New York', 'Buffalo'),
(3, 'USA', 'New York', 'Albany'),
(4, 'USA', 'Alabama', 'Birmingham'),
(5, 'USA', 'Alabama', 'Montgomery'),
(6, 'USA', 'Alabama', 'Huntsville'),
(7, 'USA', 'California', 'Los Angeles'),
(8, 'USA', 'California', 'San Francisco'),
(9, 'USA', 'California', 'San Diego'),
(10, 'Canada', 'Ontario', 'Toronto'),
(11, 'Canada', 'Ontario', 'Ottawa'),
(12, 'Canada', 'British Columbia', 'Vancouver'),
(13, 'Canada', 'British Columbia', 'Victoria'),
(14, 'Australia', 'New South Wales', 'Sydney'),
(15, 'Australia', 'New South Wales', 'Newcastle'),
(16, 'Australia', 'Queensland', 'City of Brisbane'),
(17, 'Australia', 'Queensland', 'Gold Coast\r\n');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `country_state_city`
--
ALTER TABLE `country_state_city`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `country_state_city`
--
ALTER TABLE `country_state_city`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=18;
Hello,
The presented raw PHP is asking for a malicious SQL injection.. It must be replaced ASAP.
Couch has all the necessary features to make this perfectly manageable and secured.
Don't worry is not online,is a small project on a Synology NAS. Thank you for advice.
Xqzt wrote: Don't worry is not online,is a small project on a Synology NAS. Thank you for advice.

Have you seen Folders https://docs.couchcms.com/concepts/using-folders.html?
Yes, but i can't figure out... sorry i just discovered Couch a few days ago.
I give up on dropdown selector and i want to make a page for every folder something like this:

Page 1 > Countries - with all my city list (ex. Belgium)
Page 2 > Regions - list of belgium regions (ex. West Flanderi)
Page 3 > Cities - list of regions cities (ex. Brugge)
Page 4 > Brugge page (this one i made it )

So fare i have this :)) and i`m stuck

Code: Select all
            <cms:pages masterpage='countries.php' >            
                <cms:folders masterpage='countries.php' hierarchical='1' depth='1'>
                     <cms:show k_folder_image />
                     <a href="<cms:show k_folder_link />"><cms:show k_folder_title /></a>
                </cms:folders>
            </cms:pages >
If you are stuck and don't get results with complex code (loops inside loops), try to simplify things as much as possible and see what is happening step by step.

I really advise taking the tutorial https://docs.couchcms.com/tutorials/por ... /blog.html

Once again, when in any doubt about which variables are available for use, place <cms:dump /> or <cms:dump_all /> temporarily within the template. On accessing a page, these tags will spill out all the variables (along with their values) that are available.


Note that cms:dump is extremely useful inside tag-pairs!
6 posts Page 1 of 1
cron