Forms in Php

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

    Forms in Php

    Is it possible to use the GET method with checkboxes in php?? I have a
    form that does a get but I get the following error.

    Warning: Invalid argument supplied for foreach() in


    If I change the method to a POST it works fine..

    Thanks

  • scott Johnson

    #2
    Re: Forms in Php

    What snippet of code are you using for the foreach?
    foreach($_GET as $key=>$value){} should work.

    Is there a particular reason you are using GET and not POST for the form?

    onefastmustang wrote:[color=blue]
    > Is it possible to use the GET method with checkboxes in php?? I have a
    > form that does a get but I get the following error.
    >
    > Warning: Invalid argument supplied for foreach() in
    >
    >
    > If I change the method to a POST it works fine..
    >
    > Thanks
    >[/color]

    --
    Scott Johnson

    Comment

    • onefastmustang

      #3
      Re: Forms in Php

      I am using that exact syntax.. I misspoke before.. POST does not work
      either.. The reason I was using GET is that the client wants to be
      able to use the back button on the browser without getting the PAGE
      EXPIRED page..

      my form looks like this..
      <input type="checkbox" value= "1" name="roles[]" >
      <input type="checkbox" value= "2" name="roles[]" >

      The reciveing code looks like this..


      $roles = $_GET[roles]. $_POST[roles];


      if ($roles) {
      $sqls = $sqls. " and profiles.role IN (";
      foreach ($roles as $key => $value){
      $sqls = $sqls. "$value,";
      }
      $sqls = substr("$sqls", 0 , -1);
      $sqls .= ")";
      }

      Comment

      • Oli Filth

        #4
        Re: Forms in Php

        onefastmustang said the following on 17/10/2005 18:22:[color=blue]
        > I am using that exact syntax.. I misspoke before.. POST does not work
        > either.. The reason I was using GET is that the client wants to be
        > able to use the back button on the browser without getting the PAGE
        > EXPIRED page..
        >
        > my form looks like this..
        > <input type="checkbox" value= "1" name="roles[]" >
        > <input type="checkbox" value= "2" name="roles[]" >
        >
        > The reciveing code looks like this..
        >
        >
        > $roles = $_GET[roles]. $_POST[roles];[/color]
        ^ ^
        ^ ^
        Missing quotes here?
        Also, you can't concatenate arrays like this. Try array_merge() instead.
        [color=blue]
        >
        >
        > if ($roles) {
        > $sqls = $sqls. " and profiles.role IN (";
        > foreach ($roles as $key => $value){
        > $sqls = $sqls. "$value,";
        > }
        > $sqls = substr("$sqls", 0 , -1);
        > $sqls .= ")";
        > }
        >[/color]


        --
        Oli

        Comment

        • Siv Hansen

          #5
          Re: Forms in Php

          onefastmustang wrote:[color=blue]
          > I am using that exact syntax.. I misspoke before.. POST does not work
          > either.. The reason I was using GET is that the client wants to be
          > able to use the back button on the browser without getting the PAGE
          > EXPIRED page..
          >
          > my form looks like this..
          > <input type="checkbox" value= "1" name="roles[]" >
          > <input type="checkbox" value= "2" name="roles[]" >
          >
          > The reciveing code looks like this..
          >
          >
          > $roles = $_GET[roles]. $_POST[roles];[/color]

          Do you use both get and post at the same time? If you want to use a
          method that doesn't care wether the post or get method is used try request..

          $roles = $_REQUEST['roles'];

          [color=blue]
          >
          >
          > if ($roles) {
          > $sqls = $sqls. " and profiles.role IN (";
          > foreach ($roles as $key => $value){
          > $sqls = $sqls. "$value,";
          > }
          > $sqls = substr("$sqls", 0 , -1);
          > $sqls .= ")";
          > }
          >[/color]

          Your error message is for the loop that goes through the query to the
          database. I can't see why you try to loop the way you do. Could you try
          to explain what you're trying to retreive in that query?

          Comment

          • Jerry Stuckle

            #6
            Re: Forms in Php

            onefastmustang wrote:[color=blue]
            > I am using that exact syntax.. I misspoke before.. POST does not work
            > either.. The reason I was using GET is that the client wants to be
            > able to use the back button on the browser without getting the PAGE
            > EXPIRED page..
            >
            > my form looks like this..
            > <input type="checkbox" value= "1" name="roles[]" >
            > <input type="checkbox" value= "2" name="roles[]" >
            >
            > The reciveing code looks like this..
            >
            >
            > $roles = $_GET[roles]. $_POST[roles];
            >
            >
            > if ($roles) {
            > $sqls = $sqls. " and profiles.role IN (";
            > foreach ($roles as $key => $value){
            > $sqls = $sqls. "$value,";
            > }
            > $sqls = substr("$sqls", 0 , -1);
            > $sqls .= ")";
            > }
            >[/color]

            Well, $roles won't be an array at this time - you did a string
            concatenation on it.

            If you're using GET method, use $_GET. If you're using $_POST, use
            $_POST. You can also use $_REQUEST to handle either - but personally I
            don't like that as well. But that's just my personal preference.

            Also, if neither checkbox is checked, $roles will be empty.

            A couple of changes:

            $roles = isset($_GET['roles']) ? $_GET($roles) : null; // Note quotes!

            if (isset($roles) { // Has one been set?
            if (isarray($roles ) { // Ensure it's an array
            $sqls = $sqls. " and profiles.role IN (";
            foreach ($roles as $key => $value){
            $sqls = $sqls. "$value,";
            }
            $sqls = substr("$sqls", 0 , -1);
            $sqls .= ")";
            }
            else { // Set but not an array
            $sqls = $sqls . " and profiles.role = " . $roles . ")"
            }
            }

            (note - not checked for syntax)
            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            Working...