Jenny Dithe
09-28-2010, 07:03 AM
Hi,
I have webpage 1 which when a button is clicked calls up an ajax script, this ajax script also has a button that when clicked should call a second ajax script. The first part works fine, the second does not work at all.
I am slightly confused as to where I should have my second ajax function. Should I have it in the head of webpage 1 or in the head of the first ajax script called. I have tried both ways and neither works.
Here is the script:
Webpage 1:
<head>
<script type="text/javascript">
function loadXMLDoc(){
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("show1").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","getAjax1.php",true);
xmlhttp.send();
}
</script>
<script type="text/javascript">
function loadXMLDOC2(){
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("show2").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","getAjax2.php",true);
xmlhttp.send();
}
</script>
<head>
<body>
<?php
$con = mysql_connect("localhost","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
$result = mysql_query("SELECT * FROM table1
WHERE title ='Mytitle' ORDER BY Date, Time DESC LIMIT 15");
echo "<table id='new' width='100%'>
<tr>";
while($row = mysql_fetch_array($result))
{
echo "<td><input type='button' class='button5' name='subject' value='" . $row[subject] . "' onclick='loadXMLDoc()'></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<br />
<div id="show1"></div>
</body>
</html>
Here is the first ajax script/ getAjax1.php:
<head>
</head>
<body>
<div class="standard">
<?php
$con = mysql_connect("localhost","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
$result = mysql_query("SELECT * FROM table1
WHERE title ='MyTitle' ORDER BY Date, Time DESC LIMIT 1");
echo "<table id='new' width='100%'>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<th colspan='2'>" . $row[subject] . "</th>";
echo "</tr><tr>";
echo "<td colspan='2'><br />" . $row[message] . "<br /></td>"; echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<br />
<br />
<input type="button" class="button4" name="comment" value="Add a Comment" onclick="loadXMLDoc2()" />
<br />
<br />
<div id="show2"> </div>
<br />
<br />
</div>
</body>
</html>
I am also not sure if I can call this function anything I like or if it has to be a variant of loadXMLDoc?
Help would be great ... :)
I have webpage 1 which when a button is clicked calls up an ajax script, this ajax script also has a button that when clicked should call a second ajax script. The first part works fine, the second does not work at all.
I am slightly confused as to where I should have my second ajax function. Should I have it in the head of webpage 1 or in the head of the first ajax script called. I have tried both ways and neither works.
Here is the script:
Webpage 1:
<head>
<script type="text/javascript">
function loadXMLDoc(){
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("show1").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","getAjax1.php",true);
xmlhttp.send();
}
</script>
<script type="text/javascript">
function loadXMLDOC2(){
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("show2").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","getAjax2.php",true);
xmlhttp.send();
}
</script>
<head>
<body>
<?php
$con = mysql_connect("localhost","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
$result = mysql_query("SELECT * FROM table1
WHERE title ='Mytitle' ORDER BY Date, Time DESC LIMIT 15");
echo "<table id='new' width='100%'>
<tr>";
while($row = mysql_fetch_array($result))
{
echo "<td><input type='button' class='button5' name='subject' value='" . $row[subject] . "' onclick='loadXMLDoc()'></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<br />
<div id="show1"></div>
</body>
</html>
Here is the first ajax script/ getAjax1.php:
<head>
</head>
<body>
<div class="standard">
<?php
$con = mysql_connect("localhost","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
$result = mysql_query("SELECT * FROM table1
WHERE title ='MyTitle' ORDER BY Date, Time DESC LIMIT 1");
echo "<table id='new' width='100%'>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<th colspan='2'>" . $row[subject] . "</th>";
echo "</tr><tr>";
echo "<td colspan='2'><br />" . $row[message] . "<br /></td>"; echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<br />
<br />
<input type="button" class="button4" name="comment" value="Add a Comment" onclick="loadXMLDoc2()" />
<br />
<br />
<div id="show2"> </div>
<br />
<br />
</div>
</body>
</html>
I am also not sure if I can call this function anything I like or if it has to be a variant of loadXMLDoc?
Help would be great ... :)