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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    [Newbie] Php calling javascript cookies function

    The question may be very simple since I am newbie in PHP. I have a footer banner on a website that I manage (gold-prediction) and when I click "hide" or "hide forever" the following javascript is called to hide or hide forever the banner:



    /**
    * Main JS File
    *
    * @author Your Inspiration Themes
    * @package YITH Footer Banner
    * @version 1.0.3
    */
    jQuery(document).ready(function($) {
    if($.cookie(templateDir) == '1') {
    $(".fbanner").hide();
    }
    $("#showhidefbanner a").on('click',function(event){
    event.preventDefault();
    $('.fbanner').hide();
    });
    $("#showhideforever").on('click',function(event){
    event.preventDefault();
    $(".fbanner").hide();
    $.cookie(templateDir, '1',{ expires: 365, path: '/' });
    });
    });



    Until here everything ok.
    The problem is that when someone send the form the banner the banner keeps appearing. Therefore I want to called this javascript to never show this banner again. Here is my php code:


    <?php
    /**
    * Footer Banner page
    *
    * @author Your Inspiration Themes
    * @package YITH Footer Banner
    * @version 1.0.0
    */
    ;?>
    <!-- YITH-FOOTER-BANNER -->
    <div class="fbanner">
    <div id="fbannercont" class="group">
    <div id="fbannerlogo">
    <?php if (get_option('yith_fbanner_link')):?><a href="<?php echo get_option('yith_fbanner_link');?>" title="<?php echo get_option('yith_fbanner_message');?>"><?php endif;?>
    <img src="<?php echo get_option('yith_fbanner_image');?>" alt="<?php echo get_option('yith_fbanner_message');?>"/>
    <?php if (get_option('yith_fbanner_link')):?></a><?php endif;?>
    </div>
    <ul id="fbannermess">
    <li>
    <h3>
    <?php if (get_option('yith_fbanner_link')):?><a href="<?php echo get_option('yith_fbanner_link');?>" title="<?php echo get_option('yith_fbanner_message');?>"><?php endif;?>
    <?php echo get_option('yith_fbanner_message');?>
    <?php if (get_option('yith_fbanner_link')):?></a><?php endif;?>
    </h3>
    </li>
    <li>
    <p>
    <?php if (get_option('yith_fbanner_link')):?><a href="<?php echo get_option('yith_fbanner_link');?>" title="<?php echo get_option('yith_fbanner_message');?>"><?php endif;?>
    <?php echo get_option('yith_fbanner_sub_message');?>
    <?php if (get_option('yith_fbanner_link')):?></a><?php endif;?>
    </p>
    </li>
    </ul>
    <div id="fbannernews">
    <?php if ( get_option('yith_fbanner_enable_newsletter_form') == true ): ?>
    <form method="<?php echo get_option('yith_fbanner_newsletter_method') ?>" action="<?php echo _e (get_option('yith_fbanner_newsletter_action') ,'yit');?>" class="fbannernewsletter">
    <input type="hidden" name="form_id" value="147265" />
    <input type="hidden" name="encoding" value="" />
    <fieldset>
    <?php if(get_option('yith_fbanner_newsletter_name_label')){?>
    <ul class="newsfields">
    <li class="newstop">
    <input type="text" name="<?php echo get_option('yith_fbanner_newsletter_name_name') ?>" id="<?php echo get_option('yith_fbanner_newsletter_name_name') ?>" class="name-field text-field" placeholder="<?php echo get_option('yith_fbanner_newsletter_name_label') ?>" />
    </li>
    <li class="newsbottom">
    <input type="text" name="<?php echo get_option('yith_fbanner_newsletter_email_name') ?>" id="<?php echo get_option('yith_fbanner_newsletter_email_name') ?>" class="email-field text-field" placeholder="<?php echo get_option('yith_fbanner_newsletter_email_label') ?>" />
    </li>
    </ul>
    <input type="submit" value="<?php echo get_option('yith_fbanner_newsletter_submit_label') ?>" class="submit-field newssubmit" />
    <?php } else { ?>
    <input type="text" name="<?php echo get_option('yith_fbanner_newsletter_email_name') ?>" id="<?php echo get_option('yith_fbanner_newsletter_email_name') ?>" class="email-field text-field" placeholder="<?php echo get_option('yith_fbanner_newsletter_email_label') ?>" />
    <input type="submit" value="<?php echo get_option('yith_fbanner_newsletter_submit_label') ?>" class="submit-field" />
    <?php };?>
    <?php $hiddenfields = get_option('yith_fbanner_newsletter_hidden_fields');
    if ($hiddenfields) :
    $result = explode('&',$hiddenfields);
    foreach ($result as $hivalue ) :
    $formvalue = explode('=',$hivalue);?>
    <input type="hidden" id="<?php echo $formvalue[0] ?>" name="<?php echo $formvalue[0] ?>" value="<?php echo $formvalue[1] ?>" />
    <?php endforeach; endif;?>
    </fieldset>
    </form>
    <?php endif; ?>
    </div>
    </div><!-- fbannercont -->

    <ul class="hiderzone">
    <li>
    <div id="showhideforever">
    <a href="#" title="<?php _e( get_option('yith_fbanner_hide_forever_message') ,'yit');?>">
    <?php echo _e( get_option('yith_fbanner_hide_forever_message') ,'yit');?>
    </a>
    </div>
    </li>
    <li>
    <div id="showhidefbanner">
    <a href="#" title="<?php _e( get_option('yith_fbanner_hide_message') ,'yit');?>">
    <?php _e( get_option('yith_fbanner_hide_message') ,'yit');?>
    </a>
    </div>
    </li>
    </ul>
    </div><!-- fbanner -->
    <!-- YITH-FOOTER-BANNER -->


    In resume, I want to call the javasript function also when the "Submit" button is clicked. Does anyone know how to solve this problem?
    Thanks in advance

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,581
    Thanks
    23
    Thanked 643 Times in 642 Posts
    Your showhidefbanner anchor JS does not set the cookie, nor does it allow for the banner to reappear. So if that was clicked when the form is sent, the page reloads and the banner appears.
    Why not set the cookie in the showhidefbanner click function, but less the 365. Maybe a couple of hours.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    Your showhidefbanner anchor JS does not set the cookie, nor does it allow for the banner to reappear. So if that was clicked when the form is sent, the page reloads and the banner appears.
    Why not set the cookie in the showhidefbanner click function, but less the 365. Maybe a couple of hours.
    Thanks! But I didnt understand how changing the 365days will change anything... And can you explain how can I set to hide the banner from the submit click?

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,581
    Thanks
    23
    Thanked 643 Times in 642 Posts
    I don't remember how to tie my shoes after a good nights sleep and now you ask questions about something I said 10 days ago; I'll have to start all over again.

    When you submit the form the page reloads (I'm guessing). I looked at your spaghetti mess of PHP/HTML got a head ache, so looked for $_COOKIE, used by php to read a cookie, but not here.

    Your temp show/hide buttons should set a cookie/remove the cookie as well as hiding and showing the <div class="fbanner"> and these should not be set for 365. I say that because a temp hide should not stay in effect if the user comes back to this page.

    Your PHP should look for this cookie and display or not fbanner accordingly.

    Hope this helps. If not please tell me what does not work, lets say, today or tomorrow.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #5
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    I don't remember how to tie my shoes after a good nights sleep and now you ask questions about something I said 10 days ago; I'll have to start all over again.

    When you submit the form the page reloads (I'm guessing). I looked at your spaghetti mess of PHP/HTML got a head ache, so looked for $_COOKIE, used by php to read a cookie, but not here.

    Your temp show/hide buttons should set a cookie/remove the cookie as well as hiding and showing the <div class="fbanner"> and these should not be set for 365. I say that because a temp hide should not stay in effect if the user comes back to this page.

    Your PHP should look for this cookie and display or not fbanner accordingly.

    Hope this helps. If not please tell me what does not work, lets say, today or tomorrow.
    Haha fair enough. Sorry for the delay! And sorry again now. I was too busy these last days... Thanks for the answer, but what I not being able to do is to activate the same cookie that is activated when "Hide Forever" (showhideforever) is clicked.

  • #6
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,581
    Thanks
    23
    Thanked 643 Times in 642 Posts
    Here are just the essentials Set the cookie with jquery/javascript and read the cookie with php. Php controls the page write.
    PHP Code:
    <?php
    $cook 
    'gone';
    if(isset(
    $_COOKIE["banner"]))
        
    $cook $_COOKIE["banner"];
    ?>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta charset="UTF-8" />
    <title>Title of the document</title>
    </head>

    <body>
    <?php
    $str 
    '';
    if(
    $cook == 'gone')
    $str = <<<DEMO
    <div class="fbanner">
        <div id="fbannercont" class="group">
            <div id="fbannerlogo">
            yith_fbanner_link
            <a href="" title="">
                <img src="01.jpg" alt=""/>
            </a>
            </div>
        </div>
    <button id="hideforever" value="Go Away">Go Away</button>
    </div>
    DEMO;
    echo 
    $str;
    ?>

    <script type='text/javascript' src='javascript/jquery.js'></script>
    <script type="text/javascript">

    jQuery(document).ready(function($) {
        $("#hideforever").click(function(){
            $(".fbanner").hide();
            setCookie('banner', 1, 365);
        });
    });


    function setCookie(cname, cvalue, exdays) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays*24*60*60*1000));
        var expires = "expires="+d.toUTCString();
        document.cookie = cname + "=" + cvalue + "; " + expires;
    }
    </script>
    </body>
    </html>
    The write a cookie code come from JavaScript Cookies
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #7
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,581
    Thanks
    23
    Thanked 643 Times in 642 Posts
    I gave you an answer earlier today, but this piece of shoe forum sent it to be verified. Maybe that's because of all the four letter words I use or the underlying references to dope that appear in all my posts. Time will tell if they find what I have hidden in code. If they don't it might be published.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #8
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,449
    Thanks
    17
    Thanked 273 Times in 273 Posts
    Properly made a cookie doesn't know if it's made by JS or PHP. However for either one to read it, it has to be made first, so it's only read on reload. But PHP has a much easier time with cookies, as it just treats is as a simple array. If you need something to be read right away with PHP just put something in $_SESSION.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #9
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I already have the code for the cookie. The cookie is activated when I click "hide forever button" (Stock Forecast Based On a Predictive Algorithm | I Know First |Stock Forecast Based On a Predictive Algorithm | I Know First | - Stock Forecast Based On a Predictive Algorithm. Contact Us: contact@iknowfirst.com)
    I JUST want that the cookie will be activated when the submit button is clicked. How can I do this? What do I have to add or change in my code????

  • #10
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I gave you also an answer and I dont know why didnt post...
    I already have the cookie to hide for 365 days. Therefore, I think that I already have the function. I just dont know where to place it.
    When the hide forever is clicked (check: gold-prediction.com) it activates the cookie. I want to activate also when the submit is clicked.


  •  

    Tags for this Thread

    Posting Permissions

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