Jump to content

Almost working


AL123

Recommended Posts

I integrated some ajax into my php site. I took two examples from w3schools.

One uses an xml file to display some CD info, the other goes to the data base.

They should display in their own place on the page but they replace each other.

Also my return from the database prints twice.

here is the php:

 

<?php

$r=$_GET["r"];

 

$dbh=new PDO('mysql:host=localhost;dbname=///','///','///');

if (!$dbh)

  {

  die('Could not connect: ' . mysql_error());

  }

 

function get_quote($r)

{

global $dbh;

$sql="SELECT name, quote FROM tblQuote WHERE id = '".$r."'";

$sth = $dbh->prepare($sql);

$sth->bindValue(':r', $r, PDO::PARAM_INT);//(':q', $q, PDO::PARAM_INT)

$sth-> execute();

$row = $sth->fetch(PDO::FETCH_ASSOC);

return $row;

 

}

 

$display = get_quote($r);

//print_r($display);   

echo "<table border='1'>

<tr>

<th>name</th>

<th>quote</th>

</tr>";

 

//while($row = mysql_fetch_array($display)) //$row = mysql_fetch_array($result) $key =>

//foreach($display as  $value)

foreach($display as $key => $value)

  {

  echo "<tr>";

  echo "<td>" . $display['name'] . "</td>";

  echo "<td>" . $display['quote'] . "</td>";

  echo "</tr>";

  }

echo "</table>";

 

 

 

Here is a link to the site, It's a work in progress:  http://nuke.empireindustry.com/

 

Thanks,

AL123

Link to comment
Share on other sites

Here is the js that goes with it, along with the CD script.

for the Quote:

 

var xmlhttp;

 

function showUser(str)

{

xmlhttp=GetXmlHttpObject();

if (xmlhttp==null)

  {

  alert ("Browser does not support HTTP Request");

  return;

  }

var url="processReq/getuser.php";

url=url+"?r="+str;//?q=

url=url+"&sid="+Math.random();

xmlhttp.onreadystatechange=stateChanged;

xmlhttp.open("GET",url,true);

xmlhttp.send(null);

}

 

function stateChanged()

{

if (xmlhttp.readyState==4)

{

document.getElementById("txtHint2").innerHTML=xmlhttp.responseText;

}

}

 

function GetXmlHttpObject()

{

if (window.XMLHttpRequest)

  {

  // code for IE7+, Firefox, Chrome, Opera, Safari

  return new XMLHttpRequest();

  }

if (window.ActiveXObject)

  {

  // code for IE6, IE5

  return new ActiveXObject("Microsoft.XMLHTTP");

  }

return null;

}

 

For the CD info:

 

var xmlhttp;

 

function showCD(str) // solve the problem of creating different XMLHTTP objects for different browsers.

{

xmlhttp=GetXmlHttpObject();

if (xmlhttp==null)

  {

  alert ("Your browser does not support AJAX!");

  return;

  }

var url="processReq/getcd.php";

url=url+"?q="+str;

url=url+"&sid="+Math.random();

xmlhttp.onreadystatechange=stateChanged;

xmlhttp.open("GET",url,true);

xmlhttp.send(null);

}

 

function stateChanged()

{

if (xmlhttp.readyState==4)

{

document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

}

}

 

function GetXmlHttpObject()

{

if (window.XMLHttpRequest)

  {

  // code for IE7+, Firefox, Chrome, Opera, Safari

  return new XMLHttpRequest();

  }

if (window.ActiveXObject)

  {

  // code for IE6, IE5

  return new ActiveXObject("Microsoft.XMLHTTP");

  }

return null;

}

Link to comment
Share on other sites

I tried to pass the div id this way: function stateChanged(document.getElementById("txtHint")) [<div id="txtHint">]

and nothing worked. So I tried renaming the function to stateChanged1, and that

flip-flopped the situation. Bare with me I'm new.

 

Thanks, AL123

Link to comment
Share on other sites

I tried to pass the div id this way: function stateChanged(document.getElementById("txtHint")) [<div id="txtHint">]

and nothing worked. So I tried renaming the function to stateChanged1, and that

flip-flopped the situation. Bare with me I'm new.

 

Thanks, AL123

No.

 

function stateChanged(divID) {
    if (xmlhttp.readyState==4) {
        document.getElementById(divID).innerHTML=xmlhttp.responseText;
    }
}

 

Call it like this:

 

xmlhttp.onreadystatechange=stateChanged("txtHint"); 

//OR

xmlhttp.onreadystatechange=stateChanged("txtHint2");

Link to comment
Share on other sites

This is what I did

 

function stateChanged()

{

if (xmlhttp.readyState==4)

{

xmlhttp.onreadystatechange = stateChanged("txtHint2");

 

//document.getElementById("txtHint2").innerHTML=xmlhttp.responseText;

 

}

}

 

What happens to -

innerHTML=xmlhttp.responseText; ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.