Reload URL with param depending on value of dropdown - php

I would like to reload an URL with special parameters when a selection in a dropdown is made. The actual result is, that the URL changes and I see the URL with the parameter in the browserbar, but the page does not really reload/refresh.
Thats the code where I fetch the sorttype param ... in fact a reload or refresh is not done, because other code is also not processed
// parse url
$url_components = parse_url($compurl);
// get actual url path
$url = ltrim($url_components['path'], "/"); // ltrim($_SERVER['REQUEST_URI'], "/");
// get parameters from url
$sorttype = "";
parse_str($url_components['query'], $params);
$sorttype = $params['sorttype'];
Thats the code where I place the dropdown
<!-- show sortoptions in dropdown-box with redirect on selection change -->
<form action="" method="post" style="float: right;">
<strong>Sortierung:</strong>
<select name="sortmode" onchange="location.href=this.options[this.selectedIndex].value; " style="margin-top:6px;">
<option <?php if (isset($sorttype) && ($sorttype == 'Autor')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Autor' ?>">Autor</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Erscheinungsjahr')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Erscheinungsjahr' ?>">Erscheinungsjahr</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Herausgeber')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Herausgeber' ?>">Herausgeber</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Region')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Region' ?>">Region</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Titel')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Titel' ?>">Titel</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Stadt')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Stadt' ?>">Stadt</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Verlag')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Verlag' ?>">Verlag</option>
</select>
</form>

There were multiple problems but the main is misunderstanding the post and get. When you want to use post, you don't change the url. You only need to reload the page with your variable set in $_POST. I have edited the code for minimal length:
<!DOCTYPE html>
<html lang='en'>
<head>
<?php
print_r($_POST);
#print it if want to see if it was set
$sorttype = $_POST['sorttype']; #assign the var
if (!isset($sorttype)){$sorttype="Not sorted";} #to overcome multiple checks
?>
</head>
<form action="" method="POST" style="float: left;">
<strong>Sortierung:</strong>
<select name="sorttype" style="margin-top:6px;" onchange="this.form.submit()">
<option value="Foo" <?php if (($sorttype == 'Foo')) { echo 'selected'; } ?>>Foo</option>
<option value="Bar" <?php if (isset($sorttype) && ($sorttype == 'Bar')) { echo 'selected'; } ?>>Bar</option>
</select>
</form>
</html>
Select name is the var name for post, selected has to be at the end before closing tag like this: selected>. onchange only needs to submit the selected value this.form.submit()

The following works as requested, no need for form tag.
The first line is for demo, how to use the parameter that is passed.
<h1>Got <?php echo isset($_GET['sorttype'])? $_GET['sorttype'] : 'no value selected yet'; ?></h1>
<select name="sortmode" onchange="location.href=this.options[this.selectedIndex].value; " style="margin-top:6px;">
<option <?php if (isset($sorttype) && ($sorttype == 'Autor')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Autor' ?>">Autor</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Erscheinungsjahr')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Erscheinungsjahr' ?>">Erscheinungsjahr</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Herausgeber')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Herausgeber' ?>">Herausgeber</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Region')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Region' ?>">Region</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Titel')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Titel' ?>">Titel</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Stadt')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Stadt' ?>">Stadt</option>
<option <?php if (isset($sorttype) && ($sorttype == 'Verlag')) { echo 'selected'; } ?> value="<?php echo $url . '?sorttype=Verlag' ?>">Verlag</option>
</select>

Related

Keeping select item selected in php after submit in form

