1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Global variable

Discussion in 'PHP' started by piropeator, Dec 1, 2016.

  1. #1
    Hi.
    I have this code:
    <?php
    global $fecha_trabajo;
    $fecha_trabajo = $_POST['fecha'];  <--- receive value from form
    $tpl = new Plantilla();
    $tpl->display('plantilla.tpl.php');
    ?> 
    PHP:
    In plantilla.tpl.php
    <!DOCTYPE html>
    <html lang="es">
    <body>
    <form name="view" action="process.php" method="POST" enctype="multipart/form-data">
    (.....)
    </form>
    </body>
    </html> 
    HTML:
    In process.php:
    <?php
    echo $fecha_trabajo;   <-- I trying to show this variable but....
    ?> 
    PHP:
    The system show me this message
    What is wrong? Can somebody help me?
     
    Solved! View solution.
    piropeator, Dec 1, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    PoPSiCLe, Dec 1, 2016 IP
  3. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #3
    I guess it's using that $GLOBALS variable, but, I can't understand how to use it. :(
     
    piropeator, Dec 1, 2016 IP
  4. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #4
    I use this:
    <?php
    global $fecha_trabajo;
    $fecha_trabajo = $_POST['fecha'];  <--- receive value from form
    $GLOBALS['fd'] = $fecha_trabajo;
    $tpl = new Plantilla();
    $tpl->display('plantilla.tpl.php');
    ?> 
    PHP:
    And in the other php file called by Smarty template:
    <?php
    echo $GLOBALS['fd'];   <-- I trying to show this variable but....
    ?> 
    PHP:
    But show me the same message.
     
    piropeator, Dec 2, 2016 IP
  5. #5
    The code should be like this:
    
    <?php
    $GLOBALS['fd'] = $_POST['fecha'];
    $tpl = new Plantilla();
    $tpl->display('plantilla.tpl.php');
    ?>
    
    PHP:
    
    <?php
    echo $fd;
    ?>
    
    PHP:
     
    PoPSiCLe, Dec 2, 2016 IP
  6. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #6
    There is no reason for you to use globals as $_POST is global in itself, just do `echo $_POST['fecha']` in the template and it should work fine.
     
    ThePHPMaster, Dec 3, 2016 IP
  7. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #7
    I changed that code. And I use $session[], so the variable always is in memory.
     
    piropeator, Dec 6, 2016 IP
  8. MCMatt

    MCMatt Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    This is a better route to go than what you were previously doing. Be sure to read up on security and vulnerabilities for sessions
     
    MCMatt, Feb 16, 2017 IP