Community discussion forum

php passing values

  • 4 years ago

    i guys, im new here and it's my first time to code on php


    i have problems regarding passing of values from one page to another.


    like:
    page1:


    $month = 12;


    page2:


    --how can i get the value of $month here.



    thanks

  • 4 years ago
    (this should be in the PHP section....)

    there are many ways, perhaps the best two are GET or POST variables (you could use a cookie but that would be silly in this case, for a number of reasons)

    the GET variable goes in the query string. look at the URL of this page and you'll see a question mark and other stuff at the end of the address. so if you want to pass a variable on, you could build up a hyperlink like this:

    <? $month = 12; ?>

    Click <a href="./page2.php?month=<?=$month?>">here</a> to view page two.



    and then on page two, to access the GET variable you do it like this:

    The month is <?=$_GET["month"]?>.


    if you want to use a POST variable, then set up a form and have a text box labelled "month", submit the form to "page2.php" and then access like this: The month is <?=$_POST["month"]?>.

    I hope that's clear, any questions just post back
  • 4 years ago

    thank you very much! that seems to help me a lot.


    now i have a new inquiry.


    ---i created an array of textboxes:


    page1.php


    <form action="/page1.php" method="post">
    <?for ($x = 0; $x <=2; $x++)
    {
    ?>
          <input type = "text" name = "boxname[]">
    <?
    {
    ?>



    <input type="submit" />
    </form>



    --the question is, if i want to get the values of each text box, how can i?
    printr($POST['boxname']) as i know is used to print all the values, but it only want to read each the value to be used in updating the database individually according to the index.



  • 4 years ago

    it puts it as an array, so construct a loop with a variable such as $x which will increment one each time, and put it in the square brackets

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!