How do i keep my select item selected via PHP? i did it the same way with radiobuttons and it worked. the only thing i changed was changed "checked" to "selected" as was recommended to me.
Here is my PHP:
if (empty($_POST["favFruit"])) {
$favFruitErr = "You must select 1 or more";
}
else {
$favFruit = $_POST["favFruit"];
}
And here is my form:
<select class=favfruitwindow name="favFruit[]" size="4" multiple>
<option <?php if (isset($favFruit) && $favFruit == "apple") echo "selected"; ?> value="apple">Apple</option>
<option <?php if (isset($favFruit) && $favFruit == "banana") echo "selected"; ?> value="banana">Banana</option>
<option <?php if (isset($favFruit) && $favFruit == "plum") echo "selected"; ?> value="plum">Plum</option>
<option <?php if (isset($favFruit) && $favFruit == "pomegranate") echo "selected"; ?> value="pomegranate">Pomegranate</option>
<option <?php if (isset($favFruit) && $favFruit == "strawberry") echo "selected"; ?> value="strawberry">Strawberry</option>
<option <?php if (isset($favFruit) && $favFruit == "watermelon") echo "selected"; ?> value="watermelon">Watermelon</option>
</select>
$favFruit will be an array even when selecting a single option, and null when nothing is selected.
print_r($_POST["favFruit"]); will return Array ( [0] => banana [1] => plum )
You need for see if the item exists in the array and select based on that check.
<select class=favfruitwindow name="favFruit[]" size="4" multiple>
<option <?php if (isset($favFruit) && in_array('apple', $favFruit)) echo "selected"; ?> value="apple">Apple</option>
<option <?php if (isset($favFruit) && in_array('banana', $favFruit)) echo "selected"; ?> value="banana">Banana</option>
<option <?php if (isset($favFruit) && in_array('plum', $favFruit)) echo "selected"; ?> value="plum">Plum</option>
<option <?php if (isset($favFruit) && in_array('pomegranate', $favFruit)) echo "selected"; ?> value="pomegranate">Pomegranate</option>
<option <?php if (isset($favFruit) && in_array('strawberry', $favFruit)) echo "selected"; ?> value="strawberry">Strawberry</option>
<option <?php if (isset($favFruit) && in_array('watermelon', $favFruit)) echo "selected"; ?> value="watermelon">Watermelon</option>
</select>

Echo 'selected' in html5 dropdown form using PHP

I have a form that fetches user info into the $row variable so forms are populated if the row exists if the database, but then saves any new input through the $_POST global variable, in case the user makes a mistake elsewhere on the form. It all works great.
<label for="qualification2">Grade:</label>
<input type="text" name="qualification2" id="grade2" class="form-control"
value="<?php if(isset($_POST['qualification2'])) {
echo $_POST['qualification2'];
} else { echo ($row ['qualification2']); } ?>">
I would like to do similar using a selection dropdown box but the code doesn't seem to work the same with dropdown boxes and I wonder what I'm doing wrong or if this is even possible.
I've also tried echo from the $row variable but that doesn't work either
<?php if($row['qualMonth2'] == 'February') { echo ' selected'; } ?>
This is my code:
<select input type="text" name="qualMonth2" id="qualMonth2" value="Select Month">
<option value="January" <?php if(isset($_POST['qualMonth2']) == 'January') { echo ' selected'; } ?>>January</option>
<option value="February" <?php if(isset($_POST['qualMonth2']) == 'February') { echo ' selected'; } ?>>February</option>
<option value="March" <?php if(isset($_POST['qualMonth2']) == 'March') { echo ' selected'; } ?>>March</option>
<option value="April" <?php if(isset($_POST['qualMonth2']) == 'April') { echo ' selected'; } ?>>April</option>
<option value="May" <?php if(isset($_POST['qualMonth2']) == 'May') { echo ' selected'; } ?>>May</option>
<option value="June" <?php if(isset($_POST['qualMonth2']) == 'June') { echo ' selected'; } ?>>June</option>
<option value="July" <?php if(isset($_POST['qualMonth2']) == 'July') { echo ' selected'; } ?>>July</option>
<option value="August" <?php if(isset($_POST['qualMonth2']) == 'August') { echo ' selected'; } ?>>August</option>
<option value="September" <?php if(isset($_POST['qualMonth2']) == 'September') { echo ' selected'; } ?>>September</option>
<option value="October" <?php if(isset($_POST['qualMonth2']) == 'October') { echo ' selected'; } ?>>October</option>
<option value="November" <?php if(isset($_POST['qualMonth2']) == 'November') { echo ' selected'; } ?>>November</option>
<option value="December" <?php if(isset($_POST['qualMonth2']) == 'December') { echo ' selected'; } ?>>December</option>
</select>
isset validates to true or false based on if variable is set or not. You can't check the value just with isset.
Use a small utility function to check condition:
function checkSelected($value) {
if(isset($_POST['qualMonth2']) && $_POST['qualMonth2'] == $value) {
return true;
} else {
return false;
}
}
Your select element should be then:
<select input type="text" name="qualMonth2" id="qualMonth2" value="Select Month">
<option value="January" <?php echo checkSelected('January') ? 'selected':'' ?>>January</option>
<option value="February" <?php echo checkSelected('February') ? 'selected':'' ?>>February</option>
<option value="March" <?php echo checkSelected('March') ? 'selected':'' ?>>March</option>
<option value="April" <?php echo checkSelected('April') ? 'selected':'' ?>>April</option>
<option value="May" <?php echo checkSelected('May') ? 'selected':'' ?>>May</option>
<option value="June" <?php echo checkSelected('June') ? 'selected':'' ?>>June</option>
<option value="July" <?php echo checkSelected('July') ? 'selected':'' ?>>July</option>
<option value="August" <?php echo checkSelected('August') ? 'selected':'' ?>>August</option>
<option value="September" <?php echo checkSelected('September') ? 'selected':'' ?>>September</option>
<option value="October" <?php echo checkSelected('October') ? 'selected':'' ?>>October</option>
<option value="November" <?php echo checkSelected('November') ? 'selected':'' ?>>November</option>
<option value="December" <?php echo checkSelected('December') ? 'selected':'' ?>>December</option>
</select>
Lets look at what you wrote..
<option value="February" <?php if(isset($_POST['qualMonth2']) == 'February') { echo ' selected'; } ?>>February</option>
What you're saying is.. If there is a value set for qualMonth2 (true / false), compare that against a string..
So you're comparing true || false against a string.
isset($_POST['qualMonth2']) returns true // false.
So none will ever be selected.
Refactor it..
<?php
$month = "";
if(isset($_POST['qualification2'])){
$month = $_POST['qualification2'];
}
?>
Then use $month as the comparative value.
<option value="July" <?php if($month == 'July') { echo ' selected'; } ?>>July</option>
I prefer to perform these checks in Javascript (I prefer JQuery), I think it cleans up the code, but you could also simplify this greatly by using a loop with generated months.
Loop through 1-12 for output
<select input type="text" name="qualMonth2" id="qualMonth2" value="Select Month">
<?php
for($i=1; $i<=12; $i++) {
$dateObj = DateTime::createFromFormat('!m', $i);
$monthName = $dateObj->format('F');
echo "<value>" . $monthName . "</option>";
}
?>
</select>
This approach takes a DRY (Do not repeat) approach. If you need to modify something, you only need to modify 1 line of code versus 12.
Jquery for choosing value (defaults to January if $_POST['qualMonth2'] is not set):
<script>
$('#qualMonth2').val("<?php echo (isset($_POST['qualMonth2'])) ? $_POST['qualMonth2'] : 'January' ?>");
</script>
Take what you will from this answer, I just showed you two possibilities, you can certainly avoid Javascript by adding a check in the loop if you want.

