...

View Full Version : Div Tags with PHP



treeleaf20
10-22-2009, 02:49 AM
All,
I have the following div tag:

echo "<div id=\"txtHint".$resultsetstory['story_id']."\"><form>";
echo "<a href=\"viewcomments.php?story_id=$resultsetstory[story_id]\">(".$numcomments.") Comments</a> | ";
echo "Definite (".$resultsetvotes['vote_up'].") <img src=\"images/thumb_up.png\" alt=\"\" onClick=\"javascript:updatevotes($resultsetstory[story_id], 'Up');\"> | <img src=\"images/thumb_down.png\" alt=\"\" onClick=\"javascript:updatevotes($resultsetstory[story_id], 'Down');\"> I've heard worse (".$resultsetvotes['vote_down'].")";
echo "";
echo "</form>";
echo "</div>";


I have a javascript file to handle the onclick:

var xmlhttp;

function updatevotes(str, vote) {
xmlhttp = GetXmlHttpObject();
if (xmlhttp == null) {
alert("Browser does not support HTTP Request");
return;
}
var url = "updatestoryvotes.php";
url = url + "?id=" + str + "&vote=" + vote;
url = url + "&sid=" + Math.random();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
stateChanged(str,xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
}

function stateChanged(str, responseMsg) {
document.getElementById("txtHint" + str).innerHTML = responseMsg;
}

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;
}

This all works fine. However when I change the div tag to:

<div id=\"txtSHint".$resultsetstory['story_id']."\">

and update the js file to:

function stateChanged(str, responseMsg) {
document.getElementById("txtSHint" + str).innerHTML = responseMsg;
}

It doesn't work, any ideas?

treeleaf20
10-22-2009, 02:51 AM
Also when I refresh the page the count is updated so I know the PHP page is working but for some reason it doesn't get back to the stateChanged() when I change the div id.

funnymoney
10-22-2009, 02:54 AM
This all works fine. However when I change the div tag to:

<div id=\"txtSHint".$resultsetstory['story_id']."\">

It doesn't work, any ideas?

try this instead of above



<div id=\"txtSHint(".$resultsetstory['story_id'].")\">

sry i edited, but i think i'm waaay off here, just to let you know.. i thought that instead of id stands onclick.. :(

nice code btw


ah i think i figured it out

this line

echo "<a href=\"viewcomments.php?story_id=$resultsetstory[story_id]\">(".$numcomments.") Comments</a> | ";

you can't echo arrays inline you need to escape them



echo "<a href=\"viewcomments.php?story_id={$resultsetstory[story_id]}\">(".$numcomments.") Comments</a> | ";

or


echo "<a href=\"viewcomments.php?story_id=".$resultsetstory[story_id]."\">(".$numcomments.") Comments</a> | ";

bacterozoid
10-22-2009, 02:55 AM
Did you forget to escape your quote?

Try this:


<div id=\"txtSHint".$resultsetstory['story_id']."\">

funnymoney
10-22-2009, 03:01 AM
Did you forget to escape your quote?

Try this:


<div id=\"txtSHint".$resultsetstory['story_id']."\">

bwaha, i didn't even notice the first one.. omg, it's good to be, .. :p

treeleaf20
10-22-2009, 03:04 AM
Nope, everything works fine when I didn't have the S in it. All I did was add the S. I also did some alert troubleshooting and it gets to the alert("it gets here1") but doesn't give me an alert in the stateChanged().

Here is the debugging with the alerts:

var xmlhttp;

function updatevotes(str, vote) {
xmlhttp = GetXmlHttpObject();
if (xmlhttp == null) {
alert("Browser does not support HTTP Request");
return;
}
var url = "updatestoryvotes.php";
url = url + "?id=" + str + "&vote=" + vote;
url = url + "&sid=" + Math.random();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
alert ("it gets here1")
stateChanged(str,xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
}

function stateChanged(str, responseMsg) {
alert("it gets here2");
alert(str);
document.getElementById("txtSHint" + str).innerHTML = responseMsg;
}

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;
}

If your curious, the PHP page it's calling is:

$id=stripslashes($_GET["id"]);
$vote=stripslashes($_GET["vote"]);

include "config.php";

if($vote=="Up"){
$qryvotes = "Select vote_up from story where story_id='$id'";
$resultvotes = mysql_query($qryvotes);
$resultsetvotes = mysql_fetch_array($resultvotes);

$sqladjustvotes="Update story set vote_up=$resultsetvotes[vote_up]+1 where story_id='$id'";
$resultchangevotes = mysql_query($sqladjustvotes);
}
if($vote=="Down"){
$qryvotes = "Select vote_down from story where story_id='$id'";
$resultvotes = mysql_query($qryvotes);
$resultsetvotes = mysql_fetch_array($resultvotes);

$sqladjustvotes="Update story set vote_down=$resultsetvotes[vote_down]+1 where story_id='$id'";
$resultchangevotes = mysql_query($sqladjustvotes);
}


$qryvotes = "Select vote_up, vote_down from story where story_id='$id'";
$resultvotes = mysql_query($qryvotes);

$qrynumcomments = "Select comment_id from comments where story_id='$id'";
$resultcount = mysql_query($qrynumcomments);
$numrows = mysql_num_rows($resultcount);
if($numrows==0){
$numcomments=0;
}else{
$numcomments=$numrows;
}


while($resultsetvotes = mysql_fetch_array($resultvotes))
{
echo "<a href=\"viewcomments.php?story_id=$id\">(".$numcomments.") Comments</a> | Definite (".$resultsetvotes['vote_up'].") | I've heard worse (".$resultsetvotes['vote_down'].")";
}

treeleaf20
10-22-2009, 03:16 AM
I tried your updates but that didn't work either. The PHP page worked fine as well. The only thing I changed at all was adding the S in both place and now it doesn't work...

macleodjb
10-22-2009, 03:49 AM
i don't see how you can get httpresponsetext when you haven't opened the file yet. You don't open the file until after this call.

This is how i handle my ajax calls...pretty clean i think.


function add_comment() {

var oXHR = createXHR();

var name = document.getElementById('c_name').value;
var comment = document.getElementById('comment').value;
var permalink = document.getElementById('permalink').value;
var queryString = "?name=" + name + "&comment=" + comment + "&permalink=" + permalink;
oXHR.open("GET", "../ajax/add_comment.php" + queryString, true);

oXHR.onreadystatechange = function () {
if (oXHR.readyState == 4) {
if (oXHR.status == 200) {
saveResult(oXHR.responseText);
} else {
saveResult("An error occurred: " + oXHR.statusText);
}
}
};
oXHR.send(null);
document.getElementById('c_name').value = '';
document.getElementById('comment').value = '';
}

treeleaf20
10-22-2009, 03:54 AM
I actually had two .js files and I combined them into one and it works fine now.

Thanks for the help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum