[eluser]newbie110[/eluser]
I am trying to select a country and return the regions related to it in CodeIgniter. everything is working but the new div is not refreshing instead it shows the returned values in the console instead of the browser page. Here is my attached controller function code.
Code:
public function post_action(){
if($_POST['country']){
$pid = array('pid'=>$_POST['country']);
$regions =$this->countries_model->getRegions($pid);
$html = '<select name="region" id="regions"> ';
foreach($regions as $rg){
$html .= " <option value=$rg[id]>$rg[name]</option>";
}
$html .="</select>";
// $output = '{ "message": "'.$html.'" }';
echo $html;
return;
// redirect('json/view_sites');
}
}
Here i sthe view that is calling this function
Code:
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8">
[removed][removed]
<!--[removed][removed]-->
[removed]
$(document).ready(function(){
$("select").change(function () {
var country=$("#country").val();
//alert (country);
$.ajax({
type: "POST",
url: "countries_json/post_action",
dataType: "json",
data: "country="+country,
cache:false,
success:
function(data){
$("#form_tag").html(data);
}
});
return false;
});
});
[removed]
<title>Select Countries</title>
<style type="text/css">
body {
background-color: #fff;
margin: 40px;
font-family: Lucida Grande, Verdana, Sans-serif;
font-size: 14px;
color: #4F5155;
}
a {
color: #003399;
background-color: transparent;
font-weight: normal;
}
h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 16px;
font-weight: bold;
margin: 24px 0 2px 0;
padding: 5px 0 6px 0;
}
code {
font-family: Monaco, Verdana, Sans-serif;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
#form_message{
display:none;
text-align:center;
margin-bottom:5px;
padding:10px;
}
</style>
</head>
<body>
<h1>Please select country </h1>
<div width:500px;" >
<div id="form_message"></div>
<?php
if (isset($links) && is_array($links)){
?>
<form name="ajax_form" id ="ajax_form" method="post">
<select name="countries" id="country">
<?php
foreach ($links as $lnks){
?>
<option value="<?php echo $lnks['id']?>"><?php echo $lnks['name']?></option>
<?php
}?>
</select>
</form>
</div>
<?php
}
?>
<div id="form_tag">
</div>
</body>
</html>
and here is the model returning regions
Code:
public function getRegions($pid){
$this->db->where($pid);
$reg = $this->db->get('regions');
echo"<br>";
return ($reg->result_array());
}
please sombedy help urgently.