Welcome Guest, Not a member yet? Register   Sign In
Need Help on Ajax
#1

[eluser]Nutan Lade[/eluser]
I am trying to something like Google Suggest.
And the code I wrote in my application is below

gadgets_view.php
------------------------------------------


function changeLang(){
//alert("hi");
var url = "<?=base_url()?>index.php/gadgets/lang/"+document.getElementById("lang").value;
alert(url);
[removed]="<?=base_url()?>index.php/gadgets/lang/"+document.getElementById("lang").value;
}


<!--
var req;
function getServerName()
{
var str = [removed].protocol + '//' + [removed].hostname;
return str;
}
function loadXMLDoc(url) {

// Internet Explorer
try { req = new ActiveXObject("Msxml2.XMLHTTP"); }
catch(e) {
try { req = new ActiveXObject("Microsoft.XMLHTTP"); }
catch(oc) { req = null; }
}
// Mozailla/Safari
if (!req && typeof XMLHttpRequest != "undefined") { req = new XMLHttpRequest(); }


// Call the processChange() function when the page has loaded
alert(url);if(req !=null){
req.open("GET", url, true);
req.onreadystatechange = processChange;
req.send(null);
}
}

function processChange() {
// The page has loaded and the HTTP status code is 200 OK
if (req.readyState == 4 && req.status == 200) {

// Write the contents of this URL to the searchResult layer
getObject("searchResult")[removed] = req.responseText;

}
}

function getObject(name) {
var ns4 = (document.layers) ? true : false;
var w3c = (document.getElementById) ? true : false;
var ie4 = (document.all) ? true : false;

if (ns4) return eval('document.' + name);
if (w3c) return document.getElementById(name);
if (ie4) return eval('document.all.' + name);
return false;
}


window.onload = function() {
getObject("movieSearch").focus();
}

// -->
[removed]


<body>
<table>
<tr>
<td>&lt;input type="text" id="movieSearch" name="movieSearch" &gt;&lt;/td>
</tr>
<tr>
<td><div align="center" id="searchResult" name="searchResult" style="font-family:Arial; font-size:12px; width:150px; border:#000000 solid 1px; "></div></td>
</tr>
</table>

&lt;/body&gt;


gadgets.php
----------------------------------------------------------------------------------
function searchMovie(){

$results = "";
$pat = $this->uri->segment(3);

$query = $this->db->query("SELECT * FROM movies_list WHERE movie_name LIKE '$pat%' LIMIT 10");

foreach ($query->result() as $row)
{
$results = $results."<b>$row->movie_name</b><br />";
}
return $results;

}


But the results is not written back.But the localhost page is written as the result.Please help me how to solve this one.Thanq in advance.
#2

[eluser]Hannes Nevalainen[/eluser]
Dude, grab a javascript framework for that stuff =) I'd go for MooTools but JQuery is also a great alternative =)
#3

[eluser]Nutan Lade[/eluser]
I didn't get.I will be happy if u tell me some more clearly about framework or Mootoools.
#4

[eluser]Pascal Kriete[/eluser]
Please try to use code tags when posting code. It's really hard to read this way.

Now, where in the world did you get that getObject code? I dropped IE6 quite a while ago, and here you are supporting IE4?! Ajax support came with IE5. document.getElementById is supported by every modern browser, use it.

A framework makes this job a lot easier by providing true and tested functions. jQuery is quite easy to learn if you're getting started. There documentation on their site is quite good as well.

If you don't want to use a framework (there are reasons not to), I suggest you roll the code into an easy to use interface. Something like this.
#5

[eluser]Hannes Nevalainen[/eluser]
Oki, sorry.
MooTools is a library of functions. You just have to inlude it in the header of your page. (It is like CI for javascript). The framework takes care of browser compability and other stuff so you can focus on developing new functions. Almost every Javascript framework has a easy way to deal with Ajax and Effects and other web 2.0 stuff. I really think you shuold try it out. =)
#6

[eluser]Nutan Lade[/eluser]
Sorry I forgoted to write the code for getObject.It is nothing but it checks the browser type and returns the msg.

function getObject(name) {
var ns4 = (document.layers) ? true : false;
var w3c = (document.getElementById) ? true : false;
var ie4 = (document.all) ? true : false;

if (ns4) return eval('document.' + name);
if (w3c) return document.getElementById(name);
if (ie4) return eval('document.all.' + name);
return false;
}



I just changed the code and used framework.

function ajaxSearchMovie(){
new Ajax.Request('/gadgets/searchMovie/a',
{
method:'get',
onSuccess: function(transport){
var response = transport.responseText || "no response text";
alert("Success! \n\n" + response);
},
onFailure: function(){ alert('Something went wrong...') }
});
}

&lt;body&gt;
<table>
<tr>
<td>&lt;input type="text" id="movieSearch" name="movieSearch" &gt;&lt;/td>
</tr>
<tr>
<td><div align="center" id="searchResult" name="searchResult" style="font-family:Arial; font-size:12px; width:150px; border:#000000 solid 1px; “></div></td>
</tr>
</table>

&lt;/body&gt;




gadgets.php
----------------------------------------------------------------------------------
class Gadgets extends Controller {


function Gadgets()
{
parent::Controller();
$this->load->helper('ajax');
}

function searchMovie(){

$results = “”;
$pat = $this->uri->segment(3);

$query = $this->db->query("SELECT * FROM movies_list WHERE movie_name LIKE ‘$pat%’ LIMIT 10");

foreach ($query->result() as $row)
{
$results = $results."<b>$row->movie_name</b><br />”;
}
return $results;

}

}

the above code is the code used in controller gadgets.php

these is the javascript code I used in gadgets_view.php file.

But I am getting the message "Something went wrong....."

Is that the controller is not writing any data back.If so where could be the problem.Please help me.Thanq in advance.
#7

[eluser]Pascal Kriete[/eluser]
You did post the getObject code. And I know what it does. What I'm saying is it's incredibly outdated, those browser checks haven't been needed since 1998. getElementById works just fine. Also, mootools provides has a selector function, $('idname') .

The controller has to output the result, you're returning it. Replace return with echo .

And please, please wrap your code in [ code ][ /code ] tags (no spaces).
#8

[eluser]Nutan Lade[/eluser]
Sorry I don't know about code tags.But next time i vl use them.
Already I changed the code and try using echo instead of return.But the same problem message
something went wrong.. is displaying.
#9

[eluser]Pascal Kriete[/eluser]
What happens if you visit the remote url manually?
#10

[eluser]Nutan Lade[/eluser]
Sorry dude I didn't get u.At present I am using localhost only.I am accessing data from localhost only.If the Ajax framework doesn't work here then I need to think about another logic to implement the same search operation.But I hope using Ajax will give a nice look to the application.




Theme © iAndrew 2016 - Forum software by © MyBB