Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-12-2011, 01:53 PM   PM User | #1
garevn
New Coder

 
Join Date: Apr 2011
Posts: 95
Thanks: 13
Thanked 1 Time in 1 Post
garevn is an unknown quantity at this point
ajax search js doesn't work

hello i just found this site and having this problem i decited to join your community!!
I am new in programming and i would say i am not really skilled atm.. anyway!! To the point... I wan't an ajax products search, I have my html file called 'body.html. My php file call data.php and below is my js file.

[CODE]
var xmlHttp
function finding(str)
{
xmlHttp=GetXmlHttpObject()
if(xmlHttp==null)
{
alert("Browser does not support HTTP Request")
return
}
var url="data.php"
url=url+"?productname="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("result").innerHTML=xmlHttp.responseText
}
}
function stateChanged()
{
if(xmlHttp.readyState==4|| xmlHttp.readyState=="complete")
{
document.getElementById("result").innerHTML=xmlHttp.responseText
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
}
[CODE]


When i try to right a value into the field i get the error i wrote into the code
"Browser does not support HTTP Request"
The html code contain

[CODE]
<script src="js3.js"></script>
</head>

<body>

<h2>Ajax Search Engine</h2>

<form>
search:
<input type="text" size="30" onkeyup="finding(this.value)"/>
</form>
<p>
<div id="Result"></div>
</p>
</body>
</html>
[CODE]

Sorry about any mistakes as i said i am new in programming.
Tnx hope for some answers!!

Last edited by garevn; 04-12-2011 at 04:14 PM..
garevn is offline   Reply With Quote
Old 04-12-2011, 05:53 PM   PM User | #2
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
try this


Code:
var xmlHttp
function finding(str)
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
 xmlhttp = new XMLHttpRequest()
  }catch (E) {
   alert("Browser does not support HTTP Request")
return
  }
var url="data.php"
url=url+"?productname="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("result").innerHTML=xmlHttp.responseText
}
}
function stateChanged()
{
if(xmlHttp.readyState==4|| xmlHttp.readyState=="complete")
{
document.getElementById("result").innerHTML=xmlHttp.responseText
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
}

Last edited by DanInMa; 04-12-2011 at 05:56 PM..
DanInMa is offline   Reply With Quote
Users who have thanked DanInMa for this post:
garevn (04-16-2011)
Old 04-12-2011, 06:43 PM   PM User | #3
garevn
New Coder

 
Join Date: Apr 2011
Posts: 95
Thanks: 13
Thanked 1 Time in 1 Post
garevn is an unknown quantity at this point
Really tnx man it seems really good code but it pops me an error on line 3 on

[CODE]
try {
[CODE]

And i cant figure out if it works or not


ok i changed some "{" and has no errors but still doesnt work, though it doent pops me error message like before.
Is it bossible my body.html code is wrong?

Last edited by garevn; 04-12-2011 at 06:58 PM..
garevn is offline   Reply With Quote
Old 04-13-2011, 06:40 AM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
If you turn the code into a readable format using indentation you'll see that the code is missing a closing curly bracket } somewhere ... the reason most probably is that there are more "catch" statements than "try" statements which is illegal:
Code:
var xmlHttp
function finding(str)
   try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
      try {
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
         xmlhttp = new XMLHttpRequest()
      } catch (E) {         // illegal
         alert("Browser does not support HTTP Request")
         return
      }
   var url="data.php"
   url=url+"?productname="+str
   url=url+"&sid="+Math.random()
   xmlHttp.onreadystatechange=stateChanged
   xmlHttp.open("GET",url,true)
   xmlHttp.send(null)
}
It should be something like this:
Code:
var xmlHttp;
function finding(str) {
   try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
      try {
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
         xmlhttp = new XMLHttpRequest();
      }
   }
   if(!xmlhttp) {
         alert("Browser does not support HTTP Request");
         return;
   }
   var url="data.php";
   url=url+"?productname="+str;
   url=url+"&sid="+Math.random();
   xmlHttp.onreadystatechange=stateChanged;
   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
}

