0
votes

How can i use $_GET to return the values from the URL bar?

Checkboxes are ticked in a form, this is the part that concerns the $_GET variable (I have cut some of it out):

<form action= "<?php echo $_SERVER['PHP_SELF'];?>" method="get">
echo "<table border='1'>";
// Start of the table

while($row = mysql_fetch_array($result))
// The while loop enables each of the stock codes to have
// a separate page within the table, based on the stock code.
{
echo "<tr>";
// Starts the table row.

echo "<td>Stock Code: " . $row['stock_code'] . "
</br> Stock Name: " . $row['stock_name'] . "</td>";
$num = 0;
echo "<td><input type='checkbox' id='select" . $num . "' name='select" . $num . "' value=".$row['stock_code']."></input></td>";
$num = $num+1; }

When I click submit, the stock codes go into the URL bar like this:

submitcheckbox.php?select72=DFC107&select74=DFC120&select79=DFC123

I need it to loop through the $_GET values, check which boxes are set and then update the database if they have been checked with a marker.

I am looking at using a while loop, using isset to check whether the checkboxes have been selected:

$numrows = count($row);
$i=0;
while ($i<=$numrows){
if (isset ($_GET['select.i']));
echo $_GET['select.i'];
$i++;

$save = $_GET['select.i'];
echo $save;

Haven't been very successful so far... wondering if there may be a better to way to do it like using arrays?

5

5 Answers

0
votes

At first - not while not even for but foreach. Then you just do this:

foreach($_GET as $key=>$value) {
   if(substr($key, 0, 6)=="select") {//Just check the begining of the name - fur sure, can be ommited
     echo "Checkbox #".substr($key, 6)." selected!<br>";
   }

}

If you had used while properly (and you didn't) you would iterate through many undefined values - you seem to have over 70 checkboxes! Do you want the program to check them all? You can just check the sent values.
Foreach gives you an associative array key and value for each iteration. And it gives you just values for foreach($array as $value) syntax.

Fix of syntax errors in the question code:

In the second code, you have very obvious begginer syntax errors. I will point out a few, so you can avoid them in the future:

$numrows = count($row);
$i=0;
while ($i<=$numrows){
  if (isset ($_GET['select'.$i]));  { //This must have brackets too, if it involves multiple commands!
    echo $_GET["select$i"];     //This is how we concat strings in php
    $save = $_GET['select'.$i]; //Or this
    echo $save;    
  }
  $i++;          //Iterate at the end
} //Mising ending bracket!!
0
votes

If I understand what you're attempting to do, while you're looping through the number of potential 'selects' (i.e., $_GET['select'.$i]) you could add the $i's to an array with something like this:

if (isset($_GET['select'.$i])) {  
  $my_array[] = $i;  
}

Then you can loop through $my_array and tick off the checkboxes that associate with $i with something like:

foreach($my_array as $checked) {
  // do your checkbox stuff
}
0
votes

Here is an idea on how to make it work

foreach($_GET as $key=>$value) {
 // check if the key is something like select74
 // by finding first occurance of the string on 
 // the key
 $pos = strpos($key, 'select');

 // If string exist
 if ($pos !== false) {
   // Get the save and save it
   echo $value;
   $save = $value;
   echo $save;
 }

}

Note: if instead of using a $_GET you can use a $_POST for your form you just need to change the field name from select74 to select[74] that way the $_POST will have an array call select where the key is 74 and the value DFC120

0
votes

I'm not sure count($row) is what you think it is, can you post the whole page code with that part included, in the order which it is written?

Also it is $_GET['select'.$i] not $_GET['select.i'] :

$numrows = count($row); //make sure where this comes from and that it actually contains the number of rows
for($i=0;$i<$numrows;$i++){
    if(isset($_GET['select'.$i])){
        echo $i.' isset : '.$_GET['select'.$i].'<br/>';
        //do whatever is required to save
    }   
}
-1
votes

You can use array_values($_GET) to get just the values selected from $_GET in a new array. You can then use a foreach loop to iterate these values.

foreach(array_values($_GET) as $selected) {
  // Do things with $selected
}