help needed - passing values between two loads of the same page

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

    #1

    help needed - passing values between two loads of the same page

    Hi,
    How do I pass a string from one call to a php block to another call in
    the
    same page but from another form?

    Here's my full php code:

    I'm using two forms in the same page. A hidden field 'f' with values
    '1' and
    '2' are used to distinguish calls from one form from those from the
    other.

    <html>
    <head><title>Da tabase Access</title></head>
    <body>

    <form name="QueryForm " action="db2.php " method="POST" />

    <input type="hidden" name="f" value="1" />

    <input type="text" width="100" name="txtQuery"
    value="select * from license_master; " /> <br/>

    <input type="submit" name="Query" value="Query" /> <br/>
    </form>

    <?php
    $s = ""; $nl = "<br/>";
    switch ($_POST['f']){
    case '1':
    $conn=odbc_conn ect('mysqldsnon e','root','kati patang');
    $sql = $_POST['txtQuery'];
    $rs=odbc_exec($ conn,$sql);
    echo "<table width=\"60%\">\ n";
    while (odbc_fetch_row ($rs)){
    $abbr=odbc_resu lt($rs,"abbr");
    $fullname=odbc_ result($rs,"ful lname");
    $link=odbc_resu lt($rs,"link");
    echo "<tr>
    <td colspan=\"3\" background=\"bl ue.png\">
    <p class=\"white-text\">".$abbr. "</p></td></tr>\n";

    echo "<tr><td>".$abb r."</td><td>".$fulln ame
    ."</td><td><a
    href=\"".$link. "\">".$link ."</a></td></tr>\n";

    $s .= $abbr.",".$full name.",".$link. "\n";
    //making the comma separated string here
    }

    //*************** *************** *************** *************** *************
    /*
    PROBLEM: somehow pass $s, which now contains the data in csv form
    to the next call of the same page from the second form which is below
    tried cookies but they do not work because you can only set a cookie
    right
    at the start of the page
    */
    //*************** *************** *************** *************** *************

    echo "</table>\n";
    odbc_close($con n);
    break;
    case '2':
    $of = $_POST['outputfile'];
    $of ="C:/Program Files/Apache Group/Apache2/htdocs/php/lm.csv";
    $handle = fopen($of,"w+") ;
    if (!$handle){echo "Cannot open file $of for writing".$nl;}
    if (!fwrite($handl e,$s)){echo "Cannot write to file $of".$nl;}
    fclose($handle) ;
    break;
    default:
    ;
    }
    ?>
    <form name="ExportFor m" action="db2.php " method="POST" />

    <input type="hidden" name="f" value="2" /><br/>

    <input type="text" name="outputfil e" value="lm.csv" /> <br/>
    <input type="submit" name="SubmitExp ort" value="Export" />
    <br/>
    </form>
    </body>
    </html>

    How do I get the value I stored into $s in case '1', when execution
    moves to case '2'?

    Any help is greatly appreciated.
    Thanks in advance.
    Joseph S.

  • romain.jouin@gmail.com

    #2
    Re: help needed - passing values between two loads of the same page

    I never used ob_start() and ob_end_flush() , but you can check the
    manual page :



    it says :

    " As of PHP 4, you can use output buffering to send output prior to the
    call of this function, with the overhead of all of your output to the
    browser being buffered in the server until you send it. You can do this
    by calling ob_start() and ob_end_flush() in your script, or setting the
    output_bufferin g configuration directive on in your php.ini or server
    configuration files."

    Another "make it yourself" solution would be to define a javascript
    function that you call on the "onload" event of the page. as you have
    to put it on the "body" balise, you make it calling the "submit" event
    of an hidden form that call another javascript function with your
    string as a param (string that you know at the end of your php script".
    This last function accessing a cookie... But as you don't want your
    page to be sent, you have to make this function to return false (abort
    the sending process).
    Yes, A little bit tricky...

    <body onload=document .forms[3].submit()>
    <form 1>
    ....
    <form 2>
    ....
    <form 3>
    <input type=submit onsubmit="set_c ookie(ccvs_stri ng)">

    <javascript
    function set_cookie(para m)
    {
    ....
    return false;
    }[color=blue]
    >[/color]

    ok, that is a first jet... Sure there is simpler to do it, check php
    manual...
    Regards,
    JR.

    Comment

    • Jerry Stuckle

      #3
      Re: help needed - passing values between two loads of the same page

      Joseph S. wrote:[color=blue]
      > Hi,
      > How do I pass a string from one call to a php block to another call in
      > the
      > same page but from another form?
      >
      > Here's my full php code:
      >[/color]

      <code snipped>
      [color=blue]
      >
      > How do I get the value I stored into $s in case '1', when execution
      > moves to case '2'?
      >
      > Any help is greatly appreciated.
      > Thanks in advance.
      > Joseph S.
      >[/color]

      How about placing it in the $_SESSION array? Start the session at the
      beginning of the page and store $s in $_SESSION['s']. Then pull it out
      on the second call. (Be sure to clear $_SESSION['s'] on first call to
      the page).

      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • Chung Leong

        #4
        Re: help needed - passing values between two loads of the same page

        Joseph S. wrote:[color=blue]
        > Hi,
        > How do I pass a string from one call to a php block to another call in
        > the
        > same page but from another form?[/color]

        Use a hidden field.

        Comment

        • Joseph S.

          #5
          Re: help needed - passing values between two loads of the same page

          Hi,[color=blue]
          > How about placing it in the $_SESSION array? Start the session at the
          > beginning of the page and store $s in $_SESSION['s']. Then pull it out
          > on the second call. (Be sure to clear $_SESSION['s'] on first call to
          > the page).[/color]

          I did that, the value of $s is getting stored properly in
          $_SESSION['s'], but in the second load of the page, $_SESSION['s'] is
          not showing in the case '2' code.

          Either script does not hav e access to $_SESSION or values held in
          $_SESSION are removed. Does PHP give an invalid index error in the case
          that I access $_SESSION['s'] and $_SESSION does not have an 's'
          element?
          I am asking this because, it is giving no such error, probably meaning
          that the $_SESSION array has 's' but the value is getting cleaned.

          also, in the case '2' code,
          print_r($_SESSI ON);
          outputs nothing at all!

          Also, I have tried -
          session_name("s amplesession");
          session_start() ;
          in the case '1' code at the start.

          But in the case '2' code,
          a session_is_regi stered("samples ession"); gives a FALSE.

          I have register_global s=On in my php.ini.


          Any other function needs to be used?


          Joseph S.

          Comment

          • Joseph S.

            #6
            Re: help needed - passing values between two loads of the same page


            Found a good place for tutorials, which also cleared some of the
            problems of sessions that I had in this thread:

            http://www.tutorialized.com/

            http://www.tutorialized.com/tutorials/PHP/1

            http://www.tutorialized.com/tutorial...and-Sessions/1

            Obviously, there may be others better, but for now, this place looks
            good.

            Regards,
            Joseph S.

            Comment

            • Jerry Stuckle

              #7
              Re: help needed - passing values between two loads of the same page

              Joseph S. wrote:[color=blue]
              > Hi,
              >[color=green]
              >>How about placing it in the $_SESSION array? Start the session at the
              >>beginning of the page and store $s in $_SESSION['s']. Then pull it out
              >>on the second call. (Be sure to clear $_SESSION['s'] on first call to
              >>the page).[/color]
              >
              >
              > I did that, the value of $s is getting stored properly in
              > $_SESSION['s'], but in the second load of the page, $_SESSION['s'] is
              > not showing in the case '2' code.
              >
              > Either script does not hav e access to $_SESSION or values held in
              > $_SESSION are removed. Does PHP give an invalid index error in the case
              > that I access $_SESSION['s'] and $_SESSION does not have an 's'
              > element?
              > I am asking this because, it is giving no such error, probably meaning
              > that the $_SESSION array has 's' but the value is getting cleaned.
              >
              > also, in the case '2' code,
              > print_r($_SESSI ON);
              > outputs nothing at all!
              >
              > Also, I have tried -
              > session_name("s amplesession");
              > session_start() ;
              > in the case '1' code at the start.
              >
              > But in the case '2' code,
              > a session_is_regi stered("samples ession"); gives a FALSE.
              >
              > I have register_global s=On in my php.ini.
              >
              >
              > Any other function needs to be used?
              >
              >
              > Joseph S.
              >[/color]

              First of all, you should never have register_global s set to on. It's a
              huge security risk.

              As to the session variables not working. They work fine for me.
              However, without seeing your entire code, it's almost impossible to
              determine why sessions are failing.


              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • littlefire

                #8
                Re: help needed - passing values between two loads of the same page

                Jerry Stuckle wrote:[color=blue]
                > Joseph S. wrote:
                >[color=green]
                >> Hi,
                >>[color=darkred]
                >>> How about placing it in the $_SESSION array? Start the session at the
                >>> beginning of the page and store $s in $_SESSION['s']. Then pull it out
                >>> on the second call. (Be sure to clear $_SESSION['s'] on first call to
                >>> the page).[/color]
                >>
                >>
                >>
                >> I did that, the value of $s is getting stored properly in
                >> $_SESSION['s'], but in the second load of the page, $_SESSION['s'] is
                >> not showing in the case '2' code.
                >>
                >> Either script does not hav e access to $_SESSION or values held in
                >> $_SESSION are removed. Does PHP give an invalid index error in the case
                >> that I access $_SESSION['s'] and $_SESSION does not have an 's'
                >> element?
                >> I am asking this because, it is giving no such error, probably meaning
                >> that the $_SESSION array has 's' but the value is getting cleaned.
                >>
                >> also, in the case '2' code,
                >> print_r($_SESSI ON);
                >> outputs nothing at all!
                >>
                >> Also, I have tried -
                >> session_name("s amplesession");
                >> session_start() ;
                >> in the case '1' code at the start.
                >>
                >> But in the case '2' code,
                >> a session_is_regi stered("samples ession"); gives a FALSE.
                >>
                >> I have register_global s=On in my php.ini.
                >>
                >>
                >> Any other function needs to be used?
                >>
                >>
                >> Joseph S.
                >>[/color]
                >
                > First of all, you should never have register_global s set to on. It's a
                > huge security risk.
                >
                > As to the session variables not working. They work fine for me.
                > However, without seeing your entire code, it's almost impossible to
                > determine why sessions are failing.
                >
                >[/color]

                The two major causes of session failure:
                1. The two pages don't reside on the same domain
                2. One of the two pages does not call session_start() - this needs to be
                done on every single page where a session var is used

                :)

                Comment

                • Jerry Stuckle

                  #9
                  Re: help needed - passing values between two loads of the same page

                  littlefire wrote:[color=blue]
                  >
                  > The two major causes of session failure:
                  > 1. The two pages don't reside on the same domain
                  > 2. One of the two pages does not call session_start() - this needs to be
                  > done on every single page where a session var is used
                  >
                  > :)[/color]

                  Those are two major causes. I wouldn't say they are THE two major causes.

                  For instance - calling session_start() after some output has been
                  written. Not having write access to the temporary directory. Incorrect
                  session values in the php.ini file. And several more common errors.

                  The problem is - which is it?

                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  • Joseph S.

                    #10
                    Re: help needed - passing values between two loads of the same page

                    > littlefire wrote:[color=blue][color=green]
                    > >
                    > > The two major causes of session failure:
                    > > 1. The two pages don't reside on the same domain
                    > > 2. One of the two pages does not call session_start() - this needs to be
                    > > done on every single page where a session var is used[/color][/color]
                    [color=blue]
                    > Those are two major causes. I wouldn't say they are THE two major causes.
                    >
                    > For instance - calling session_start() after some output has been
                    > written. Not having write access to the temporary directory. Incorrect
                    > session values in the php.ini file. And several more common errors.
                    >
                    > The problem is - which is it?[/color]

                    In my case it was no.2 above, sesion_start() absent.

                    Also, I discovered that the PHP _interpreter_ goes line by line in
                    displaying as well as processing. So, if you want to see whether a call
                    to the file is the first ever call or a subsequent refresh (or form
                    submittal for that matter), one way to do that is to create a dummy
                    session variable $_SESSION['a']=1 as the last line of the php file, and
                    in the lines above, before your actual processing begins, check whether
                    $_SESSION['a'] is set. If it the first ever call to the page, it will
                    not be set depending on which you can take some actions(keeping fields
                    empty etc.). If it is a second call via a refresh or a form submittal,
                    you will have $_SESSION['a'] set.

                    Little tricks often are helpful.
                    (a.php)
                    <?php
                    /* Keeping the look of the output HTML the same between
                    multiple calls to the same page */
                    session_start() ;
                    ?>
                    <html>
                    <head>
                    <title>Title</title>
                    </head>
                    <body>
                    <form name="taxform" action="a.php" method="POST" >
                    <center>
                    Enter Gross<br/>
                    <input type="text"
                    name="gross"
                    value="<?php if(isset($_SESS ION['a'])){echo $_POST['gross'];}?>"

                    />
                    <br/>
                    <input type="submit"
                    name="Calcbtn"
                    value="Calculat e Tax"
                    />
                    <br/>

                    <?php
                    /* doing the calculation here */
                    $gross = (float)$_POST["gross"];
                    $tax = 0;
                    $rate = 0.3;
                    $tax = $gross*$rate;
                    ?>
                    <br />
                    Tax<br/>
                    <?php echo $_SESSION['a'] ?><br/>
                    <input type="text"
                    name="tax"
                    value="<?php if(isset($_SESS ION['a'])){echo $tax;} ?>"
                    />
                    <br/>
                    </center>
                    </form>
                    </body>
                    </html>
                    <?php $_SESSION['a'] = 1; ?>

                    Regards,
                    Joseph S.

                    Comment

                    • Joseph S.

                      #11
                      Re: help needed - passing values between two loads of the same page

                      Hi,
                      I got a nice workaround for the original problem of multiple forms I
                      had:
                      In many cases, you want multiple submit buttons and not multiple forms
                      really.
                      So here is a sample code that works fine for the muliple submits case:
                      (multisubmits.p hp)
                      <?php
                      session_start() ;
                      ?>
                      <html>
                      <head>
                      <title>Title</title>
                      </head>
                      <body>
                      <center>
                      <form name="taxform" action="multisu bmits.php" method="POST" >
                      Enter Gross<br/>
                      <input type="text"
                      name="gross"
                      value="<?php if(isset($_SESS ION['a'])){echo $_POST['gross'];}?>"

                      />
                      <br/>
                      <table>
                      <tr>
                      <td><input type="submit" name="Tax30" value="30pcTax" /></td>
                      <td><input type="submit" name="Tax40" value="40pcTax" /></td>
                      <td><input type="submit" name="Tax50" value="50pcTax" /></td>
                      </tr>
                      </table>
                      </form>
                      <?php
                      /* doing the calculation here */
                      $gross = (float)$_POST["gross"];
                      $tax = 0;
                      if (isset($_POST['Tax30'])){
                      $tax = $gross * 0.3;
                      $country = "USA";
                      }else if(isset($_POST['Tax40'])){
                      $tax = $gross * 0.4;
                      $country = "United Kingdom";
                      }else if(isset($_POST['Tax50'])){
                      $tax = $gross * 0.5;
                      $country = "France";
                      }
                      ?>
                      <br />
                      Tax<br/>
                      <?php echo $_SESSION['a'] ?><br/>
                      <?php if(isset($_SESS ION['a'])){echo $tax;} ?><br/>
                      Must be <?php echo $country; ?>!<br/>
                      </center>
                      </body>
                      </html>
                      <?php $_SESSION['a'] = 1; ?>

                      That takes care of a lot of things - you can make forms with any
                      arbitrary multiple number of submit buttons and you just have to check
                      which submit was pressed and act accordingly!

                      Also, my original problem of multiple forms is also solved by session
                      variables. Anyways, now I deal with multiple forms like this:

                      <?php
                      session_start() ;
                      ?>
                      <html>
                      <head>
                      <title>Title</title>
                      </head>
                      <body>

                      <center>
                      <form name="taxform" action="multifo rms.php" method="GET" >
                      Enter Gross<br/>
                      <input type="text"
                      name="gross1"
                      value="<?php if(isset($_SESS ION['a'])){echo $_GET['gross1'];}?>"

                      /><br/>
                      <input type="submit" name="Tax30" value="30pcTax" />
                      </form>

                      <form name="taxform" action="multifo rms.php" method="GET" >
                      Enter Gross<br/>
                      <input type="text"
                      name="gross2"
                      value="<?php if(isset($_SESS ION['a'])){echo $_GET['gross2'];}?>"

                      /><br/>
                      <input type="submit" name="Tax40" value="40pcTax" />
                      </form>

                      <form name="taxform" action="multifo rms.php" method="GET" >
                      Enter Gross<br/>
                      <input type="text"
                      name="gross3"
                      value="<?php if(isset($_SESS ION['a'])){echo $_GET['gross3'];}?>"

                      /><br/>
                      <input type="submit" name="Tax50" value="50pcTax" />
                      </form>
                      </center>

                      <?php
                      /* doing the calculation here */
                      $tax = 0;
                      if (isset($_GET['Tax30'])){
                      $gross = (float)$_GET["gross1"];
                      $tax = $gross * 0.3;
                      $country = "USA";
                      }else if(isset($_GET['Tax40'])){
                      $gross = (float)$_GET["gross2"];
                      $tax = $gross * 0.4;
                      $country = "United Kingdom";
                      }else if(isset($_GET['Tax50'])){
                      $gross = (float)$_GET["gross3"];
                      $tax = $gross * 0.5;
                      $country = "France";
                      }
                      ?>
                      <br />
                      Tax<br/>
                      <?php echo $_SESSION['a'] ?><br/>
                      <?php if(isset($_SESS ION['a'])){echo $tax;} ?><br/>
                      Must be <?php echo $country; ?>!<br/>
                      </body>
                      </html>
                      <?php $_SESSION['a'] = 1; ?>

                      Thanks to all,
                      Regards,
                      Joseph S.

                      Comment

                      Working...