submit from on selcted value when page load

This is my php form i want to submit from on selected value on page load how it's possible. on page load i want to select Architect as a selected value.
<select name="with" <?php if(isset($_REQUEST['market']) && !empty($_REQUEST['market'])) { ?> onChange="if(this.value=='1'){window.location='?market=<?php echo $_REQUEST['market']; ?>&with=1'} else if(this.value=='2') {window.location='?market=<?php echo $_REQUEST['market']; ?>&with=2'} else {window.location='<?php echo $baseUrl."viewMeetings.php?market=".$_REQUEST['market']; ?>'}; <?php } else { ?> onChange=" if(this.value=='1'){window.location='?with=1'} else
if(this.value=='2') {window.location='?with=2'} else {window.location='<?php echo $baseUrl."viewMeetings.php"; ?>'}; <?php } ?>">
<option value="" <?php if(!isset($_REQUEST['with'])) { echo "selected=selected"; } ?>>ALL </option>
<option value="1" <?php if(isset($_REQUEST['with'])) { if($_REQUEST['with']==1) { echo "selected=selected"; } } ?>>Customer</option>
<option value="2" <?php if(isset($_REQUEST['with'])) { if($_REQUEST['with']==2) { echo "selected=selected"; } } ?> >Architect</option>
</select>
Try not using inline javascript. I am not the best at Javascripting but see if this works. Your description is not very clear so hopefully this is what you are looking for! The loading and so forth technically works, but whether it works like you are expecting is another story:
jQuery Libraries
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
Select Button (Add id)
<select name="with" id="with-it">
<option value="" <?php if(!isset($_REQUEST['with'])) { echo "selected=selected"; } ?>>ALL </option>
<option value="1" <?php if(isset($_REQUEST['with'])) { if($_REQUEST['with']==1) { echo "selected=selected"; } } ?>>Customer</option>
<option value="2" <?php if(isset($_REQUEST['with'])) { if($_REQUEST['with']==2) { echo "selected=selected"; } } ?> >Architect</option>
</select>
Script for onChange
<script>
$("#with-it").change(function() {
var ValueSet = $(this).val();
<?php if(isset($_REQUEST['market'])) { ?>
if(ValueSet == '1') {
window.location='?market=<?php echo $_REQUEST['market']; ?>&with='+ValueSet
}
else if(ValueSet == '2') {
window.location='?market=<?php echo $_REQUEST['market']; ?>&with='+ValueSet
}
else {
window.location='<?php echo $baseUrl."viewMeetings.php?market=".$_REQUEST['market']; ?>'}
<?php }
else { ?>
if(ValueSet == '1'){
window.location='?with=1';
}
else if(ValueSet =='2') {
window.location = '?with=2';
}
else {
window.location = '<?php echo $baseUrl."viewMeetings.php"; ?>';
}
<?php } ?>
});
</script>

