Jump to content

Get values of multiple input checkboxes


SamC
 Share

Recommended Posts

I've been trying to expand my knowledge with PHP and do something a bit more complex than the usual static sites. I created a bunch of pages 'building-entry' with title, town, etc... but just concentrating on the town at the moment. The option values (checkboxes here) are populated by the town names of pages that exist in the system, I haven't predefined these anywhere.

Got this so far:

// search-form.php
<?php
    // get all buildings
    $buildings = $pages->find("template=building-entry");
    $towns = [];

    //get unique towns into array
    foreach ($buildings as $building) {
  
        if(!in_array($building->addressBuildingTown, $towns)) {
            $towns[] = $building->addressBuildingTown;
        }

        $uniqueTowns = $towns;
        sort($uniqueTowns);
    }
?>

<form id='building_search' method='get' action='<?php echo $config->urls->root?>search/'>

    <h3>Building search</h3>

    <p>
        <label for='search_town'>Town</label><br>
            <?php
                foreach ($uniqueTowns as $town) {
                    $optionValue = str_replace(' ', '-', strtolower($town));
                    echo "<input type='checkbox' name='town[]' value='{$optionValue}'>{$town}<br>";
                }
            ?>
    </p>

    <p><input type='submit' id='search_submit' name='submit' value='Search' /></p>

</form>

...which goes to:

// search.php
// yet to add sanitizer
<?php
    $towns = $input->get->town;
    print_r($towns);
?>

So when I select a couple of values and submit the form, on search.php it outputs:

Array ( [0] => town-1 [1] => town-2 )

So it appears I am getting the right values. The URL is a bit funky though, I end up with:

mydemo.com/search/?town%5B%5D=town-1&town%5B%5D=town-2=&submit=Search


Is this the right way to go about this? Is there a way to get a more friendly URL? I'm going to use these values to create a selector to list all buildings in a town(s).

I was also looking at the search form in skyscrapers which after a search has a URL like:

http://demo.processwire.com/search/?keywords=&city=austin&height=&floors=&year=&submit=1

// note &submit=1

Why does mine (which has the same line of code as skyscapers):

<p><input type='submit' id='search_submit' name='submit' value='Search' /></p>

..end in

&submit=Search

I would have though mine would be the same? Thanks for any advice on the above.

Link to comment
Share on other sites

3 hours ago, SamC said:

 

I was also looking at the search form in skyscrapers which after a search has a URL like:


http://demo.processwire.com/search/?keywords=&city=austin&height=&floors=&year=&submit=1

// note &submit=1

Why does mine (which has the same line of code as skyscapers):


<p><input type='submit' id='search_submit' name='submit' value='Search' /></p>

..end in


&submit=Search

I would have though mine would be the same? Thanks for any advice on the above.

The name is returned with the associated value assigned, as so:

pwtopic.png.17c89dd435b326b977933f9a2d9dc431.png

Change it to:

name='submit' value='1'

 

  • Like 1
Link to comment
Share on other sites

@rick thanks but I tried that earlier already but it changed the text on the button to '1'. I still can't explain why skyscrapers code:

<p><input type='submit' id='search_submit' name='submit' value='Search' /></p>

..results in a URL with &submit=1 on the end?! Unless I'm looking at old code, the site is the demo one online, not locally installed. Got the files from here, this is the search form:

https://github.com/ryancramerdesign/SkyscrapersProfile/blob/master/site/templates/includes/search_form.php

 

--edit--

Just had a thought, my contact form is a button rather than input:

<button type="submit" name="sendMe" value="1">SEND MESSAGE</button>

Maybe that would serve me better. Or maybe it doesn't matter because I'm the only one that'll ever look at the URL and say "I wish that said 1 instead of submit"...

Link to comment
Share on other sites

Your post variable for the button is sent as, sendMe=1, whereas the input field is sent as, submit=Search. You can test the post variables and retrieve the values in order to perform whatever logic you require: IF sendMe == 1 THEN ..., or IF submit == 'Search' THEN ...

I don't have access to the skyscrapers code at the moment, so I can't comment on the differences you are seeing. However, in general, all completed fields; hidden, input, options, arrays, whatever, have their field names and values posted back to the script defined in the action attribute of that form.

The field names can be whatever name you want (allowed characters) and the values can be whatever you want. So my previous post was slightly incorrect, in that you don't need to 'change' the value (you can if you want), you only need to test that field's name for the value you expect then perform whatever task you want.

 

  • Like 1
Link to comment
Share on other sites

1 minute ago, rick said:

Your post variable for the button is sent as, sendMe=1, whereas the input field is sent as, submit=Search. You can test the post variables and retrieve the values in order to perform whatever logic you require: IF sendMe == 1 THEN ..., or IF submit == 'Search' THEN ...

I don't have access to the skyscrapers code at the moment, so I can't comment on the differences you are seeing. However, in general, all completed fields; hidden, input, options, arrays, whatever, have their field names and values posted back to the script defined in the action attribute of that form.

The field names can be whatever name you want (allowed characters) and the values can be whatever you want. So my previous post was slightly incorrect, in that you don't need to 'change' the value (you can if you want), you only need to test that field's name for the value you expect then perform whatever task you want.

 

Thanks @rick I get what you meant. I use if sendMe=1 for validation on submission of my contact form. The input submit code in this post is a different site where I'm filtering buildings on a page (after form submit). I literally just got into manually creating forms a week or so ago (I used to use a Drupal module for this). I took that for granted, but I've learned so much in the past two days building this small test site, it's been well worth it. Used str_replace, rtrim, strtolower, in_array, learned about creating/adding to/looping php arrays, differences between post and get, getting input after form submissions, building PW selectors from dynamic data. Been a great couple of days! :)

Finally graduating to more dynamic things now.

  • Like 1
Link to comment
Share on other sites

It's fun ain't it :)

I had always used sites like w3schools and php.net as references, and concepts are explained simply. They have great examples and you can most always find answers to your questions.

Of course, ProcessWire specific questions will be answered here. It's a great staff and there are many knowledgeable people willing to help.

So ask away!

Link to comment
Share on other sites

10 hours ago, Soma said:

If I look at the code on Skyscraper Demo http://demo.processwire.com I see: 


<button type="submit" id="search_submit" class="uk-button uk-button-primary" name="submit" value="1"><i class="uk-icon-search"></i>
Search</button>

 

I didn't think of looking at the source on the actual page itself, I only referenced the file I downloaded.

sad-but-true-i-failed-the-stupid-test-4ibFug-clipart.jpg

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...