Someone please explain what this does?

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

    #1

    Someone please explain what this does?

    I have been looking through this script and came across something that I
    dont quite understand how it functions or is used. Basically its brackets
    that are added on at the end of a form field value.

    EXAMPLE:
    <input name="eid[]" type="hidden" id="eid[]" value="<? echo $ses_id; ?>">

    in other words what does the [] brackets pass to the function thats
    processing the form, basically how is the [] used.

    Also here is the part of the script that is processing it., as well as the
    more coding of the originating form code
    =============== ====
    // UPDATE BASKET QUANTITY
    if (isset($_POST["UpdateChg"])) {

    session_start() ;
    include "functions_cart .php";

    $i = 0;
    $size = count($_POST["eid"]);

    for ($i = 0; $i <= $size-1; $i++) {

    // call remove bad characters function
    $badsymbols = array(" ","-","+","*","/",".");
    $_POST["newquan"][$i] = str_replace($ba dsymbols,"",
    $_POST["newquan"][$i]);

    if (is_numeric($_P OST["newquan"][$i])) {

    // if any quantity's equal 0 then remove from cart
    if ($_POST["newquan"][$i] == 0) {
    unset($_SESSION["cart"][$_POST["eid"][$i]]);
    }

    // update quantity in cart.
    if (array_key_exis ts($_POST["eid"][$i], $_SESSION["cart"])) {

    add_item_to_car t($_POST["eid"][$i], $_POST["newquan"][$i]);

    }

    } // END IF NUMERIC

    }

    header ("location:".$_ SERVER['HTTP_REFERER']);

    } // END BASKET QUANTITY
    =============== ===============
    =============== ============
    MORE OF THE FORM CODE

    <td class="dividing border"><input name="newquan[]" type="text"
    id="newquan[]3" value="<? echo $ses_quan; ?>" size="5" maxlength="4">
    <input name="eid[]" type="hidden" id="eid[]" value="<? echo $ses_id;
    ?>"></td>
    =============== ============





  • J.O. Aho

    #2
    Re: Someone please explain what this does?

    Chris H wrote:[color=blue]
    > I have been looking through this script and came across something that I
    > dont quite understand how it functions or is used. Basically its brackets
    > that are added on at the end of a form field value.
    >
    > EXAMPLE:
    > <input name="eid[]" type="hidden" id="eid[]" value="<? echo $ses_id; ?>">[/color]

    Instead of sending variables you send arrays, this is specially good if you
    have a list where you can select more than one option.

    Arrays can be easier to handle as you can loop through them and preform a task
    on them all, while using single variables, you will need to it "manually" for
    each.

    for($i=0;$i<cou nt($array);$i++ ) {
    $array[$i]=fix($array[$i]);
    }

    vs

    $variable0=fix( $variable0);
    $variable1=fix( $variable1);
    $variable2=fix( $variable2);
    $variable3=fix( $variable3);
    $variable4=fix( $variable4);
    $variable5=fix( $variable5);
    $variable6=fix( $variable6);
    $variable7=fix( $variable7);
    $variable8=fix( $variable8);
    $variable9=fix( $variable9);
    $variable10=fix ($variable10);
    $variable11=fix ($variable11);
    $variable12=fix ($variable12);

    But the code will be easier to follow if you use single variables (if you name
    them properly).


    //Aho

    Comment

    • Michael Trausch

      #3
      Re: Someone please explain what this does?

      Chris H wrote:[color=blue]
      > I have been looking through this script and came across something that I
      > dont quite understand how it functions or is used. Basically its brackets
      > that are added on at the end of a form field value.
      >
      > EXAMPLE:
      > <input name="eid[]" type="hidden" id="eid[]" value="<? echo $ses_id; ?>">
      >
      > in other words what does the [] brackets pass to the function thats
      > processing the form, basically how is the [] used.
      >[/color]

      PHP sees these types of variables upon submission, and counts them as
      variables. You can have twenty items named eid[] and, when the user
      presses the Submit button, PHP gets them in $_POST[] as an array. So,
      in this case, it would be $_POST['eid'][0] through whatever is the
      highest element of the array.

      It makes for things that just aren't possible (well, they are possible,
      but very tedious to do) without using array functionality in HTML forms.

      http://www.webmasterworld.com/forum88/1204.htm is a start, but you can
      find a whole world more of information from Google.

      You can also explicitly name arrays within your HTML code and PHP will
      get them and do the "right thing" with them when the request is sent.
      It's actually quite nice.

      HTH,
      Mike

      Comment

      • Carl Vondrick

        #4
        Re: Someone please explain what this does?

        Chris H wrote:
        [color=blue]
        > =============== ============
        > MORE OF THE FORM CODE
        >
        > <td class="dividing border"><input name="newquan[]" type="text"
        > id="newquan[]3" value="<? echo $ses_quan; ?>" size="5" maxlength="4">
        > <input name="eid[]" type="hidden" id="eid[]" value="<? echo $ses_id;
        > ?>"></td>
        > =============== ============[/color]

        Note that you can also have names like foo[bar] and foo[bar2]. This will
        work the same as foo[]. It is best to think of these as arrays.

        Comment

        Working...