PHP Beginner question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pepito3@gmail.com

    #1

    PHP Beginner question

    Hi. I have a question about PHP programming philosophy:

    Imagine a web of a shop with a table displaying the name of some
    products. There is a link "Buy" next to each product. I want that link
    to execute some PHP code, but I don't want the navigator to show
    another web.

    So imagine the file is product_list.ph p and the link is: <a
    href="add_produ ct.php?code=... > Buy </a>. I want the code in
    add_product.php to be executed but I want the navigator to display the
    same page it is displaying at the moment (product_list.p hp or
    whatever).

    How can I do that?

    Thanks!

  • David Haynes

    #2
    Re: PHP Beginner question

    pepito3@gmail.c om wrote:[color=blue]
    > Hi. I have a question about PHP programming philosophy:
    >
    > Imagine a web of a shop with a table displaying the name of some
    > products. There is a link "Buy" next to each product. I want that link
    > to execute some PHP code, but I don't want the navigator to show
    > another web.
    >
    > So imagine the file is product_list.ph p and the link is: <a
    > href="add_produ ct.php?code=... > Buy </a>. I want the code in
    > add_product.php to be executed but I want the navigator to display the
    > same page it is displaying at the moment (product_list.p hp or
    > whatever).
    >
    > How can I do that?
    >
    > Thanks!
    >[/color]

    There are a number of ways of doing this, but the most common ones would be:
    1. Have the page call itself and add the php code at the top of the page
    to handle the order stuff.
    2. Have the page call another php file that processes the form data and
    then redirects to the calling form page again.

    The first style has the benefit that all the code is in one place but
    also has the problem that the amount of business logic code may obscure
    the form code and that changing the look and feel of the form may be
    difficult. Various 'tricks' with include() logic may help with this.

    The second style separates the form (i.e. look and feel) from the
    business logic by having a pre-defined set of private data that is
    passed from the form to the business logic (via POST or GET) and from
    the business logic to the form (via SESSION or GET). This separation is
    often called the view (for the form piece) and the controller (for the
    business logic piece). If you add a database or other data repository
    (i.e. the data 'model'), then you are closer to implementing a full
    model-view-controller (MVC2) implementation.

    Both will work quite well. Choose the one that best suits your needs.

    -david-

    Comment

    • Toby Inkster

      #3
      Re: PHP Beginner question

      pepito3 wrote:
      [color=blue]
      > So imagine the file is product_list.ph p and the link is: <a
      > href="add_produ ct.php?code=... > Buy </a>. I want the code in
      > add_product.php to be executed but I want the navigator to display the
      > same page it is displaying at the moment (product_list.p hp or
      > whatever).[/color]

      Google for "AJAX" but be aware that browser support is not 100%. If using
      AJAX, it's a good idea to have the regular old links kick in for those
      browsers that don't support it (or have Javascript disabled), which isn't
      as tricky as it sounds.

      --
      Toby A Inkster BSc (Hons) ARCS
      Contact Me ~ http://tobyinkster.co.uk/contact

      Comment

      • pepito3@gmail.com

        #4
        Re: PHP Beginner question

        > 2. Have the page call another php file that processes the form data and[color=blue]
        > then redirects to the calling form page again.[/color]

        How can I make the called file to acknowledge the calling file?

        i.e.:

        *** product_list.ph p:
        ....
        <a href="add_produ ct.php?code=... > Buy! </a>
        ....

        *** add_product.php :

        <?php
        add_product($_G ET['code']);
        // How to go back to product_list.ph p (or whatever file previous
        to add_product.php )?

        ?php>

        Comment

        • David Haynes

          #5
          Re: PHP Beginner question

          pepito3@gmail.c om wrote:[color=blue][color=green]
          >> 2. Have the page call another php file that processes the form data and
          >> then redirects to the calling form page again.[/color]
          >
          > How can I make the called file to acknowledge the calling file?
          >
          > i.e.:
          >
          > *** product_list.ph p:
          > ...
          > <a href="add_produ ct.php?code=... > Buy! </a>
          > ...
          >
          > *** add_product.php :
          >
          > <?php
          > add_product($_G ET['code']);
          > // How to go back to product_list.ph p (or whatever file previous
          > to add_product.php )?
          >
          > ?php>
          >[/color]

          You would use the header() function of php.

          I use this function I use. The original code was posted here a while
          back by someone else.

          <?php
          /**
          * Module: redirect.inc
          *
          * Manages a redirection when there may be a session active.
          * Also correctly re-addresses URLs that are not absolute.
          *
          * @param string $url
          */
          function redirect($url) {
          session_write_c lose();

          if( substr($url, 0, 4) != 'http' ) {
          if( isset($_SERVER['HTTP_HOST']) ) {
          $url = 'http://'.$_SERVER['HTTP_HOST'].$url;
          } else {
          $url = 'http://localhost'.$url ;
          }
          }

          header("locatio n: $url");
          exit;
          }
          ?>

          So, your code would become:
          <?php
          add_product($_G ET['code']);
          redirect('produ ct_list.php');
          ?>

          If you are going to use the two file approach, then you really need to
          start understanding sessions.

          add_product.php
          <?php
          include('redire ct.inc');

          session_start() ;

          // process the GET values
          if( isset($_GET['code') ) {
          $SESSION['product_codes'][] = $_GET['code'];
          }

          redirect('list_ product.php');
          ?>

          <?php
          session_start() ;
          // read back the product codes
          $product_codes = $_SESSION['product_codes'];

          echo "You have ordered: ".implode(' , ', $product_codes) ; // for example

          ....
          ?>

          Also, it would not hurt to start using a naming convention to help
          associate the view with the controller. I use the following:

          foo.html.php - this is the HTML format view for the code.
          foo.xml.php - this is the XML format view for XSLT processes.
          foo.ctrl.php - this is the controller for all the foo views.

          I also pass the controller URL from the controller to the view via the
          SESSION. That way I always know that <form action="<?php echo
          controller;?>" method="xxx"> will always call the correct controller
          (i.e. the controller that called the view in the first place.)

          -david-


          Comment

          • pepito3@gmail.com

            #6
            Re: PHP Beginner question

            > redirect('list_ product.php');

            The problem is that I won't always call add_product.php from
            list_product.ph p. Moreover, list_product complete url may be:
            ....list_produc ts.php?section= X&page=X&...
            so a static "redirect" won't work fine for my problem...

            Comment

            • David Haynes

              #7
              Re: PHP Beginner question

              pepito3@gmail.c om wrote:[color=blue][color=green]
              >> redirect('list_ product.php');[/color]
              >
              > The problem is that I won't always call add_product.php from
              > list_product.ph p. Moreover, list_product complete url may be:
              > ...list_product s.php?section=X &page=X&...
              > so a static "redirect" won't work fine for my problem...
              >[/color]

              I think you need to read up on how a browser and web server interact
              some more. This is pretty basic stuff but you seem to be missing some
              key concepts based upon the type of questions you are asking.

              The 'list_product.p hp' may be *any* valid url string.
              That includes:
              URLs that are not absolute.
              Different urls based upon code.
              URLS that include get operators.
              URLS that are mod_mapped.

              -david-

              Comment

              • pepito3@gmail.com

                #8
                Re: PHP Beginner question

                >I think you need to read up on how a browser and web server interact[color=blue]
                >some more. This is pretty basic stuff but you seem to be missing some
                >key concepts based upon the type of questions you are asking.[/color]
                [color=blue]
                >The 'list_product.p hp' may be *any* valid url string.
                >That includes:
                >URLs that are not absolute.
                >Different urls based upon code.
                >URLS that include get operators.
                >URLS that are mod_mapped.[/color]

                My question is much simpler (and I understand how a http server works),
                maybe I didn't explain myself very well:

                If I used redirect(X) I would have to store X in $_POST or $_SESSION
                arrays so that add_product.php can get that X. That's something I can't
                do as an action performed when the user clicks on a link. So how can I
                do that?

                Comment

                • Jerry Stuckle

                  #9
                  Re: PHP Beginner question

                  pepito3@gmail.c om wrote:[color=blue][color=green]
                  >>I think you need to read up on how a browser and web server interact
                  >>some more. This is pretty basic stuff but you seem to be missing some
                  >>key concepts based upon the type of questions you are asking.[/color]
                  >
                  >[color=green]
                  >>The 'list_product.p hp' may be *any* valid url string.
                  >>That includes:
                  >>URLs that are not absolute.
                  >>Different urls based upon code.
                  >>URLS that include get operators.
                  >>URLS that are mod_mapped.[/color]
                  >
                  >
                  > My question is much simpler (and I understand how a http server works),
                  > maybe I didn't explain myself very well:
                  >
                  > If I used redirect(X) I would have to store X in $_POST or $_SESSION
                  > arrays so that add_product.php can get that X. That's something I can't
                  > do as an action performed when the user clicks on a link. So how can I
                  > do that?
                  >[/color]

                  You can't just store it in the $_POST variable; a new page will wipe out any
                  values here. But storing them in $_SESSION works fine (as long as you don't
                  change domains).

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

                  Comment

                  • David Haynes

                    #10
                    Re: PHP Beginner question

                    pepito3@gmail.c om wrote:[color=blue][color=green]
                    >> I think you need to read up on how a browser and web server interact
                    >> some more. This is pretty basic stuff but you seem to be missing some
                    >> key concepts based upon the type of questions you are asking.[/color]
                    >[color=green]
                    >> The 'list_product.p hp' may be *any* valid url string.
                    >> That includes:
                    >> URLs that are not absolute.
                    >> Different urls based upon code.
                    >> URLS that include get operators.
                    >> URLS that are mod_mapped.[/color]
                    >
                    > My question is much simpler (and I understand how a http server works),
                    > maybe I didn't explain myself very well:
                    >
                    > If I used redirect(X) I would have to store X in $_POST or $_SESSION
                    > arrays so that add_product.php can get that X. That's something I can't
                    > do as an action performed when the user clicks on a link. So how can I
                    > do that?
                    >[/color]
                    The only way to pass data via a link is to use a GET method. This will
                    pass data from the browser to the web server.

                    Why are you restricted to a link? With some simple css code, you can
                    make a form look like a link and then you would have POST methods
                    available to you.

                    Regardless of whether you use POST or GET from the browser to the
                    server, you will have to use SESSION to send the data to another page.

                    So, if you are going to use GET on the links:

                    [foo1.php]
                    <?php
                    session_start() ;
                    $foo = isset($_SESSION['foo']) ? $_SESSION['foo'] : '';
                    $fred = isset($_SESSION['fred']) ? $_SESSION['fred'] : '';
                    ....
                    // create a dynamic url to the foo2 page based on whether
                    // any of foo or fred is set
                    $url = "foo2.php";

                    // NOTE: if there are a big set of values, this could be
                    // done in a foreach loop.
                    $first = true;
                    if( $foo != '' ) {
                    $url .= '?foo='.$foo;
                    $first = false;
                    }
                    if( $fred != '' ) {
                    if( $first ) $url .= '?fred='.$fred;
                    else $url .= '&fred='.fred ;
                    }
                    ?>
                    ....
                    <a href="<?php echo $url;?>">Link</a>
                    ....

                    [foo2.php]
                    <?php
                    session_start() ;
                    if( isset($_GET['foo']) ) $_SESSION['foo'] = $_GET['foo'];
                    if( isset($_GET['fred'] ) $_SESSION['fred'] = $_GET['fred'];
                    ....
                    session_write_c lose();
                    header("locatio n: foo1.php");
                    ?>

                    If this doesn't answer your issue, perhaps you could restate it with a
                    small example since I am obviously not understanding your problem.

                    -david-

                    Comment

                    • Toby Inkster

                      #11
                      Re: PHP Beginner question

                      David Haynes wrote:
                      [color=blue]
                      > <?php
                      > /**
                      > * Module: redirect.inc
                      > *
                      > * Manages a redirection when there may be a session active.
                      > * Also correctly re-addresses URLs that are not absolute.
                      > *
                      > * @param string $url
                      > */
                      > function redirect($url) {
                      > session_write_c lose();
                      >
                      > if( substr($url, 0, 4) != 'http' ) {
                      > if( isset($_SERVER['HTTP_HOST']) ) {
                      > $url = 'http://'.$_SERVER['HTTP_HOST'].$url;
                      > } else {
                      > $url = 'http://localhost'.$url ;
                      > }
                      > }
                      >
                      > header("locatio n: $url");
                      > exit;
                      > }
                      > ?>[/color]

                      redirect("http_ 1.1_spec.html") ;

                      :-)

                      --
                      Toby A Inkster BSc (Hons) ARCS
                      Contact Me ~ http://tobyinkster.co.uk/contact

                      Comment

                      • David Haynes

                        #12
                        Re: PHP Beginner question

                        Toby Inkster wrote:[color=blue]
                        > David Haynes wrote:
                        >[color=green]
                        >> <?php
                        >> /**
                        >> * Module: redirect.inc
                        >> *
                        >> * Manages a redirection when there may be a session active.
                        >> * Also correctly re-addresses URLs that are not absolute.
                        >> *
                        >> * @param string $url
                        >> */
                        >> function redirect($url) {
                        >> session_write_c lose();
                        >>
                        >> if( substr($url, 0, 4) != 'http' ) {
                        >> if( isset($_SERVER['HTTP_HOST']) ) {
                        >> $url = 'http://'.$_SERVER['HTTP_HOST'].$url;
                        >> } else {
                        >> $url = 'http://localhost'.$url ;
                        >> }
                        >> }
                        >>
                        >> header("locatio n: $url");
                        >> exit;
                        >> }
                        >> ?>[/color]
                        >
                        > redirect("http_ 1.1_spec.html") ;
                        >
                        > :-)[/color]

                        I use a convention that all urls in redirect must either be absolute or
                        start with /, but I see your point. substr($url, 0, 5) != 'http:' would
                        be better.

                        Thanks!
                        -david-

                        Comment

                        • Toby Inkster

                          #13
                          Re: PHP Beginner question

                          David Haynes wrote:
                          [color=blue]
                          > I use a convention that all urls in redirect must either be absolute or
                          > start with /, but I see your point. substr($url, 0, 5) != 'http:' would
                          > be better.[/color]

                          redirect('https ://www.example.com ');

                          :-)

                          Try:

                          !preg_match('/^(ftp|http|http s):/i', $url)

                          --
                          Toby A Inkster BSc (Hons) ARCS
                          Contact Me ~ http://tobyinkster.co.uk/contact

                          Comment

                          • R. Rajesh Jeba Anbiah

                            #14
                            Re: PHP Beginner question

                            pepito3@gmail.c om wrote:[color=blue]
                            > Hi. I have a question about PHP programming philosophy:
                            >
                            > Imagine a web of a shop with a table displaying the name of some
                            > products. There is a link "Buy" next to each product. I want that link
                            > to execute some PHP code, but I don't want the navigator to show
                            > another web.[/color]
                            <snip>

                            1. Ajax
                            2. header('Status: 204 No Content'); aka "no refresh links"

                            --
                            <?php echo 'Just another PHP saint'; ?>
                            Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

                            Comment

                            Working...