Search pe site cu Ajax

Discutii si intrebari legate de scripturi si functii JavaScript, jQuery si Ajax, cod JavaScript in general.
lauma
Mesaje:16

Search pe site cu Ajax

Salut! Vreau sa ofer posibilitatea unui search pe site. Imi puteti spune unde am gresit??Ms anticipat.
Acesta este codul meu:

fisierul index.html

Cod: Selectaţi tot

<html>
<head>
<script type="text/javascript" src="search.js"></script>
</head>
<body >

                                                <form name="searchNameForm">                                           
                                                <input type="text" maxlength="60" name="wordToSearchFor" id="edit-name" style="width: 35%" onKeyUp="searchProduct(this.value)"   />
						</form>
						<div id="res" > </div>
</body>
</html>
fisierul search.js

Cod: Selectaţi tot

function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
		{
				objXMLHttp=new XMLHttpRequest()
		}
		else if (window.ActiveXObject)
		{
				objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
		}
		    	return objXMLHttp;
}


var req;	
function searchProduct(word)
{
	req=GetXmlHttpObject();
	if (req == "")
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url="searchWord.php";
	url=url+"?wordToSearchFor"+word;
	req.onreadystatechange=function()
	{
		if (req.readyState == 4)
		if (req.status == 200)
			if (req.responseText == "")
			{	
				
				document.getElementById("res").innerHTML="<span style="color:red">The recipe doesn't exist!</span>";
			}
			else
			{
				
				document.getElementById("res").innerHTML=req.responseText;
			}			
	}
	req.open("GET",url,true);
	req.send(null);
}
fisierul searchWord.php

Cod: Selectaţi tot

 include "dbconnect.php";
 if (isset($_GET['word']))
    $word=trim($_GET['word']);
    $data="";
    if (!empty($word))
    {
        $word="%".$word."%";
        $result=mysql_query("select name from products where name like '$word'");
        $nr=mysql_num_rows($result);
        if ($nr)
        {
            $data.="<ul>";
            while ($row=mysql_fetch_row($result))
            {
                $data.="<li><i><b>".$row[0]."</b></i>:".$row[1]."</li>";
            }
            $data.="</ul>";
        }
    }
    echo $data;
 

MarPlo Mesaje:4343
Salut
Multe greseli in codul functiei searchProduct().
Incearca fisierul search.js cu acest cod:

Cod: Selectaţi tot

function get_XmlHttp() {
  // Creaza variabila care va contine instanta la XMLHttpRequest, initial cu valoare nula
  var xmlHttp = null;

  if(window.XMLHttpRequest) {		// Daca browser-ul e Forefox, Opera, Safari, ...
    xmlHttp = new XMLHttpRequest();
  }
  else if(window.ActiveXObject) {	// Daca browser-ul este Internet Explorer
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }

  return xmlHttp;
}

// Functia care trimite datele la un fisier PHP si returneaza raspunsul
function searchProduct(word) {
  var cerere_http =  get_XmlHttp();		// Apeleaza functia pt. crearea instantei la obiectul XMLHttpRequest

  // Creaza adresa URL cu perechea de date indice=valoare
  var  url = 'searchWord.php?word='+word;

  cerere_http.open("GET", url, true);			// Creaza cererea
  cerere_http.send(null);		// Efectueaza trimiterea datelor

  // Verifica starea cererii
  // Daca raspunsul e primit complet, il transfera in eticheta HTML cu id-ul 'res'
  cerere_http.onreadystatechange = function() {
    if (cerere_http.readyState == 4) {
      document.getElementById('res').innerHTML = cerere_http.responseText;
    }
    else document.getElementById("res").innerHTML='<span style="color:red">The recipe doesn`t exist!</span>';
  }
}

Subiecte similare