WordPress.org

Ready to get started?Download WordPress

Forums

$_GET in a function in functions.php (4 posts)

  1. sam_beckett
    Member
    Posted 2 years ago #

    Hello,

    In my comments.php I inserted a form :

    <form name="myForm" action="" method="GET" style="float:right" >
    		<SELECT NAME="format">
    <OPTION>Test 1
    <OPTION>Test 2
    </SELECT>
    		<SELECT NAME="ordre">
    <OPTION>Example 1
    <OPTION>Example 2
    </SELECT>
    	<input type="submit" name="submit" value="Trier">
        		</form>

    I want to store the selected value in a cookie, so I added this function to my functions.php :

    <?php
    function set_navi_cookie() {
    $sc_ordre = $_REQUEST['ordre'];
    setcookie('naviga',$sc_ordre,time()+60, COOKIEPATH, COOKIE_DOMAIN, false);
    }
    add_action( 'init', 'set_navi_cookie');
    ?>

    but $sc_ordre is null. I tried $_REQUEST['ordre']; but it's always null.

    What I did wrong ?

  2. Peter Wilson
    Member
    Posted 2 years ago #

    Does it work if you change the select to:

    <SELECT NAME="ordre">
    <OPTION value="Example 1">Example 1</OPTION>
    <OPTION value="Example 1">Example 2</OPTION>
    </SELECT>
    ?>
  3. sam_beckett
    Member
    Posted 2 years ago #

    Thanks for your help, but no, it doesn't work. I think that </option> is not needed in this example.

    I can catch the passing variables in single.php for exemple but not in functions.php where I need it.

  4. Mark / t31os
    Moderator
    Posted 2 years ago #

    It's invalid not to have closing tags for options, so yes they are required if you want to write valid code.

    $sc_ordre will be null outside the function because it only has scope inside the function.

    About variable scope
    http://php.net/manual/en/language.variables.scope.php

Topic Closed

This topic has been closed to new replies.

About this Topic