开发者

Taking multiple values from checkboxes and passing them to the header on "submit"

开发者 https://www.devze.com 2023-02-11 21:36 出处:网络
This is probably very easy but im also very new to php. Im looking for a way that i can have checkboxes on a form that can be passed via the submit button to a php file that generates a Header locate

This is probably very easy but im also very new to php. Im looking for a way that i can have checkboxes on a form that can be passed via the submit button to a php file that generates a Header locate based on the values.

for example

Apples X Pears Oranges X

would result in the output php file doing开发者_开发问答 a header("Location:url.com/Apples+Oranges");

Im haivng difficulty working out how to get the checkbox values as a string and secondly in doing this above the header causes the header to fail for obvious reasons so I guess i have to use some output buffer?

Thanks!


Basically you just get the post variables and redirect before any other output is made:

<?php 
if(((bool) $_POST)){
    $url = 'http://url.com/';
    $url .= implode('+',$_POST['fruit']);
    $url .= $_POST['fruittext'];
    header("Location: ".$url);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
<body>
<form action="" method="post">
    <input value="Apples"           type="checkbox" name="fruit[]" />
    <input value="Pears"            type="checkbox" name="fruit[]" />
    <input value="Oranges"          type="checkbox" name="fruit[]" />
    <input value="magicmushrooms"   type="checkbox" name="fruit[]" />
    <textarea name="fruittext"></textarea>
    <input type="submit" />
</form>
</body>
</html>


Assuming that there are no other form elements being submitted along with those checkboxes, and the form is being submitted via POST:

 $string = url_encode(implode(',', array_keys($_POST)));
 header("Location: url/$string");

If there are other values and you only want the checkboxes, you'll have to specify which form fields should be treated this way, rather than just grabbing everything.

0

精彩评论

暂无评论...
验证码 换一张
取 消