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.

php hidden variables problem

Discussion in 'PHP' started by vijaykoul, Aug 9, 2005.

  1. #1
    I have a problem making PHP work to its full. i have installed php 5.x version, apache 1.3.x ver and mysql 3.x version

    all of them are working fine. i have a problem....
    when i pass any hidden variable within a form like in the code below

    <?
    if ($chk == "yes")
    {
    echo "hi";
    }
    ?>
    <form>
    <input type="hidden" name="chk" value="yes">
    <input type="submit" value="submit">
    </form>

    now, when i press this submit button, instead of getting "hi" as the output, i get an error which reads as

    undefined variable "chk"

    i tried to solve this problem many a times, but cant. i even reinstalled php...but that did not solve the purpose....

    i think there must be some problem in the php.ini file

    can anybody help me... please..
    thanks in advance....

    vijay koul
     
    vijaykoul, Aug 9, 2005 IP
  2. anton-io!

    anton-io! Active Member

    Messages:
    540
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    seems to me you have to set the $chk variable first ...

    if you're using a form submit, to retrieve the variable, you would need to use:

    // sets the variable
    $chk = $_POST['chk'];

    if ($chk == "yes")
    {
    echo "hi";
    }


    if you are passing the variable in a URL (eg: something.php?chk=yes), then you would use:

    $chk = $_GET['chk'];

    simple examples:

    http://www.tizag.com/phpT/examples/formvar.php
    http://www.w3schools.com/php/php_forms.asp

    Hope that help!

    :D
     
    anton-io!, Aug 9, 2005 IP
  3. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #3
    Would it not be good practise as well to set the form parameters such as

    <form action=$PHP_SELF method=POST>

    I think it's $_GET as well if you dont state method=POST.
     
    Weirfire, Aug 10, 2005 IP
  4. rvarcher

    rvarcher Peon

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can change your level of error reporting using a line like this at the beginning of your file.

    This won't report notices, like undefined variables:
    
    error_reporting(E_ERROR | E_WARNING | E_PARSE);
    
    PHP:
    This reports everything, which seems to be your default setting.
    
    error_reporting(E_ALL);
    
    PHP:
    PHP error info here:
    http://us2.php.net/error_reporting


    The $_REQUEST[] Global variable contains GET and POST data. You could use that to reference your chk, $_REQUEST['chk']. Referencing $chk directly works but not a best practice.

    PHP global variable info here:
    http://us3.php.net/variables.predefined
     
    rvarcher, Aug 10, 2005 IP