php - to redirect a page according to the selected option value -
i want redirect page according selected option value if select 1 option tag redirect google.com page else selected option redirect on gmail.com page.i write these code redirect same value if select option solution these.
<?php $package = isset($_get['package_select']); if ($package == 'one') { header("location: http://google.com"); } else if ($package == 'two') { header("location: http://youtube.com"); } ?> <html> <body> <form action="#" method="get" id="packageform" > <select name="package_select"> <option value="">select package</option> <option value="one">one listing</option> <option value="two">two listings</option> </select> <input id="submitbutton" type="submit" value="submit" /> </form> </body> </html>
isset
return true
or false
depending on whether or not given variable set. when this:
$package = isset($_get['package_select']);
$package
either true
or false
.
so when compare if ($package == 'one')
or else if ($package == 'two')
, equalities never achieved correctly. if either one
or two
selected, $package == 'one'
evaluate true, because true
equal both 'one'
, 'two'
when compared using ==
, , you'll first redirect.
instead, can use built-in filter function assign value of $_get['package_select']
$package
, or null
value if $_get['package_select']
not set. (this case if form had not been submitted. if empty "select package" option selected, isset($_get['package_select'])
still return true
.)
$package = filter_input(input_get, 'package_select');
then if
/elseif
work properly, because $package
can contain either one
or two
instead of bool.
Comments
Post a Comment