Welcome Guest, Not a member yet? Register   Sign In
how to catch data in CI with basic Ajax
#1

[eluser]ludo31[/eluser]
Hello ;
I would like to test Ajax in CI , it's my first code with Ajax so I create a simple code but my problem is how to catch the data : sometimes we use input-> post : here the case is I search via select in my view :

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Ajax essai&lt;/title&gt;
     [removed]
function showUser(str)
{
var xmlhttp;
if (str=="")
  {
  document.getElementById("txtHint")[removed]="";
  return;
  }  
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint")[removed] = xmlhttp.responseText;
    }
}
xmlhttp.open("GET", "ajaxcontrolleur.php?q="+str, true);
xmlhttp.send();
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;form&gt;
&lt;select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
&lt;/form&gt;
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>

&lt;/body&gt;
&lt;/html&gt;

here : the name of my controlleur where I send the parameter is ajaxcontrolleur

Code:
xmlhttp.open("GET", "ajaxcontrolleur.php?q="+str, true);

and in my controller I try to catch the parameter but it doesn't work :

Code:
class Ajaxcontrolleur extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
    }


    public function index()
    {
         /* we try to catch  */
        $q = $this->input->get('q');
        
        /* we find in database  */
        $data['infos'] = $this->modelajax->cherche($q);
        
        /* and we send in another view file  */
        $this->load->view('ajax_view',$data);
        
    }


    
}

so I would like ask you how to catch the value of q ??
another question did we proceed like this in Ajax with CI ?

thanks
#2

[eluser]toopay[/eluser]
[quote author="ludo31" date="1332668356"]
Code:
xmlhttp.open("GET", "ajaxcontrolleur.php?q="+str, true);

and in my controller I try to catch the parameter but it doesn't work :
[/quote]
You're pointed to the wrong url. Should be:
Code:
xmlhttp.open("GET", "ajaxcontrolleur?q="+str, true);
#3

[eluser]ludo31[/eluser]
I try to make like that but nothing run !!

Code:
xmlhttp.open("GET", "ajaxcontrolleur?q="+str, true);

and sometimes change the way to catch

Code:
//  $q = $_GET["q"];
        
       $this->input->get('q');

I don't know if we must create another view file or we need to redirect in first view file




Theme © iAndrew 2016 - Forum software by © MyBB