Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts

    Div Tags with PHP

    All,
    I have the following div tag:
    PHP Code:
                    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:
    PHP Code:
    var xmlhttp;

            function 
    updatevotes(strvote) {
                
    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"urltrue);
                
    xmlhttp.send(null);
            }

            function 
    stateChanged(strresponseMsg) {
                
    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:
    PHP Code:
    <div id="txtSHint".$resultsetstory['story_id']."\"> 
    and update the js file to:
    PHP Code:
            function stateChanged(strresponseMsg) {
                
    document.getElementById("txtSHint" str).innerHTML responseMsg;
            } 
    It doesn't work, any ideas?

  • #2
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    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.

  • #3
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Quote Originally Posted by treeleaf20 View Post

    This all works fine. However when I change the div tag to:
    PHP Code:
    <div id="txtSHint".$resultsetstory['story_id']."\"> 
    It doesn't work, any ideas?
    try this instead of above


    PHP Code:
    <div id="txtSHint(".$resultsetstory['story_id'].")\"> 
    Edit: 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


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

    PHP Code:
    echo "<a href=\"viewcomments.php?story_id=".$resultsetstory[story_id]."\">(".$numcomments.") Comments</a> | "
    Last edited by funnymoney; 10-22-2009 at 03:00 AM.

  • #4
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    Did you forget to escape your quote?

    Try this:
    Code:
    <div id=\"txtSHint".$resultsetstory['story_id']."\">

  • #5
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Quote Originally Posted by bacterozoid View Post
    Did you forget to escape your quote?

    Try this:
    Code:
    <div id=\"txtSHint".$resultsetstory['story_id']."\">
    bwaha, i didn't even notice the first one.. omg, it's good to be, ..

  • #6
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    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:
    PHP Code:
    var xmlhttp;

            function 
    updatevotes(strvote) {
                
    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"urltrue);
                
    xmlhttp.send(null);
            }

            function 
    stateChanged(strresponseMsg) {
                
    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:
    PHP Code:
    $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'].")";
      } 

  • #7
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    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...

  • #8
    Regular Coder
    Join Date
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts
    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.
    Code:
    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 = '';
    }
    Last edited by macleodjb; 10-22-2009 at 03:52 AM.

  • #9
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    I actually had two .js files and I combined them into one and it works fine now.

    Thanks for the help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •