Hi,
I've got a form containing
<input type=checkbox name="orange" value="true">orange<br>
<input type=checkbox name="apple" value="true">apple<br>

that sends the info to a 2nd php form which contains
$array = array($GET[orange],$GET[apple]);
$x=0;
for ($x=0;$x<2;$x++) {
if ($array[$x]) {
echo $array[$x]."<br>";
}
}

This prints out the word "true" for each input box that is selected, but what i really want is, of course, the name of the fruit, a line of "true"'s doesn't help much.
Is there a "name" kind of a thing i can use here?

Thanks,
Jenny.

"oranges are not the only fruit"

    foreach ($_GET as $G => $V) {
    	echo $G . "<br>"; 
    }
    

    note: the name of your submit button will also appear in this list, so you'll have to write code to extract that...

      Thanks, brilliant, that does the job. Can I do something like
      foreach ($_GET in array[] as $G => $V) so it only extracts the GET parts from that array, thus avoiding having the submit button etc pirint out too?

      Jen.
      in array[]

        no, you can't do that, because the submit button's name is part of the $GET array; however, you can just not name your submit button. the problem with this route is that i have a lot of code that depends on my submit button being named i.e.

        if ($submit) {
            // do stuff
        } else {
           // do other stuff
        }
        

        you could do something like this, if your submit button needs a name:

        foreach ($_GET as $G => $V) {
           if ($G !== "submit") {   // substitute name of your button for "submit"
              echo $G . "<br>"; 
           }
        }
        

          Thanks for the quick replies.
          I was just fiddling about with " != " - not too bad with a few nested if's, the problem is however that I have a set of other text inputs named $feature[$x] where $x varies, so I'm now stuck on
          if ($G != "feature"."") {
          etc.
          where
          can be an integer of any length.
          Bah.
          Jen.

            now you're getting into Regular Expression territory. Regular Expressions are great, but they can be quite daunting at first.

            so, if i understand you correct, then you have text input boxes with names such as:
            feature12
            feature147
            feature589756

            if this is the case, then this code would omit printing those:

            foreach ($_GET as $G => $V) {
            	if ($G !== "submit" && !ereg("^feature([0-9])+$", $G)) {
            		echo $G . "<br>";
            	} 
            }
            

            sorry, i don't really have the time to explain how that works. you will have to read up on that yourself at www.php.net

            however, if you don't have any other fields in your form that start with "feature" except those which you wish to omit , then you could use something such as this:

            foreach ($_GET as $G => $V) {
            	if ($G !== "submit" && substr($G, 0, 7) !== "feature") {
            		echo $G . "<br>";
            	} 
            }
            

              jwalk76 - Wonderful:

              My code is reduced to a neat three lines, works(!), and I've discovered regular expressions.
              Thanks ever so much.
              Jen.

              🙂

                Do not miss the possibility to pass an array from the form.

                If you but it this way:
                <INPUT type="checkbox" name="fruits[]" value="apple">
                <INPUT type="checkbox" name="fruits[]" value="orange">

                then in your script the array $fruits will be available with elements for checked fruits
                e.g. both were checked you'll have $fruits[0] = 'apple' and $fruits[0] = 'orange'.

                  a month later

                  This gets to a problem I am having. I receive an HTML response from a transaction server after validating credit cards. It sends back a full web page. I want to strip out the hidden fields from the code and dump them into an array. here's a taste of what the site returns...

                  <input type="hidden" name="RESULT" value="0">
                  <input type="hidden" name="AUTHCODE" value="010101">
                  <input type="hidden" name="RESPMSG" value="Approved">
                  <input type="hidden" name="AVSDATA" value="NNN">
                  <input type="hidden" name="PNREF" value="V93802H5558">
                  <input type="hidden" name="HOSTCODE" value="">
                  <input type="hidden" name="INVOICE" value="">
                  <input type="hidden" name="AMOUNT" value="100.00">
                  <input type="hidden" name="TAX" value="">

                  basically, i want to make variables out of this html code which match the names and values returned.

                  I'm not sure if i should use regular expressions or not. whatever you think is fastest of course.

                    N. Q.,
                    i got bored at work (don't tell), so i whipped this up. it uses output buffering to grab the returned form fields before they are output to the browser. the buffered output is then placed in a variable where it can be manipulated to your liking such as passing it to a function that extracts the names and values of the form fields:

                    
                    <?
                    
                    /* Start the function that extracts the names and values
                    and places them into the $main[] array for later use */
                    
                    function form2variable($passValue) {
                    
                    // remove quotes from the whole string
                    $cleanupValue = str_replace("\"", "", $passValue);
                    
                    // replace all closing tags with a space so we can explode into an array
                    $cleanupValue = str_replace(">", " ", $cleanupValue);
                    
                    // place whole string into an array
                    $hiddenValues = explode(" ", $cleanupValue);
                    
                    foreach ($hiddenValues as $theValue) {
                    
                    	if (preg_match('/name=(.*)/',$theValue)) {
                    
                    		// make array of form field names
                    		$name[] = substr($theValue, 5);
                    
                    	}
                    	if (preg_match('/value=(.*)/',$theValue)) {
                    
                    		// make array of form field values
                    		$value[] = substr($theValue, 6);
                    
                    	}
                    
                    }
                    
                    $main = array();
                    
                    for ($i = 0; $i < sizeof($name); $i++) {
                    
                    	// make array where keys = names and values = values
                    	$main[$name[$i]] = $value[$i];
                    
                    }
                    
                    foreach ($main as $m=>$n) {
                    
                    	// format and print the array of all names and values
                    	print $m . " = " . $n . "<br>";
                    
                    }
                    
                    }
                    
                    //  buffering form content so it won't be output to browser
                    ob_start();
                    
                    //  where i'm getting the form content from
                    require("get.php");
                    
                    // placing form contents into a variable to call in function
                    $buffer = ob_get_contents();
                    
                     //  end output buffering
                    ob_end_clean();
                    
                    // call form2variable function passing $buffer as a parameter
                    form2variable($buffer);
                    
                    ?>
                    
                    

                    you may have to modify this for your particular application, but you get the point. see the results here http://www.adoobomboot.com/testing/main.php

                      Eeeeexcellllllleeeeent! Much appreciated vB Code! Hats off to you.

                        Write a Reply...