Welcome Guest, Not a member yet? Register   Sign In
Refreshing page in codeigniter
#1

[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>
&lt;html lang="en"&gt;&lt;head>
&lt;meta charset="utf-8"&gt;
[removed][removed]
&lt;!--[removed][removed]--&gt;
[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]
&lt;title&gt;Select Countries&lt;/title&gt;

&lt;style type="text/css"&gt;

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;
}

&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

<h1>Please select country </h1>



    
    <div  width:500px;" >
    <div id="form_message"></div>
    &lt;?php

if (isset($links) && is_array($links)){
?&gt;
      &lt;form name="ajax_form" id ="ajax_form" method="post"&gt;
      <select name="countries" id="country">
     &lt;?php
foreach ($links as $lnks){
  ?&gt;
        <option value="&lt;?php echo $lnks['id']?&gt;">&lt;?php echo $lnks['name']?&gt;</option>
&lt;?php
  
  }?&gt;
        </select>
      &lt;/form&gt;
    </div>
&lt;?php  
  }
?&gt;
<div id="form_tag">
    
</div>
&lt;/body&gt;
&lt;/html&gt;
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.
#2

[eluser]InsiteFX[/eluser]
Try this.

Replace $ in scrpit tags with s
Code:
<$cript type="text/javascript">
$(document).ready(function(){

$("select").change(function () {
    
        var country=$("#country").val();
    
      //alert (country);
        $.ajax({
        type: "POST",
        url: "&lt;?php echo base_url('countries_json/post_action');?&gt;",
      // using the below code
        url: base_url+"countries_json/post_action",

        dataType: "json",
        data: "country="+country,
        cache:false,
        success:
          function(data){
            $("#form_tag").html(data);
  
          }
        
        });

      return false;

    });
  
});
</$cript>

// You can also add this to your html document header section.

&lt;head&gt;
<$cript type="text/javascript" charset="utf-8">
  //&lt;![CDATA[
   var base_url = "&lt;?php echo base_url(); ?&gt;";
   var site_url = "&lt;?php echo site_url(); ?&gt;";
  // ]]>
</$cript>
&lt;/head&gt;

If you have removed index.php using .htaccess then you need to include index.php in your url links.




Theme © iAndrew 2016 - Forum software by © MyBB