Last edited by devnull69; 04-13-2011 at 09:57 AM..
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
garevn (04-16-2011)
Old 04-13-2011, 08:33 AM   PM User | #5
garevn
New Coder

 
Join Date: Apr 2011
Posts: 95
Thanks: 13
Thanked 1 Time in 1 Post
garevn is an unknown quantity at this point
tnx devnull for your answer too.
i tried it out but still pops an error on line 3.
I add an "{" at the end of line 2 and then had no errors but i had no results too...
garevn is offline   Reply With Quote
Old 04-13-2011, 09:59 AM   PM User | #6
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
ooh my bad ... I added the missing opening bracket { after the first function in the code above. Now it should be fine. Sometimes we are not testing each code line before we post here ... then you'll have to put some "common sense" into it :-)
devnull69 is offline   Reply With Quote
Old 04-13-2011, 10:21 AM   PM User | #7
garevn
New Coder

 
Join Date: Apr 2011
Posts: 95
Thanks: 13
Thanked 1 Time in 1 Post
garevn is an unknown quantity at this point
its ok i added the missing bracket.
Any ideas why it doesnt display results?
garevn is offline   Reply With Quote
Old 04-13-2011, 10:26 AM   PM User | #8
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Wow ok ... you solve one problem and stumble upon the next

Javascript is case sensitive so xmlhttp and xmlHttp are two different objects. Please change all appearances to the same case.

Second:
Code:
 || xmlHttp.readyState=="complete"
This part does not make any sense. You should replace it with
Code:
 && xmlHttp.status==200
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
garevn (04-16-2011)
Old 04-13-2011, 10:58 AM   PM User | #9
garevn
New Coder

 
Join Date: Apr 2011
Posts: 95
Thanks: 13
Thanked 1 Time in 1 Post
garevn is an unknown quantity at this point
tnx devnull for your time u spend on me. As i said i am new in programming soz about any mistakes!!
still it doesnt work....
i post u all 3 files u may find any mistakes...


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="js3.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

Code:
</head>

<body>
<form>
search: 
<input type="text" size="30" onkeyup="finding(this.value)"/>
</form>
<p><div id="Result"></div>
</p>
</body>
</html>









Code:
var xmlHttp;
function finding(str) {
   try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
      try {
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
         xmlhttp = new XMLHttpRequest();
      }
   }
   if(!xmlhttp) {
         alert("Browser does not support HTTP Request");
         return;
   }
   var url="data.php";
   url=url+"?productname="+str;
   url=url+"&sid="+Math.random();
   xmlHttp.onreadystatechange=stateChanged;
   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
}
function stateChanged()
{
if(xmlHttp.readyState==4 && xmlHttp.status==200)
{
document.getElementById("result").innerHTML=xmlHttp.responseText
}
}
function stateChanged()
{
if(xmlHttp.readyState==4 && xmlHttp.status==200)
{
document.getElementById("result").innerHTML=xmlHttp.responseText
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
}










PHP Code:
<?php require_once('../Connections/Mysitedb.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  if (
PHP_VERSION 6) {
    
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}

$colname_Recordset1 "-1";
if (isset(
$_GET['productname'])) {
  
$colname_Recordset1 $_GET['productname'];
}
mysql_select_db($database_Mysitedb$Mysitedb);
$query_Recordset1 sprintf("SELECT manufacturer, productname, price FROM products WHERE productname = %s"GetSQLValueString($colname_Recordset1"text"));
$Recordset1 mysql_query($query_Recordset1$Mysitedb) or die(mysql_error());
$row_Recordset1 mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php do { ?>
   <form id="form1" name="form1" method="post" action="">
    <table width="353">
    <tr>
      <td width="59">name:</td>
      <td width="282"><?php echo $row_Recordset1['productname']; ?></td>
    </tr>
    <tr>
      <td>brand:</td>
      <td><?php echo $row_Recordset1['manufacturer']; ?></td>
    </tr>
    <tr>
      <td>price</td>
      <td><?php echo $row_Recordset1['price']; ?></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>

  </form>
  <?php } while ($row_Recordset1 mysql_fetch_assoc($Recordset1)); ?>
</body>
</html>
<?php
mysql_free_result
($Recordset1);
?>
garevn is offline   Reply With Quote
Old 04-13-2011, 11:03 AM   PM User | #10
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Did you read my last post?
Quote:
Javascript is case sensitive so xmlhttp and xmlHttp are two different objects. Please change all appearances to the same case.
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
garevn (04-16-2011)
Old 04-13-2011, 11:24 AM   PM User | #11
garevn
New Coder

 
Join Date: Apr 2011
Posts: 95
Thanks: 13
Thanked 1 Time in 1 Post
garevn is an unknown quantity at this point
ups y i did that

Code:
var xmlHttp;
function finding(str) {
   try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
      try {
         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
         xmlHttp = new XMLHttpRequest();
      }
   }
   if(!xmlHttp) {
         alert("Browser does not support HTTP Request");
         return;
   }
   var url="data.php";
   url=url+"?productname="+str;
   url=url+"&sid="+Math.random();
   xmlHttp.onreadystatechange=stateChanged;
   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
}
function stateChanged()
{
if(xmlHttp.readyState==4 && xmlHttp.status==200)
{
document.getElementById("result").innerHTML=xmlHttp.responseText
}
}
function stateChanged()
{
if(xmlHttp.readyState==4 && xmlHttp.status==200)
{
document.getElementById("result").innerHTML=xmlHttp.responseText
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
}


I changed and the div in htm from Result to result.

Now whatever letter i type into the search area it displayes

name:
brand:
price

nothing more...
garevn is offline   Reply With Quote
Old 04-13-2011, 11:30 AM   PM User | #12
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
This is definitely a PHP problem ... the Javascript ajax request is running fine and returning the PHP output. But PHP is not producing the right output.

Can you post your question on the PHP section?
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
garevn (04-16-2011)
Old 04-13-2011, 11:33 AM   PM User | #13
garevn
New Coder

 
Join Date: Apr 2011
Posts: 95
Thanks: 13
Thanked 1 Time in 1 Post
garevn is an unknown quantity at this point
ok i do and i will let u know tnx a lot man
garevn is offline   Reply With Quote
Old 04-13-2011, 01:15 PM   PM User | #14
garevn
New Coder

 
Join Date: Apr 2011
Posts: 95
Thanks: 13
Thanked 1 Time in 1 Post
garevn is an unknown quantity at this point
ok solved m8s it had to do my database stracture nothing important tnx for the code help
garevn is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:51 PM.


Advertisement
Log in to turn off these ads.