Giuly
Giuly

Reputation: 3

multiple checkboxes submit url generator php

I have multiple checkboxes in my form and i want to use those to generate the url. An example :

  <form action="checkbox.php" method="post">
  <input type="checkbox" name="checkbox" value="a">
  <input type="checkbox" name="checkbox" value="b">
  <input type="checkbox" name="checkbox" value="c">
  <input type="checkbox" name="checkbox" value="d">
  <br>
  <br>
  <input type="submit" name="Submit" value="Submit">
  </form>

When the user presses the submit button he will have to be redirect to the generated url created as followed :

www.domain.com/checkbox/a+b+c+d

Depending on the choises of the user the url should be generated as above. How can i pull this of?

Upvotes: 0

Views: 1440

Answers (2)

Samy Dindane
Samy Dindane

Reputation: 18706

In your checkbox.php, get all the checked checkboxes, create the url you want, and redirect the user to it with header("Location: $url").

HTML:

<input type="checkbox" name="checkbox[]" value="a" />
<input type="checkbox" name="checkbox[]" value="b" />
<input type="checkbox" name="checkbox[]" value="c" />
<input type="checkbox" name="checkbox[]" value="d" />

checkbox.php:

<?php

if ($_POST) {
  $url = "http://www.domain.com/checkbox/";
  $params = '';
  $checkBoxes = $_POST['checkbox'];
  foreach ($checkBoxes as $value) {
    $params .= $value;
    if ($value != $checkBoxes[count(checkBoxes) - 1])
      $params .= '+';
  }
  $url .= $params;
}

header("Location: $url");

Upvotes: 1

J A
J A

Reputation: 1766

Here's a php example if you don't want to use AJAX

Top of your checkbox.php:

<?php

    if (isset($_POST['checkbox'])){
       $url = 'checkbox/' . implode('+',$_POST['checkbox']);

       header('Location:'. $url);
       die;    
    }


?> 

Upvotes: 0

Related Questions