select box's value after submit

I have simple code
File name:newEmptyPHP.php
<form method="post" action="newEmptyPHP.php">Title:
<select name="title">
<option value="select">Select</option>;
<option value="Dr" selected=<?php if(isset($_POST[ 'title'])=="Dr" ){ echo "selected"; } ?>>Dr</option>';
<option value="Prof">Prof</option>';
<option value="Mr">Mr</option>';
<option value="Ms">Ms</option>';
<option value="Miss">Miss</option>';
<option value="Mrs">Mrs</option>';</select>
<input type="submit" value="submit">
</form>
I want to keep selected particular value which was selected before the form submission.
But i did not get the desirable output.
any help appreciated.
change this
<option value="Dr" selected=<?php if(isset($_POST['title']) == "Dr")
{ echo "selected"; } ?> >Dr</option>';
to
<option value="Dr" <?php if(isset($_POST['title']) && $_POST['title'] == "Dr")
{ echo "selected='selected'"; } ?> >Dr</option>';
<?php if(isset($_POST['title']) == "Dr"){ echo "selected"; } ?>
Change to
<?php if(isset($_POST['title']) && $_POST['title'] == "Dr"){ echo "selected"; } ?>
But better to use JS / jQuery. Example for jQuery:
<?php if(isset($_POST['title']) && $_POST['title'] == "Dr"):?>
<script type="text/javascript">
$(function(){
$('select[name=title]').val('<?php echo html_special_chars($_POST[''title'])?>');
});
</script>
<?php endif?>
<?php
if($_POST['title'] == "Dr")
{ ?>
<option value="Dr" selected>Dr</option>
<?php
}
else
{
?>
<?php
<option value="Dr">Dr</option>
<?php
}
?>
Change
<option value="Dr" selected=<?php if(isset($_POST['title']) == "Dr"){ echo "selected"; } ?> >Dr</option>';
To :
<option value="Dr" <?php if(isset($_POST['title']) && $_POST['title'] == "Dr"){ echo 'selected="selected"'; } ?> >Dr</option>';
1.Remove isset where you are checking it for value, isset will return either true or false
2.Make selected="selected inside if condition
There is a mistake in your code. isset(Something) will return either true or false and will not return Dr you are looking for.
Retry with this
<form method="post" action="newEmptyPHP.php">
Title:<select name="title" >
<option value="" >Select</option>
<option value="Dr" selected=<?php if(isset($_POST['title']) && $_POST['title'] == "Dr"){ echo "selected"; } ?> >Dr</option>';
<option value="Prof" <?php if(isset($_POST['title']) && $_POST['title'] == "Prof"){ echo "selected"; } ?>>Prof</option>';
<option value="Mr" <?php if(isset($_POST['title']) && $_POST['title'] == "Mr"){ echo "selected"; } ?>>Mr</option>';
<option value="Ms" <?php if(isset($_POST['title']) && $_POST['title'] == "Ms"){ echo "selected"; } ?>>Ms</option>';
<option value="Miss" <?php if(isset($_POST['title']) && $_POST['title'] == "Miss"){ echo "selected"; } ?>>Miss</option>';
<option value="Mrs" <?php if(isset($_POST['title']) && $_POST['title'] == "Mrs"){ echo "selected"; } ?>>Mrs</option>';
</select>
<input type="submit" value="submit">
I thin you have static option values.If so the you have to check for every option like as below for one.
<form method="post" action="newEmptyPHP.php">
Title:<select name="title" >
<option value="select" >Select</option>;
<?php
$selected = "";
if(isset($_POST['title']) && $_POST['title'] == "Dr")
{
$selected = 'selected="selected"';
}
?>
<option value="Dr" <?php echo $selected; ?>>Dr</option>
<option value="Prof">Prof</option>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Miss">Miss</option>
<option value="Mrs">Mrs</option>
</select>
<input type="submit" value="submit">
</form>
I would rather prefer this code for the purpose
$option_array = array('select'=>'select','mr'=>'mr'... other values);
<?php
$option_string = '';
foreach($option_array as $key=>$value)
{
if($key == $_POST['title'])
{
$selected = 'selected';
}
else
{
$selected = '';
}
$option_string .= "<option value='$key' $selected>$value</option>";
}
?>
<form method="post" action="newEmptyPHP.php">Title:
<select name="title">
<?php echo $option_string; ?>
</select>
<input type="submit" value="submit">

