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 12 of 12

Thread: AJAX help

  1. #1
    New Coder
    Join Date
    Jan 2010
    Location
    England
    Posts
    35
    Thanks
    4
    Thanked 0 Times in 0 Posts

    AJAX help

    hiya guys

    i have a problem, what im doing is making a timestamp on a web page with php, got that bit sorted using gmdate(); and time(); with echos printing out the format but now i want it not to show on the page unless a user clicks on a button, lets say a drop down box with options like show time in hours and mins or show time in days and hours etc

    so the echo i output i will output in diff formats from the same timestamp using a select drop down box

    i want the user to select `show time in days and hours` and then it shows them the echo that represents that format

    how do i go about this? ive done a bit of reading about it re javascript and php and found out i need to use ajax, i found one example that i couldnt get working

    any help peeps?

  • #2
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post
    This doesn't exactly do what you want but it is a start.

    Code:
    JQUERY:
        $("#clicked").click(function () {
        $("#target").show("slow");
        });
    
    
    Hidden DIV:
    <div id="target" style="display:none"><?php echo $time; ?></div>
    Button:
    <input type="button" id="clicked'>
    Also you could also use .toggle() instead of .show()

    For a drop-down menu: (remember that you'll need to load the value of the item on the list or else it won't work right)
    Code:
    JQUERY:
    $("#myList").change(function() {
        $("#target").show();
        return false;
    });

  • Users who have thanked FrostedFlakes for this post:

    venom6pak (01-24-2011)

  • #3
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    660
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Seeing as your problem has more to do with the Javascript part of AJAX, as opposed to the PHP component, I have moved your question here to the AJAX Category.

    Hopefully you should get more spot on advice here!
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #4
    New Coder
    Join Date
    Jan 2010
    Location
    England
    Posts
    35
    Thanks
    4
    Thanked 0 Times in 0 Posts
    thanks frosted flakes that looks exactly what im looking for forgive me for being so amateurish but ive tried to use that method as follows and its telling me Parse error: syntax error, unexpected '(', expecting T_VARIABLE or '$' in index.php

    <?php
    $("#clicked").click(function () {
    $("#target").show("slow");
    });

    function time(){
    echo gmdate('D, d M Y H:i:s T');
    }
    ?>

    <html>
    <body>

    <div id="target" style="display:none"><?php echo $time; ?></div>
    <input type="button" id="clicked'>
    </body>
    </html>

    where am i going wrong?

  • #5
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    You've mixed up Javascript and PHP: the jQuery code that FrostedFlakes posted is Javascript; don't wrap it in <?php ?> tags, wrap it in HTML <script> tags.

  • #6
    New Coder
    Join Date
    Jan 2010
    Location
    England
    Posts
    35
    Thanks
    4
    Thanked 0 Times in 0 Posts
    ah man i still cant get it working, if i put this code into blank php page i get the time i want `<?php echo gmdate('D d/m/Y H:i:s a'); ?>`

    if i use this below what ive tried to put together from frosted flakes example i hust get a blank screen with `<input type="button" id="clicked'>` displayed in browser

    <html>
    <script>
    $("#clicked").click(function () {
    $("#target").show("slow");
    });
    </script>

    <div id="target" style="display:none"><?php echo gmdate('D d/m/Y H:i:s a'); ?></div>
    <input type="button" id="clicked'>
    </html>

    i then moved the closing div tag to after the input button part to see if that resolved it as shown below but this just left a blank screen

    <html>
    <script>
    $("#clicked").click(function () {
    $("#target").show("slow");
    });
    </script>

    <div id="target" style="display:none"><?php echo gmdate('D d/m/Y H:i:s a'); ?>
    <input type="button" id="clicked'> </div>
    </html>


    i still need a method to display a time in php with a buttonclick

  • #7
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Quote Originally Posted by venom6pak View Post
    if i use this below what ive tried to put together from frosted flakes example i hust get a blank screen with `<input type="button" id="clicked'>` displayed in browser
    Code:
    <html>
     <script>   
        $("#clicked").click(function () {
        $("#target").show("slow");
        });
     </script>
    
    <div id="target" style="display:none"><?php echo gmdate('D d/m/Y H:i:s a'); ?></div>
    <input type="button" id="clicked'> 
    </html>
    Do you mean this is the complete code for your page? What about head and body elements? They are essential ... so the minimum would be something like this:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script>
    $(document).ready(function() {
        $("#clicked").click(function () {
           $("#target").show("slow");
        });
    });
    </script>
    </head>
    <body>
    <div id="target" style="display:none"><?php echo gmdate('D d/m/Y H:i:s a'); ?></div>
    <input type="button" id="clicked" /> 
    </body>
    </html>
    Note: Mind the closing quote of the input ... it was a single quote and had to be changed to a double quote.

    By the way: You are not trying to "display a time in PHP". You are using Javascript to display a previously hidden text (that originally was the output of a PHP script which finished already).

  • #8
    New Coder
    Join Date
    Jan 2010
    Location
    England
    Posts
    35
    Thanks
    4
    Thanked 0 Times in 0 Posts
    thanks for your reply devnull i was at the time doing just that trying to iron out any red herrings so to speak anyway i now have the code on the server exactly like this

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <script type="text/javascript">
    $("#clicked").click(function () {
    $("#target").show("slow");
    });
    </script>

    </head>
    <body>

    <div id="target" style="display:none"><?php echo gmdate('D d/m/Y H:i:s a'); ?> </div>
    <input type="button" id="clicked">

    </body>
    </html>


    thing is all that i get displayed is a tiny little grey button that when clicked doesnt do anything, do you happen to know how to resolve this?

  • #9
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Please compare your code with the code I provided earlier. There is one important difference:

    Put this around your javascript
    Code:
    $(document).ready(function() {
       // your code here
    });
    It will make sure the button exists before you try to assign the click handler to it.

  • Users who have thanked devnull69 for this post:

    venom6pak (01-27-2011)

  • #10
    New Coder
    Join Date
    Jan 2010
    Location
    England
    Posts
    35
    Thanks
    4
    Thanked 0 Times in 0 Posts
    hi again devnull thanks for your reply i must be starting to be very annoying now lol haha but im trying !

    this is how the code is on my page

    <!DOCTYPE html>
    <html>
    <head>
    <script>
    $(document).ready(function() {
    $("#clicked").click(function () {
    $("#target").show("slow");
    });
    });
    </script>
    </head>
    <body>
    <div id="target" style="display:none"><?php echo gmdate('D d/m/Y H:i:s a'); ?></div>
    <input type="button" id="clicked" />
    </body>
    </html>


    but i cannot get it to do anything it just displays to me a tinygrey button


  • #11
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    First of all: You should definitely get used to debugging ... on Firefox install the Firebug add-on and take a look at what you get in the HTML source. It should display the timestamp inside the (hidden) div. If that is not the case, the problem is with PHP and not jQuery. But only you can tell us ....

    And: Well ... you have been using $(), so everybody is expecting that you have the necessary <script src="....../jquery.xxxx.js"> included. Do you? Otherwise the urge to debug would be even greater ;-)
    Last edited by devnull69; 01-27-2011 at 03:01 PM.

  • Users who have thanked devnull69 for this post:

    venom6pak (01-27-2011)

  • #12
    New Coder
    Join Date
    Jan 2010
    Location
    England
    Posts
    35
    Thanks
    4
    Thanked 0 Times in 0 Posts
    ahha yea thanks devnull ive changed it about that many times i forgot to put the source link back in

    it works now what a legend you are


  •  

    Posting Permissions

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