68 users online (0 members and 68 guests)  


  Results 1 to 3 of 3

Related

  1. javascript in echo php    Forum: PHP Forum
    Replies: 2
  2. echo Javascript    Forum: PHP Forum
    Replies: 1
  3. If statement inside <td>    Forum: CGI Perl Forum
    Replies: 1
  4. Javascript in php echo()    Forum: PHP Forum
    Replies: 6
  5. Javascript onclick Event Handler:    Forum: Javascript Forum
    Replies: 3
  1. #1
    deliriousthunder's Avatar
    Junior Member

    Status
    Offline
    Join Date
    Feb 2009
    Posts
    29

    Javascript event inside echo statement

    It's a little more awkward than I could explain in the title sorry.

    Rather than describing the problem, I'll paste the code in:

    Code:
    <?php
    if ($registeredUser == 0) {
    	$display_table = "display: none;";
    	echo "You are not a registered user yet. If you would like to post anyway, <a href='#' onClick='dis('post_inputs','inline')'>click here</a>";
    	}
    else {
    	$display_table = "display: inline;";
    	}
    ?>
    <div id="post_inputs" style="<?php echo $display_table?>">a_table_is_contained_here</div>
    And this is the script code I used in the page head:

    Code:
    <script type="text/javascript">
       function dis(objId, sDisplay) {
            var obj = document.getElementById(objId);
             obj.style.display = sDisplay;
          }
    </script>
    I'm figuring it has something to do with the style parameter being defined by a php echo, and was thinking the next thing to try was changing the variable "$display_table" onclick. I just couldn't figure out how to do that

    Thanks for any help,
    Tom

  2. #2
    deliriousthunder's Avatar
    Junior Member

    Status
    Offline
    Join Date
    Feb 2009
    Posts
    29

    Re: Javascript event inside echo statement

    I knew this would happen.

    I spent all day yesterday trying to solve a problem. By late afternoon, after trying a few different methods, I came up with the above solution. Then when it didn't work I couldn't understand why not. The solution is simplistic enough that I just couldn't see anything wrong with it.

    So I posted here, hoping for help.

    5 minutes later I did something I should've done in the first place. Opened up firebug looked at the offending line, and the code was all screwed up.

    Looked at my php echo statement and realised I had used single quotes, screwing up my <a> tag. Replaced the offending quotes with \" and problem is solved.

    Typical

  3. #3
    rangana's Avatar
    Moderator/Allstar

    Status
    Offline
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    317

    Re: Javascript event inside echo statement

    Glad you have it figured out Tom. Thanks for coming back on board and letting us know how you managed to provide a fix
    Checkout my porfolio.
    Please click the button when a member helped you.
    Take time to use Forum's Search function.



Tags for this Thread