Post back the data if validation fails for "Select"

Once submitted selected option, the data is not stored.
just want to know how to post back the data if validation fails
The following line doesnt really work for me.
<select id="numbers" name="numbers" value="<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : " "; ?>"/>
if someone could give me a hand?
Many thanks, here is my code
<?php
if(isset($_POST['numbers']) &&($_POST['fruits']) && $_POST['numbers'] != "null" && $_POST['fruits'] !== "null")
{
echo "Thank you!";
} elseif (isset($_POST['numbers']) && $_POST['numbers'] = "null") {
echo "you forgot to choose a number";
}
elseif(isset($_POST['fruits']) && $_POST['fruits'] = "null")
{
echo "you forgot to choose fruit name";
}
?>
<form id="form" name="form" method="post" action="">
<label for="expiry">Select</label>
<select id="numbers" name="numbers" value="<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : " "; ?>"/>
<option value="null" selected="selected">-</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
</select>
<select id="fruits" name="fruits" value="<?php echo (isset($_POST['fruits']))? $_POST['fruits'] : ''; ?>"/>
<option value="null" selected="selected">-</option>
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Pear">Pear</option>
</select>
<input type="submit" value="Send" />
</form>
Solved it, Maybe not a best way, but at least got it sorted:
<?php
$item = null; #
$itemyear = null;
if(isset($_POST['numbers'])){
$item = $_POST['numbers'];
}
if(isset($_POST['fruits'])){
$itemyear = $_POST['fruits'];
}
if(isset($item) && isset($itemyear) && $item != "null" && $itemyear !== "null")
{
echo "Thank you!";
} elseif ($item == "null") {
echo "you forgot to choose a number";
}
elseif($itemyear == "null")
{
echo "you forgot to choose fruit name";
}
?>
<form id="form" name="form" method="post" action="">
<label for="expiry">Select</label>
<select id="numbers" name="numbers" />
<option value="null" selected="selected">-</option>
<option value="01" <?php if($item == '01'): echo "selected='selected'"; endif; ?>>01</option>
<option value="02" <?php if($item == '02'): echo "selected='selected'"; endif; ?>>02</option>
<option value="03" <?php if($item == '03'): echo "selected='selected'"; endif; ?>>03</option>
</select>
<select id="fruits" name="fruits" />
<option value="null" selected="selected">-</option>
<option value="Apple"<?php if($itemyear == 'Apple'): echo "selected='selected'"; endif; ?> >Apple</option>
<option value="Banana"<?php if($itemyear == 'Banana'): echo "selected='selected'"; endif; ?>>Banana</option>
<option value="Pear"<?php if($itemyear == 'Pear'): echo "selected='selected'"; endif; ?>>Pear</option>
</select>
<input type="submit" value="Send" />
</form>
<?php
echo $item ."-". $itemyear;
?>
the PHP isset() function returns either true or false (depending on whether the input is.. well... set.
You would want to use this:
value='<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : ""; ?>
You can't set a value attribute on a <select>. You have to find the correct <option> and set its selected attribute. Personally, I put a data-default attribute on the <select> and then use JavaScript to loop through the options and find the right one.
Oh now I see.
I don't know what the usual thing to do is but one way is to put all the data as a query string when you redirect the user back. The reason why it doesn't stay in the $_POST global is because it's only kept on the page you post to then it's gone.
So when you redirect the user back
if(validationFailed)
{
header("Location: page.php?data=example");
}
The data can then be retrieved in page.php by using
$data = $_GET['data']; // contains "example"

Categories