using variables from form

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phi

    using variables from form

    Hi there,

    I've just started learning php and I hope you guys could help me with
    this problem. I've found a useful pice of code from a book to make a form
    and I would like to use it. I would like to print the variables (input)
    on the screen , but I can't make it work. I think I have to do something
    with an array, but don't know what. I've tried

    $field = $_POST['field'];
    echo $field;

    and also

    $arr = $_POST['array'];
    print_r($arr);

    but it doesn't work.

    Could some one show me what I'm missing?

    Thanks a bunch!


    Phi

    ############### ############ code ############### ############### #####

    <?php

    // new function
    echo "<br><br><h4>se cond function</h4>\n";

    function print_form($act ion="", $method="post")
    {
    if (empty($action) ){return FALSE;}
    echo "<form action=$action method=$method> \n";
    $numargs = func_num_args() ;
    for($i = 2; $i < $numargs; $i++)
    {
    echo "$i <input type = text name =". func_get_args($ i) . ">
    <br>\n";
    }
    echo "<input type=submit>";

    echo "</form>";
    }
    print_form("aut oform2.php", "", "field", "field2", "field3", "field4");


    // testing if the variable exist...and it doesn't...
    if(isset($_POST['field'])){
    echo "exist";
    }
    else {
    echo "does not exist!";
    }

    ?>

    ############### ############ end code ############### ############### ####
  • Henrik Hansen

    #2
    Re: using variables from form

    Phi wrote:[color=blue]
    > Hi there,
    >
    > I've just started learning php and I hope you guys could help me with
    > this problem. I've found a useful pice of code from a book to make a form
    > and I would like to use it. I would like to print the variables (input)
    > on the screen , but I can't make it work. I think I have to do something
    > with an array, but don't know what. I've tried
    >
    > $field = $_POST['field'];
    > echo $field;
    >
    > and also
    >
    > $arr = $_POST['array'];
    > print_r($arr);
    >
    > but it doesn't work.
    >
    > Could some one show me what I'm missing?
    >
    > Thanks a bunch!
    >[/color]


    try to make a phpinfo(); on the page you submit to and see if the
    variables are there (almost at the bottom).

    --
    Henrik Hansen

    Comment

    • Aphrael

      #3
      Re: using variables from form

      Henrik Hansen wrote:
      [color=blue][color=green]
      >> $arr = $_POST['array'];
      >> print_r($arr);[/color][/color]

      print_r($_POST) ; ??

      Aphrael...
      --
      "La demande mondiale d’ordinateurs n’excédera pas cinq machines."
      (Thomas Watson, Fondateur d'IBM, 1945)

      Comment

      • Phi

        #4
        Re: using variables from form

        Henrik Hansen <hhSPAM@fsck.dk > wrote in
        news:R77Mc.2705 0$Vf.1475592@ne ws000.worldonli ne.dk:
        [color=blue]
        > Phi wrote:[color=green]
        >> Hi there,
        >>
        >> I've just started learning php and I hope you guys could help me with
        >> this problem. I've found a useful pice of code from a book to make a
        >> form and I would like to use it. I would like to print the variables
        >> (input) on the screen , but I can't make it work. I think I have to
        >> do something with an array, but don't know what. I've tried
        >>
        >> $field = $_POST['field'];
        >> echo $field;
        >>
        >> and also
        >>
        >> $arr = $_POST['array'];
        >> print_r($arr);
        >>
        >> but it doesn't work.
        >>
        >> Could some one show me what I'm missing?
        >>
        >> Thanks a bunch!
        >>[/color]
        >
        >
        > try to make a phpinfo(); on the page you submit to and see if the
        > variables are there (almost at the bottom).
        >[/color]

        I don't know if it's this what you mean. Thanks for your time again...


        phpinfo();
        ############### ############### ############### ############### ###
        _REQUEST["Array"] test test test
        _GET["Array"] test test test
        _SERVER["HTTP_HOST"] localhost
        _SERVER["HTTP_USER_AGEN T"] Mozilla/5.0 (Windows; U; Windows NT 5.2; en-
        US; rv:1.7) Gecko/20040626 Firefox/0.8
        _SERVER["HTTP_ACCEP T"] application/x-shockwave-
        flash,text/xml,application/xml,application/xhtml+xml,text/html;q=
        0.9,text/plain;q=0.8,vid eo/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/
        *;q=0.1
        _SERVER["HTTP_ACCEPT_LA NGUAGE"] en-us,en;q=0.5
        _SERVER["HTTP_ACCEPT_EN CODING"] gzip,deflate
        _SERVER["HTTP_ACCEPT_CH ARSET"] ISO-8859-1,utf-8;q=0.7,*;q=0.7
        _SERVER["HTTP_KEEP_ALIV E"] 300
        _SERVER["HTTP_CONNECTIO N"] keep-alive
        _SERVER["PATH"] C:\\WINDOWS\\sy stem32;C:\\WIND OWS;C:\\WINDOWS \\System32
        \\Wbem;C:\\Prog ram Files\\ATI Technologies\\A TI Control Panel;C:\\Progr am
        Files\\Microsof t SQL Server\\80\\Too ls\\BINN
        _SERVER["SystemRoot "] C:\\WINDOWS
        _SERVER["COMSPEC"] C:\\WINDOWS\\sy stem32\\cmd.exe
        _SERVER["PATHEXT"] .COM;.EXE;.BAT; .CMD;.VBS;.VBE; .JS;.JSE;.WSF;. WSH
        _SERVER["WINDIR"] C:\\WINDOWS
        _SERVER["SERVER_SIGNATU RE"] <address>Apac he/2.0.50 (Win32) PHP/4.3.7
        Server at localhost Port 80</address>
        _SERVER["SERVER_SOFTWAR E"] Apache/2.0.50 (Win32) PHP/4.3.7
        _SERVER["SERVER_NAM E"] localhost
        _SERVER["SERVER_ADD R"] 127.0.0.1
        _SERVER["SERVER_POR T"] 80
        _SERVER["REMOTE_ADD R"] 127.0.0.1
        _SERVER["DOCUMENT_R OOT"] Z:/www/apache
        _SERVER["SERVER_ADM IN"] whatyouwant@nom ail.com
        _SERVER["SCRIPT_FILENAM E"] Z:/www/apache/_faq/func/autoform2.php
        _SERVER["REMOTE_POR T"] 2168
        _SERVER["GATEWAY_INTERF ACE"] CGI/1.1
        _SERVER["SERVER_PROTOCO L"] HTTP/1.1
        _SERVER["REQUEST_METHOD "] GET
        _SERVER["QUERY_STRI NG"]
        Array=test+test +test&Array=tes t+test+test&Arr ay=test+test+te st&Array
        =test+test+test &Array=test+tes t+test
        _SERVER["REQUEST_UR I"] /_faq/func/autoform2.php?
        Array=test+test +test&Array=tes t+test+test&Arr ay=test+test+te st&Array=test
        +test+test&Arra y=test+test+tes t
        _SERVER["SCRIPT_NAM E"] /_faq/func/autoform2.php
        _SERVER["PHP_SELF"] /_faq/func/autoform2.php
        _SERVER["PATH_TRANSLATE D"] Z:/www/apache/_faq/func/autoform2.php
        _SERVER["argv"]

        Array
        (
        [0] => Array=test
        [1] => test
        [2] => test&Array=test
        [3] => test
        [4] => test&Array=test
        [5] => test
        [6] => test&Array=test
        [7] => test
        [8] => test&Array=test
        [9] => test
        [10] => test
        )

        _SERVER["argc"] 11
        _ENV["ALLUSERSPROFIL E"] C:\\Documents and Settings\\All Users
        _ENV["ClusterLog "] C:\\WINDOWS\\Cl uster\\cluster. log
        _ENV["CommonProgramF iles"] C:\\Program Files\\Common Files
        _ENV["COMPUTERNA ME"] TOWER
        _ENV["ComSpec"] C:\\WINDOWS\\sy stem32\\cmd.exe
        _ENV["INCLUDE"] C:\\Program Files\\Microsof t Visual Studio .NET 2003\\SDK
        \\v1.1\\include \\
        _ENV["LIB"] C:\\Program Files\\Microsof t Visual Studio .NET 2003\\SDK
        \\v1.1\\Lib\\
        _ENV["NUMBER_OF_PROC ESSORS"] 2
        _ENV["OS"] Windows_NT
        _ENV["Path"] C:\\WINDOWS\\sy stem32;C:\\WIND OWS;C:\\WINDOWS \\System32
        \\Wbem;C:\\Prog ram Files\\ATI Technologies\\A TI Control Panel;C:\\Progr am
        Files\\Microsof t SQL Server\\80\\Too ls\\BINN
        _ENV["PATHEXT"] .COM;.EXE;.BAT; .CMD;.VBS;.VBE; .JS;.JSE;.WSF;. WSH
        _ENV["PROCESSOR_ARCH ITECTURE"] x86
        _ENV["PROCESSOR_IDEN TIFIER"] x86 Family 6 Model 8 Stepping 3,
        GenuineIntel
        _ENV["PROCESSOR_LEVE L"] 6
        _ENV["PROCESSOR_REVI SION"] 0803
        _ENV["ProgramFil es"] C:\\Program Files
        _ENV["SystemDriv e"] C:
        _ENV["SystemRoot "] C:\\WINDOWS
        _ENV["TEMP"] C:\\WINDOWS\\TE MP
        _ENV["TMP"] C:\\WINDOWS\\TE MP
        _ENV["USERPROFIL E"] C:\\Documents and Settings\\Netwo rkService
        _ENV["VS71COMNTO OLS"] C:\\Program Files\\Microsof t Visual Studio .NET 2003
        \\Common7\\Tool s\\
        _ENV["windir"] C:\\WINDOWS
        _ENV["AP_PARENT_ PID"] 1384
        ############### ############### ############### ############### ###

        Comment

        • Marian Heddesheimer

          #5
          Re: using variables from form

          On 23 Jul 2004 11:53:39 GMT, Phi <phiserver@hotm ail.com(remove) >
          wrote:
          [color=blue]
          >Could some one show me what I'm missing?[/color]

          1. you forgot to pass "post" to your function:

          print_form("aut oform2.php", "post", "field", "field2", "field3",
          "field4");

          would work with your script.

          2. the result from func_get_args() is an array, you you have to change

          echo "$i <input type = text name =". func_get_args($ i) . ">

          to this:

          $fields= func_get_args() ;
          echo "$i <input type = text name =". $fields[$i] . ">

          With these changes your code works fine.

          Regards

          Marian

          --
          Tipps und Tricks zu PHP, Coaching und Projektbetreuun g
          Da ich mich seit einiger Zeit aus der Programmierung von Websites zurückgezogen habe, sind meine Tutorials und Schulungsinhalte nicht mehr länger verfügbar, da sie inzwischen veraltet sind.

          Comment

          • Henrik Hansen

            #6
            Re: using variables from form

            Phi wrote:[color=blue]
            > Henrik Hansen <hhSPAM@fsck.dk > wrote in
            > news:R77Mc.2705 0$Vf.1475592@ne ws000.worldonli ne.dk:
            >
            >[color=green]
            >>Phi wrote:
            >>[color=darkred]
            >>>Hi there,
            >>>
            >>>I've just started learning php and I hope you guys could help me with
            >>>this problem. I've found a useful pice of code from a book to make a
            >>>form and I would like to use it. I would like to print the variables
            >>>(input) on the screen , but I can't make it work. I think I have to
            >>>do something with an array, but don't know what. I've tried
            >>>
            >>>$field = $_POST['field'];
            >>>echo $field;
            >>>
            >>>and also
            >>>
            >>>$arr = $_POST['array'];
            >>>print_r($arr );
            >>>
            >>>but it doesn't work.
            >>>
            >>>Could some one show me what I'm missing?
            >>>
            >>>Thanks a bunch!
            >>>[/color]
            >>
            >>
            >>try to make a phpinfo(); on the page you submit to and see if the
            >>variables are there (almost at the bottom).
            >>[/color][/color]


            as you can see in your phpinfo() the variables are in _GET not in _POST,
            so use _GET not _POST or change your submit method in the form.

            --
            Henrik Hansen

            Comment

            • Phi

              #7
              Re: using variables from form

              Marian Heddesheimer <220704.8.nolin k@spamgourmet.c om> wrote in
              news:m622g0dhk5 3v7rs8o7an6i0dl jsp6jcrij@4ax.c om:
              [color=blue]
              > On 23 Jul 2004 11:53:39 GMT, Phi <phiserver@hotm ail.com(remove) >
              > wrote:
              >[color=green]
              >>Could some one show me what I'm missing?[/color]
              >
              > 1. you forgot to pass "post" to your function:
              >
              > print_form("aut oform2.php", "post", "field", "field2", "field3",
              > "field4");
              >
              > would work with your script.
              >
              > 2. the result from func_get_args() is an array, you you have to change
              >
              > echo "$i <input type = text name =". func_get_args($ i) . ">
              >
              > to this:
              >
              > $fields= func_get_args() ;
              > echo "$i <input type = text name =". $fields[$i] . ">
              >
              > With these changes your code works fine.
              >
              > Regards
              >
              > Marian
              >[/color]

              Thanks, your tips did the trick. I changed it and it worked! Now I have a
              much better understanding of how the script and certain functions work.

              Phi

              ##############c ode##########
              /// new function
              echo "<br><br><h4>se cond function</h4>\n";

              function print_form($act ion="", $method="POST")
              {
              if (empty($action) ){return FALSE;}
              echo "<form action=$action method=$method> \n";
              $numargs = func_num_args() ;
              for($i = 2; $i < $numargs; $i++)
              {
              $fields= func_get_args() ;
              echo $fields[$i]. "<input type = text name =".$fields[$i]. ">
              <br>\n";

              }
              echo "<input type=submit>";

              echo "</form>";
              }
              print_form("aut oform2_2.php", "post", "field", "field2", "field3",
              "field4");



              //test the variables

              $field = $_POST['field'];
              echo $field;

              $field2 = $_POST['field2'];
              echo $field2;

              Comment

              • Phi

                #8
                Re: using variables from form

                Henrik Hansen <hhSPAM@fsck.dk > wrote in news:r78Mc.2706 0$Vf.1476102
                @news000.worldo nline.dk:
                [color=blue]
                > Phi wrote:[color=green]
                >> Henrik Hansen <hhSPAM@fsck.dk > wrote in
                >> news:R77Mc.2705 0$Vf.1475592@ne ws000.worldonli ne.dk:
                >>
                >>[/color][/color]
                [color=blue]
                >
                > as you can see in your phpinfo() the variables are in _GET not in _POST,
                > so use _GET not _POST or change your submit method in the form.
                >[/color]

                Thank you for your help. It works fine now with Marian's tips.
                Thank for you tip to use phpinfo(). I'll make use of it more often.

                Kind regards...

                Phi

                Comment

                Working...