Reputation: 1
Here is the HTML/JS Code:
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("dd123").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","externalphpcode.php?t=" + Math.random(),true);
xmlhttp.send();
}
window.onload=loadXMLDoc();
</script>
And the External PHP Code:
<?php
header ('Location: urlofHTMLcode');
$con = mysql_connect("hostname","databasename","password");
if (!$con)
{
die('Test Error'.mysql_error());
}
$db_selected = mysql_select_db("databasename", $con);
if (!db_selected)
{
die("Error 3 : ".mysql_error());
}
$result= "SELECT COLUMN_1 FROM T_DEALS WHERE COLUMN_2 = '2011-01-03'
and COLUMN_3 = 'VALUE_1'" or die ("Error 4 :".mysql_error());
$row = mysql_fetch_array($result);
echo $row;
mysql_close($con);
exit ();
Here's the issue I've been encountering, with much of the HTML/JS redacted for brevity. I've verified that the HTML/JS file is accurately calling the external PHP file by altering the PHP to insert into mySQL. I've been trying to get the SQL Query result to display in a specified DIV container via the PHP echo command paired with the ".responseText" command via AJAX. What am I doing wrong? There doesn't seem to be an issue with the object referencing in HTML, and when debugging the PHP/mySQL connection an error message would replace the DIV with the appropriate error message which is not occurring once the code is altered to call the mySQL data values.
Thank you in advance.
Upvotes: 0
Views: 887
Reputation: 1
To follow up, I have FINALLY found the bug. It seems in my .php call function I put a value in the header which was restricting proper output. It had nothing to do with the .js or the .php language.
UserZer0, you were absolutely right... when I echo $row it outputs array. But I'm finally getting output and can work with that.
A thousand thank you's!
Upvotes: 0
Reputation: 1501
You didn't call mysql_query on your query so your $result might as well have read
$result="String" Usless statement "String";
Usually you store the sql in a seperate string so you would rewrite as follows:
$sql= "SELECT COLUMN_1 FROM T_DEALS WHERE COLUMN_2 = '2011-01-03' and COLUMN_3 = 'VALUE_1'";
$result= mysql_query($sql);
if(!$result) {die ("Error 4 :".mysql_error());}
Next the $row var is an array which you should either step through with a for loop , or access a specific index. You cant echo arrays directly, all you will ever get is "array" in your output.
The for loop would be something like
for $cell in $row{
echo $cell;
}
and finnally in the code you posted you ommited the final ?> so make sure it's actually there in your php code.
Also I'd really recomend you use jQuery instead of the plan js you are using as it will be less buggy on different browsers, this tutorial will get you started http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery if you haven't heard about it.
Upvotes: 2