Passing values between scripts:

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

    Passing values between scripts:

    Hello,

    I wondered if anyone could offer some guidance over my php script. I
    was hoping the example would allow the user to click the submit
    buttons and the item number increment.

    And when the user clicks, the my basket icon the items.php scripts
    starts and the variables from the current script are passed to this.

    I tried to display the variables as the items are incremented, though
    I got no value. Can anyone see the logical error?

    Thank you

    <?php
    global $item1, $item2;

    if(isset($_GET['item1'])){
    $item1++;
    }
    if(isset($_GET['item2'])){
    $item2++;
    }
    ?>

    <head>
    <title>PHP Example</title>
    </head>

    <body>
    <p>
    <table border="0" width="100%">
    <tr>
    <td>
    <a><img src="mybasket.j pg"></a>
    </td>
    </tr>
    </table>
    </p>

    <form method=post>
    <table border="0" width="79%">

    <tr>
    <td>
    <input type="submit" name = "item1" value = "Add item1">
    </td>
    <td>
    <input type="submit" name = "item2" value = "Add item2">
    </td>
    </tr>

    </table>
    </form>

    </body>
    </html>
  • Andy Hassall

    #2
    Re: Passing values between scripts:

    On 12 Jul 2004 13:32:01 -0700, AntoniRyszard@a ol.com (Antoni) wrote:
    [color=blue]
    >I wondered if anyone could offer some guidance over my php script. I
    >was hoping the example would allow the user to click the submit
    >buttons and the item number increment.
    >
    >And when the user clicks, the my basket icon the items.php scripts
    >starts and the variables from the current script are passed to this.
    >
    >I tried to display the variables as the items are incremented, though
    >I got no value. Can anyone see the logical error?
    >
    >Thank you
    >
    ><?php
    >global $item1, $item2;[/color]

    If this is the top level of your script, putting 'global' here is without
    purpose. What did you want it to do?
    [color=blue]
    >if(isset($_G ET['item1'])){
    >$item1++;[/color]

    $item1 is undefined, incrementing sets it to 1.
    [color=blue]
    >}
    >if(isset($_G ET['item2'])){
    >$item2++;[/color]

    $item2 is undefined, incrementing sets it to 1.

    --
    Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
    http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

    Comment

    • Justin Koivisto

      #3
      Re: Passing values between scripts:

      Antoni wrote:[color=blue]
      > I wondered if anyone could offer some guidance over my php script. I
      > was hoping the example would allow the user to click the submit
      > buttons and the item number increment.
      >
      > And when the user clicks, the my basket icon the items.php scripts
      > starts and the variables from the current script are passed to this.
      >
      > I tried to display the variables as the items are incremented, though
      > I got no value. Can anyone see the logical error?[/color]

      try the following mods...
      [color=blue]
      > <?php[/color]

      // removed a line

      session_start() ;
      [color=blue]
      > if(isset($_GET['item1'])){[/color]

      $_SESSION['item1']++;

      [color=blue]
      > }
      > if(isset($_GET['item2'])){[/color]

      $_SESSION{'item 2']++;
      [color=blue]
      > }
      > ?>[/color]

      ....skipped some code

      <form method="get">

      ....If you want to use the post method, then all the $_GET references
      need to be $_POST...

      printf('item1: %d<br>item2: %d',$_SESSION['item1'],$_SESSION['item2']);

      --
      Justin Koivisto - spam@koivi.com
      PHP POSTERS: Please use comp.lang.php for PHP related questions,
      alt.php* groups are not recommended.

      Comment

      • Antoni

        #4
        Re: Passing values between scripts:

        Thank you for replying.

        I changed the code, to include the sessions.

        When the program runs I get a php error,

        PHP Notice: Undefined index: item1 in C:\php\example. php on line 12
        PHP Notice: Undefined index: item2 in C:\php\example. php on line 12

        In this example, I hoping the user could click the submit buttons
        item1 or item2, many times and we keep a counter of each item. Then
        pass these values to the next script.

        In my orginal code I post, I was trying to achieve the program without
        using the sessions. Would it be possible to keep a count of the items,
        just using variables?

        Many thanks

        <?php
        session_start() ;

        if(isset($_GET['item1'])){
        $_SESSION['item1']++;
        }

        if(isset($_GET['item2'])){
        $_SESSION['item2']++;
        }

        printf('item1: %d<br>item2:
        %d',$_SESSION['item1'],$_SESSION['item2']);
        ?>

        <head>
        <title>PHP Example</title>
        </head>

        <body>
        <p>
        <table border="0" width="100%">
        <tr>
        <td>
        <a><img src="mybasket.j pg"></a>
        </td>
        </tr>
        </table>
        </p>

        <form method=post>
        <table border="0" width="79%">

        <tr>
        <td>
        <input type="submit" name = "item1" value = "Add item1">
        </td>
        <td>
        <input type="submit" name = "item2" value = "Add item2">
        </td>
        </tr>

        </table>
        </form>

        </body>
        </html>

        Comment

        • Antoni

          #5
          Re: Passing values between scripts:

          Could anyone advice?

          Thank you

          Comment

          • Justin Koivisto

            #6
            Re: Passing values between scripts:

            Antoni wrote:
            [color=blue]
            > Thank you for replying.
            >
            > I changed the code, to include the sessions.
            >
            > When the program runs I get a php error,
            >
            > PHP Notice: Undefined index: item1 in C:\php\example. php on line 12
            > PHP Notice: Undefined index: item2 in C:\php\example. php on line 12[/color]

            That's because you are trying to print the variables before they have
            been set on the first request
            [color=blue]
            > In this example, I hoping the user could click the submit buttons
            > item1 or item2, many times and we keep a counter of each item. Then
            > pass these values to the next script.
            >
            > In my orginal code I post, I was trying to achieve the program without
            > using the sessions. Would it be possible to keep a count of the items,
            > just using variables?[/color]

            It *can* be done, but sessions are so much easier to do it with.
            Otherwise, you'll have to include the vars as a query string in the
            form's action:

            echo '<form method="post" action="', $_SERVER['PHP_SELF'], '?item1=',
            $_POST['item1'], '&item2=',$_POS T['item2'], '">

            Then when you process the script you'll need to do something like:

            if(isset($_POST['item1']) && isset($_GET['item1']))
            $_POST['item1']+=$_GET['item1']
            }else{
            $_POST['item1']=$_GET['item1'];
            }

            if(isset($_POST['item2']) && isset($_GET['item2']))
            $_POST['item2']+=$_GET['item2']
            }else{
            $_POST['item2']=$_GET['item2'];
            }

            Realize that the code I am giving you here is *all*untested* but in
            theory should work...


            Otherwise, see my notes below about using sessions...
            [color=blue]
            > <?php
            > session_start() ;
            >
            > if(isset($_GET['item1'])){
            > $_SESSION['item1']++;
            > }[/color]

            Change $_GET to $_POST if you use the post method in the form
            [color=blue]
            > if(isset($_GET['item2'])){
            > $_SESSION['item2']++;
            > }[/color]

            same here
            [color=blue]
            > <form method=post>[/color]

            this is the form method I mentioned

            --
            Justin Koivisto - spam@koivi.com
            PHP POSTERS: Please use comp.lang.php for PHP related questions,
            alt.php* groups are not recommended.

            Comment

            • Antoni

              #7
              Re: Passing values between scripts:

              Thank you for replying.

              I decided to use sessions and changed my code to include the post
              function.

              The php message was displayed

              PHP Notice: Undefined index: item1 in C:\php\example. php on line 14
              PHP Notice: Undefined index: item2 in C:\php\example. php on line 14

              Should I not be concerned by this notice?

              The script now also displays the session values 0, but when I clicked
              the submit buttons these numbers did not increment. And just wondered
              if I needed to change some option in the php.ini file?

              Many thanks

              <?php
              session_start() ;

              if(isset($_POST['item1'])){
              $_SESSION['item1']++;
              }

              if(isset($_POST['item2'])){
              $_SESSION['item2']++;
              }
              ?>

              <?php
              printf('item1: %d<br>item2:
              %d',$_SESSION['item1'],$_SESSION['item2']);
              ?>

              <head>
              <title>PHP Example</title>
              </head>

              <body>
              <p>
              <table border="0" width="100%">
              <tr>
              <td>
              <a><img src="mybasket.j pg"></a>
              </td>
              </tr>
              </table>
              </p>

              <form method=post>
              <table border="0" width="79%">

              <tr>
              <td>
              <input type="submit" name = "item1" value = "Add item1">
              </td>
              <td>
              <input type="submit" name = "item2" value = "Add item2">
              </td>
              </tr>

              </table>
              </form>

              </body>
              </html>

              Comment

              • Tim Van Wassenhove

                #8
                Re: Passing values between scripts:

                In article <9359a333.04071 41437.3937210@p osting.google.c om>, Antoni wrote:[color=blue]
                > PHP Notice: Undefined index: item1 in C:\php\example. php on line 14
                > PHP Notice: Undefined index: item2 in C:\php\example. php on line 14[/color]

                The first time you use $_SESSION['item1'].. thus in $_SESSION['item1']++
                $_SESSION['item1'] is not defined. Instead of relying on implicit
                variable initialisation, you could do it explicit

                if (isset($_POST[['item1']))
                {
                if (!isset($_SESSI ON['item1']))
                {
                $_SESSION['item1'] = 0;
                }
                $_SESSION['item1']++;
                }

                --
                Tim Van Wassenhove <http://home.mysth.be/~timvw>

                Comment

                • Antoni

                  #9
                  Re: Passing values between scripts:

                  Hello all,

                  I changed my example to initialise the value to 0. The current error
                  is:

                  PHP Notice: Undefined index: item1 in C:\php\example. php on line 22
                  PHP Notice: Undefined index: item2 in C:\php\example. php on line 22

                  line 22 is: printf('item1: %d<br>item2:
                  %d',$_SESSION['item1'],$_SESSION['item2']);

                  Any thoughts?

                  Thank you

                  <?php
                  session_start() ;

                  if (isset($_POST['item1'])){
                  if(!isset($_SES SION['item1'])){
                  $_SESSION['item1'] = 0;
                  $_SESSION['item1']++;
                  }
                  }

                  if (isset($_POST['item2'])){

                  if(!isset($_SES SION['item2'])){
                  $_SESSION['item2'] = 0;
                  $_SESSION['item2']++;
                  }
                  }
                  ?>

                  <?php
                  printf('item1: %d<br>item2:
                  %d',$_SESSION['item1'],$_SESSION['item2']);
                  ?>

                  <head>
                  <title>PHP Example</title>
                  </head>

                  <body>
                  <p>
                  <table border="0" width="100%">
                  <tr>
                  <td>
                  <a><img src="mybasket.j pg"></a>
                  </td>
                  </tr>
                  </table>
                  </p>

                  <form method=post>
                  <table border="0" width="79%">

                  <tr>
                  <td>
                  <input type="submit" name = "item1" value = "Add item1">
                  </td>
                  <td>
                  <input type="submit" name = "item2" value = "Add item2">
                  </td>
                  </tr>

                  </table>
                  </form>

                  </body>
                  </html>

                  Comment

                  • Tim Van Wassenhove

                    #10
                    Re: Passing values between scripts:

                    In article <9359a333.04071 50217.1a341c3f@ posting.google. com>, Antoni wrote:[color=blue]
                    ><?php
                    > session_start() ;
                    >
                    > if (isset($_POST['item1'])){
                    > if(!isset($_SES SION['item1'])){
                    > $_SESSION['item1'] = 0;
                    > $_SESSION['item1']++;
                    > }
                    > }[/color]

                    $_SESSION['item1] is initialised only when $_POST['item1'] is set.
                    And you only add 1 to $_SESSION['item1'] if it's not set.
                    [color=blue]
                    > printf('item1: %d<br>item2:
                    > %d',$_SESSION['item1'],$_SESSION['item2']);
                    > ?>[/color]

                    Meaby you need to have a look at the control structures in the manual?
                    These things are basic.

                    --
                    Tim Van Wassenhove <http://home.mysth.be/~timvw>

                    Comment

                    • Antoni

                      #11
                      Re: Passing values between scripts:

                      Hello,

                      In this version the error/notice messages have disappeared. Though,
                      when the submit buttons are clicked the item counter display never
                      increases over 0.

                      Could anyone advice how to update the item display?

                      Or perhaps suggest changes to my php.ini file?

                      Thanks

                      <?php
                      session_start() ;

                      if(!isset($_SES SION['item1'])) $_SESSION['item1'] = 0;
                      if(!isset($_SES SION['item2'])) $_SESSION['item2'] = 0;

                      if(isset($_POST['item1'])){
                      $_SESSION['item1']++;
                      }

                      if(isset($_POST['item2'])){
                      $_SESSION['item2']++;
                      }
                      ?>

                      <?php
                      printf('item1: %d<br>item2:
                      %d',$_SESSION['item1'],$_SESSION['item2']);
                      ?>

                      <head>
                      <title>PHP Example</title>
                      </head>

                      <body>
                      <p>
                      <table border="0" width="100%">
                      <tr>
                      <td>
                      <a><img src="mybasket.j pg"></a>
                      </td>
                      </tr>
                      </table>
                      </p>

                      <form method=post>
                      <table border="0" width="79%">

                      <tr>
                      <td>
                      <input type="submit" name = "item1" value = "Add item1">
                      </td>
                      <td>
                      <input type="submit" name = "item2" value = "Add item2">
                      </td>
                      </tr>

                      </table>
                      </form>

                      </body>
                      </html>

                      Comment

                      • Pedro Graca

                        #12
                        Re: Passing values between scripts:

                        Antoni wrote:[color=blue]
                        > Or perhaps suggest changes to my php.ini file?[/color]

                        An exact copy of your script works as expected here.

                        Check session_save_pa th in your php.ini

                        --
                        USENET would be a better place if everybody read: | to email me: use |
                        http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
                        http://www.netmeister.org/news/learn2quote2.html | header, textonly |
                        http://www.expita.com/nomime.html | no attachments. |

                        Comment

                        • Antoni

                          #13
                          Re: Passing values between scripts:

                          Hello,

                          My session file are being stored in windows temp folder.

                          Could anyone see a problem in the following php.ini statements?

                          Thank you

                          [Session]
                          ; Handler used to store/retrieve data.
                          session.save_ha ndler = files

                          ; Argument passed to save_handler. In the case of files, this is the path
                          ; where data files are stored. Note: Windows users have to change this
                          ; variable in order to use PHP's session functions.
                          session.save_pa th = c:\windows\temp

                          ; Whether to use cookies.
                          session.use_coo kies = 1

                          ; This option enables administrators to make their users invulnerable to
                          ; attacks which involve passing session ids in URLs; defaults to 0.
                          ; session.use_onl y_cookies = 1

                          ; Name of the session (used as cookie name).
                          session.name = PHPSESSID

                          ; Initialize session on request startup.
                          session.auto_st art = 0

                          ; Lifetime in seconds of cookie or, if 0, until browser is restarted.
                          session.cookie_ lifetime = 0

                          ; The path for which the cookie is valid.
                          session.cookie_ path = /

                          ; The domain for which the cookie is valid.
                          session.cookie_ domain =

                          ; Handler used to serialize data. php is the standard serializer of PHP.
                          session.seriali ze_handler = php

                          ; Define the probability that the 'garbage collection' process is started
                          ; on every session initialization.
                          ; The probability is calculated by using gc_probability/gc_divisor,
                          ; e.g. 1/100 means there is a 1% chance that the GC process starts
                          ; on each request.

                          session.gc_prob ability = 1
                          session.gc_divi sor = 1000

                          ; After this number of seconds, stored data will be seen as 'garbage' and
                          ; cleaned up by the garbage collection process.
                          session.gc_maxl ifetime = 1440

                          ; PHP 4.2 and less have an undocumented feature/bug that allows you to
                          ; to initialize a session variable in the global scope, albeit register_global s
                          ; is disabled. PHP 4.3 and later will warn you, if this feature is used.
                          ; You can disable the feature and the warning seperately. At this time,
                          ; the warning is only displayed, if bug_compat_42 is enabled.

                          session.bug_com pat_42 = 0
                          session.bug_com pat_warn = 1

                          ; Check HTTP Referer to invalidate externally stored URLs containing ids.
                          ; HTTP_REFERER has to contain this substring for the session to be
                          ; considered as valid.
                          session.referer _check =

                          ; How many bytes to read from the file.
                          session.entropy _length = 0

                          ; Specified here to create the session id.
                          session.entropy _file =

                          ;session.entrop y_length = 16

                          ;session.entrop y_file = /dev/urandom

                          ; Set to {nocache,privat e,public,} to determine HTTP caching aspects.
                          ; or leave this empty to avoid sending anti-caching headers.
                          session.cache_l imiter = private

                          ; Document expires after n minutes.
                          session.cache_e xpire = 180

                          ; trans sid support is disabled by default.
                          ; Use of trans sid may risk your users security.
                          ; Use this option with caution.
                          ; - User may send URL contains active session ID
                          ; to other person via. email/irc/etc.
                          ; - URL that contains active session ID may be stored
                          ; in publically accessible computer.
                          ; - User may access your site with the same session ID
                          ; always using URL stored in browser's history or bookmarks.
                          session.use_tra ns_sid = 0

                          ; The URL rewriter will look for URLs in a defined set of HTML tags.
                          ; form/fieldset are special; if you include them here, the rewriter will
                          ; add a hidden <input> field with the info which is otherwise appended
                          ; to URLs. If you want XHTML conformity, remove the form entry.
                          ; Note that all valid entries require a "=", even if no value follows.
                          url_rewriter.ta gs = "a=href,area=hr ef,frame=src,in put=src,form=fa keentry"

                          Comment

                          • Pedro Graca

                            #14
                            Re: Passing values between scripts:

                            Antoni wrote:[color=blue]
                            > Could anyone see a problem in the following php.ini statements?[/color]
                            (snip)[color=blue]
                            > session.save_pa th = c:\windows\temp[/color]

                            Try these:
                            session.save_pa th = c:/windows/temp
                            session.save_pa th = c:\\windows\\te mp


                            --
                            USENET would be a better place if everybody read: | to email me: use |
                            http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
                            http://www.netmeister.org/news/learn2quote2.html | header, textonly |
                            http://www.expita.com/nomime.html | no attachments. |

                            Comment

                            • Antoni

                              #15
                              Re: Passing values between scripts:

                              Thank you for replying.

                              I will try, though when I looked in the windows\temp folder the
                              session files are being added to the folder.

                              It just seems, after creating the session file, the same file is is
                              not being updated with the new item count.

                              I am using the dzsoft php editor, which comes with an internal server
                              to preview scripts.

                              Thanks you

                              Comment

                              Working...