Array doesn't pass.

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

    Array doesn't pass.

    Hello all,

    I have a form with some checkboxes.
    The names of these checkboxes come from an array.
    When i click the submit button the resultcode doesn't recognize the names
    when i want to check wether or not some checkboxes are ticked.

    Assume that i tick checkboxes 100, 150 and 200

    Below is some code i've used.(between the ****** lines)
    *************** *************** *************** ***
    if (!isSet($_POST['submit']))
    {
    ShowForm();
    }
    else
    {
    HandleForm();
    }

    function ShowForm()
    {
    //normally, these values come out of a database
    $question=array ();
    $question[0]="100";
    $question[1]="125";
    $question[2]="150";
    $question[3]="175";
    $question[4]="200";

    echo"<form name='form1' method='post' action='".$_SER VER['PHP_SELF']."'>";

    for($i=0; $i<5; $i++)
    {
    echo "<input type='checkbox' name=$question[$i]This is the
    question with ID ". $question[$i]."<br/>";
    }
    echo "<BR><input type='submit' name='submit' value='Show choice'>";
    echo"</form>";
    }

    function HandleForm()
    {
    for($i=0; $i<5; $i++)
    {
    if(isset($_POST[$question[$i]]))
    {
    echo "Question ".$_POST[$question[$i]]." was
    ticked.<br/>";
    }
    }
    }
    *************** *************** *************** ***********
    Function HandleForm doesn't recognize $_POST[question[$i]]

    Can anyone help me solve this problem.

    T.i.a.
    Regards
    Tino Wintershoven
    The Netherlands



  • Daz

    #2
    Re: Array doesn't pass.

    T. Wintershoven wrote:
    Can anyone help me solve this problem.
    Perhaps you might want consider placing this into the HandleForm
    function:
    echo "<pre>",print_r ($_POST),"</pre>;

    This will allow you to see exactly what has been passed back in the
    $_POST variable, and what format it's in.

    Comment

    • Daz

      #3
      Re: Array doesn't pass.


      T. Wintershoven wrote:
      Can anyone help me solve this problem.
      Another solution might be this:

      if (!isSet($_POST['submit']))
      {
      ShowForm();
      }
      else
      {
      HandleForm();
      }
      // Declare the question array in the global scope.
      $question=array ();
      $question[0]="100";
      $question[1]="125";
      $question[2]="150";
      $question[3]="175";
      $question[4]="200";

      function ShowForm() {
      global $question;

      echo"<form name='form1' method='post'
      action='".$_SER VER['PHP_SELF']."'>";

      for($i=0; $i<5; $i++) {
      // This time we are making a unique name for the checkbox
      // (i.e question_1, question_2 etc...).
      echo "<input type='checkbox' name=$question_ $iThis is the "
      ."question with ID ". $question[$i]."<br/>";
      }
      echo "<BR><input type='submit' name='submit' value='Show
      choice'>";
      echo"</form>";

      }

      function HandleForm() {
      global $question;

      for($i=0; $i<5; $i++) {
      // Create the name for the checkbox we can't to check.
      $question_name = "question_" .$i;
      // This may not be a legit method, but by adding '2' dollar
      signs to the beginning
      // of the variable, the variable name is correctly parsed as it
      should be.
      if(isset($_POST[$$question_name])) {
      echo "Question ".$_POST[$question[$i]]." was "
      ."ticked.<br/>";
      }
      }
      }

      I haven't tested it, but I hope you get the idea.

      Comment

      • Pedro Graca

        #4
        Re: Array doesn't pass.

        Daz wrote:
        echo "<pre>",print_r ($_POST),"</pre>;
        echo "<pre>"; print_r($_POST) ; echo "</pre>";

        Will not print the result value of the print_r() call, which is 1. Or
        you could do

        echo "<pre>",print_r ($_POST, true),"</pre>;

        which will make print_r() return a formatted string with the contents
        of $_POST instead of the 1 above.
        print_r($someth ing, true) *does*not* output anything; the output is
        done by the echo.

        Comment

        • Daz

          #5
          Re: Array doesn't pass.


          Pedro Graca wrote:
          Daz wrote:
          echo "<pre>",print_r ($_POST),"</pre>;
          >
          echo "<pre>"; print_r($_POST) ; echo "</pre>";
          >
          Will not print the result value of the print_r() call, which is 1. Or
          you could do
          >
          echo "<pre>",print_r ($_POST, true),"</pre>;
          >
          which will make print_r() return a formatted string with the contents
          of $_POST instead of the 1 above.
          print_r($someth ing, true) *does*not* output anything; the output is
          done by the echo.
          Hi pedro.

          I'm sorry, but you are incorrect in one of your statements.
          echo "<pre>",print_r ($_POST),"</pre>; will work just fine, I recommend
          you try it as I use it all the time. Please note, my use of commas as
          opposed to periods.

          Best wishes.

          Daz.

          Comment

          • Daz

            #6
            Re: Array doesn't pass.


            Daz wrote:
            Pedro Graca wrote:
            Daz wrote:
            echo "<pre>",print_r ($_POST),"</pre>;
            echo "<pre>"; print_r($_POST) ; echo "</pre>";

            Will not print the result value of the print_r() call, which is 1. Or
            you could do

            echo "<pre>",print_r ($_POST, true),"</pre>;

            which will make print_r() return a formatted string with the contents
            of $_POST instead of the 1 above.
            print_r($someth ing, true) *does*not* output anything; the output is
            done by the echo.
            >
            Hi pedro.
            >
            I'm sorry, but you are incorrect in one of your statements.
            echo "<pre>",print_r ($_POST),"</pre>; will work just fine, I recommend
            you try it as I use it all the time. Please note, my use of commas as
            opposed to periods.
            >
            Best wishes.
            >
            Daz.
            Sorry Pedro. I misread your post.

            Comment

            • Daz

              #7
              Re: Array doesn't pass.


              Daz wrote:
              T. Wintershoven wrote:
              Can anyone help me solve this problem.
              >
              Perhaps you might want consider placing this into the HandleForm
              function:
              echo "<pre>",print_r ($_POST),"</pre>;
              >
              This will allow you to see exactly what has been passed back in the
              $_POST variable, and what format it's in.
              Please note I accidentally missed a double quote from the end of my
              eacho statement. It should have read:

              echo "<pre>",print_r ($_POST),"</pre>";

              Comment

              • Pedro Graca

                #8
                Re: Array doesn't pass.

                Daz wrote:
                echo "<pre>",print_r ($_POST),"</pre>; will work just fine
                Yes, except that it will print an extra "1" (without the quotes).
                The extra "1" is the return value of the print_r() call.

                Comment

                • Kimmo Laine

                  #9
                  Re: Array doesn't pass.

                  "T. Wintershoven" <twintershoven@ casema.nlwrote in message
                  news:s7udnRhqqd 0iMNHYRVnygQ@ca sema.nl...
                  Hello all,
                  >
                  I have a form with some checkboxes.
                  The names of these checkboxes come from an array.
                  When i click the submit button the resultcode doesn't recognize the names
                  when i want to check wether or not some checkboxes are ticked.
                  >
                  Assume that i tick checkboxes 100, 150 and 200
                  >
                  Below is some code i've used.(between the ****** lines)
                  *************** *************** *************** ***
                  if (!isSet($_POST['submit']))
                  {
                  ShowForm();
                  }
                  else
                  {
                  HandleForm();
                  }
                  >
                  function ShowForm()
                  {
                  //normally, these values come out of a database
                  $question=array ();
                  $question[0]="100";
                  $question[1]="125";
                  $question[2]="150";
                  $question[3]="175";
                  $question[4]="200";
                  >
                  echo"<form name='form1' method='post' action='".$_SER VER['PHP_SELF']."'>";
                  >
                  for($i=0; $i<5; $i++)
                  {
                  echo "<input type='checkbox' name=$question[$i]This is the
                  ^^^^^^^^^^^^^
                  "$question[$i]" will print literally 'array[1]', not '100' like you
                  intended. If you want it to work, you gotta denote the array reference with
                  curly braces:

                  echo "<input type='checkbox' name='{$questio n[$i]}'This is the...."

                  Now you'll get '100', '125', etc, which gets us to the second problem:
                  having plain numbers as field names. Well, perhaps it's not a problem, but
                  it is something I wouldn't do. The only explanation I can offer for this is
                  "just because". I'd throw in some alphabets, just in case, name them 'q100',
                  'q125', etc...

                  question with ID ". $question[$i]."<br/>";
                  }
                  echo "<BR><input type='submit' name='submit' value='Show choice'>";
                  echo"</form>";
                  }
                  >
                  function HandleForm()
                  {
                  for($i=0; $i<5; $i++)
                  {
                  if(isset($_POST[$question[$i]]))
                  Here you reference correctly the field names 100, 125, etc.. but the reason
                  they are not found is the fields are actually named array[0]..., like I
                  explained earlier...


                  --
                  "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
                  http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
                  spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


                  Comment

                  Working...