Add checked checkbox values to url

Hi,

I have a form and need to try to work out how to get the values checked from them into the url as a series of values.

The url on entering the page will look like this -

result.php?Country=1&regions=&category=&star=

But for the page to read the values of the checkboxes, I need it to output to the url so it ends up like when for example 2 checkboxes are selected

result.php?regions%5B%5D=89&regions%5B%5D=221&Country=1

The form is set up like this at the moment without a submit button

<form name="form1" id="myForm"
<?php
$r=mysql_query("SELECT DISTINCT(tbl_resorts.Id_Rsrt), Nom_Rsrt, IdCntry_Rsrt, Id_show, Act_Hot, IdRsrt_Hot FROM tbl_resorts LEFT JOIN tbl_hotels ON (tbl_resorts.Id_Rsrt=tbl_hotels.IdRsrt_Hot) WHERE (tbl_resorts.IdCntry_Rsrt='".$selectCountry."') AND (tbl_hotels.Act_Hot='1') ORDER BY Nom_Rsrt");

while($q=mysql_fetch_assoc($r)){ ?>
<input type="checkbox" name="regions[]" value="<?php echo $q['Id_Rsrt']?>" <?php echo (isset($_REQUEST['regions']) && in_array($q['Id_Rsrt'], $_REQUEST['regions'])) ? 'checked="checked"' : '' ?> class="inline" /><?php echo $q['Nom_Rsrt']?></li>
<?php } ?>
</form>

So basically I need help to be able to submit the values of the selected checkboxes to the url on submit.

This is how I am reading those values to allow the page to display the results from the region selection within the country already selected to get to the page

if($_SERVER['REQUEST_METHOD']=='POST' ) { $post=$_POST;   }
elseif($_SERVER['REQUEST_METHOD']=='GET' ) { $post=$_GET; }
if (!empty($_GET['regions']) ? $_GET['regions'] : null) {
 $regionsArray = array();
 foreach($_GET['regions'] as $regions) {	 
 $regionsArray[] = '\''.$regions.'\'';
}	 
$selectRegion = filter_input(INPUT_GET, 'regions', FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY);
$selectRegion = $selectRegion ? $selectRegion : array(); 	 
$regionData = implode(',', $regionsArray);
$sqlregion =  ' AND IdRsrt_Hot IN ('. $regionData .' )';	 
}

Ok I think I got part of it as below

if ( isset( $_POST['submit'] ) ) {
foreach($_POST['regions'] as $regions){
echo $regions . ' ';
}}

But I lose the country part within the url as below

result.php?regions%5B%5D=354

When I need it to keep the Country value too

result.php?regions%5B%5D=354&Country=1

And Country value is collected as below if that helps

$selectCountry=@$_GET['Country'];

These two statements don’t seem to do anything

if($_SERVER['REQUEST_METHOD']=='POST' ) { $post=$_POST;   }
elseif($_SERVER['REQUEST_METHOD']=='GET' ) { $post=$_GET; }

You assign $_GET to $get (for no good reason) and then continue to use $_GET.

And the statement

if (!empty($_GET['regions']) ? $_GET['regions'] : null) {

seems to be a confusing mixture of an IF statement and the PHP ternary operator. Not at all sure what you’re trying to achieve there.

Stick the country value in a hidden form field to pass it back through to whatever is processing the form.

Hi gandalf458

Thank for posting, and I suppose I’m not sure myself, think I was writing it and it worked so left it all there, but took the first if else out and no effect but with the other are you saying to take the if statement out and leave it as below.

 $regionsArray = array();
 foreach($_GET['regions'] as $regions) {	 
 $regionsArray[] = '\''.$regions.'\'';

I got you drorpsnoot,

So added this to the bottom of the form

<input type="hidden" name="Country" value="<?php echo $_GET['Country'];?>" />

But how do I add it here, sorry

if ( isset( $_POST['submit'] ) ) {
foreach($_POST['regions'] as $regions){
echo $regions . ' ';
}
}

Sorry got it, simple as, well it works anyway

$Country = $Country;

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.