denoteone Posted April 28, 2009 Share Posted April 28, 2009 I want to show the selected month as the drop down default if the form has been submitted. I am getting ".4." in the drop down and the rest of the form is not showing? can anyone see what I am doing wrong? <form action="<?=$PHP_SELF?>" method="post"> Month: <select name="month" id="month"> <?PHP if(isset($_POST["B1"])){ echo '<option value="please pick" > .' . $_POST['month'] . ' .</option>'; }else{ echo '<option value="please pick" >Select</option>'; } ?> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/155972-solved-showing-variable-in-drop-down/ Share on other sites More sharing options...
denoteone Posted April 28, 2009 Author Share Posted April 28, 2009 The most important part of the code is... if(isset($_POST["B1"])){ echo "<option value=\"please pick\" >$_POST['month']</option>"; }else{ echo "<option value=\"please pick\" >Select</option>"; } ?> can I just add a post variable in the middle of the echo like the above example? Quote Link to comment https://forums.phpfreaks.com/topic/155972-solved-showing-variable-in-drop-down/#findComment-821052 Share on other sites More sharing options...
Psycho Posted April 28, 2009 Share Posted April 28, 2009 if(isset($_POST["B1"])){ echo "<option value=\"please pick\" >$_POST['month']</option>"; }else{ echo "<option value=\"please pick\" >Select</option>"; } ?> can I just add a post variable in the middle of the echo like the above example? Arrays that use a quote around the index aren't parsed correctly within double quotes. But, you can wrap the variable within brackets echo "<option value=\"please pick\" >{$_POST['month']}</option>"; I tend to wrap all my variables in brackets when they are included in strings with double quotes. Don't know why you are having the issue described above. Have you check the HTML output source for any clues? Quote Link to comment https://forums.phpfreaks.com/topic/155972-solved-showing-variable-in-drop-down/#findComment-821055 Share on other sites More sharing options...
mtoynbee Posted April 28, 2009 Share Posted April 28, 2009 This is a good solution for what you are doing: <?php echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">"; echo "Month: <select name=\"month\" id=\"month\">"; $topValue = isset($_POST['month'])?intval($_POST['month']):"Select"; echo "<option value=\"please pick\" >".$topValue."</option>"; echo "<option value=\"1\">1</option>"; echo "<option value=\"2\">2</option>"; echo "<option value=\"3\">3</option>"; echo "<option value=\"4\">4</option>"; echo "<option value=\"5\">5</option>"; echo "<option value=\"6\">6</option>"; echo "<option value=\"7\">7</option>"; echo "<option value=\"8\">8</option>"; echo "<option value=\"9\">9</option>"; echo "<option value=\"10\">10</option>"; echo "<option value=\"11\">11</option>"; echo "<option value=\"12\">12</option>"; echo "</select>"; Quote Link to comment https://forums.phpfreaks.com/topic/155972-solved-showing-variable-in-drop-down/#findComment-821056 Share on other sites More sharing options...
premiso Posted April 28, 2009 Share Posted April 28, 2009 I did not read the original topic, but just improving mtoynbee's code a tad <?php echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">"; echo "Month: <select name=\"month\" id=\"month\">"; $topValue = isset($_POST['month'])?intval($_POST['month']):"Select"; echo "<option value=\"please pick\" >".$topValue."</option>"; for ($i=1; $i < 13; $i++) echo "<option value=\"{$i}\">{$i}</option>"; echo "</select>"; Why have all those echos when you can have it easily generated for you Quote Link to comment https://forums.phpfreaks.com/topic/155972-solved-showing-variable-in-drop-down/#findComment-821059 Share on other sites More sharing options...
funkyapache Posted April 28, 2009 Share Posted April 28, 2009 The reason you getting ".4." is because you have two many "." in your string echo '<option value="please pick" > .' . $_POST['month'] . ' .</option>'; try echo '<option value="please pick" > ' . $_POST['month'] . ' </option>'; Quote Link to comment https://forums.phpfreaks.com/topic/155972-solved-showing-variable-in-drop-down/#findComment-821062 Share on other sites More sharing options...
mtoynbee Posted April 28, 2009 Share Posted April 28, 2009 Was trying to keep it as close to the original script as possible. However I would actually use my selectbox function... if (!function_exists('SelectBox')) { function SelectBox($name,$class,$vars,$labels,$js=false,$alphabetise=false,$status=false,$selected_value=false,$dont_title_case=false,$return_as_variable=false) { if ($selected_value) { $selected_value = ereg_replace("\(","\\(",$selected_value); $selected_value = ereg_replace("\)","\\)",$selected_value); $selected_value = ereg_replace("\?","\\?",$selected_value); if (!eregi("\^|",$vars)) { $remove_bars = true; } $vars = "|".$vars."|"; $vars = ereg_replace("\|".$selected_value."\|", "\|".$selected_value."*\|", $vars); if (substr($vars, -1) == "|" && !$remove_bars) { $vars = substr($vars,1,-1); } if (substr($vars, -1) == "|" && $remove_bars) { $vars = substr($vars,0,-1); } $vars = stripslashes($vars); unset($selected_value); if ($remove_bars) { $vars = substr ($vars,1); } } if (eregi("\(Blanks\)",$labels)) { $rearrange_array = true; } $class = explode ("|",$class); $vars = explode ("|",$vars); $labels = explode ("|",$labels); if ($alphabetise) { if ($rearrange_array) { $labels = array_slice($labels, 0, count($labels) - 2); $vars = array_slice($vars, 0, count($vars) - 2); array_multisort($labels, SORT_ASC, SORT_STRING, $vars, SORT_NUMERIC, SORT_DESC); array_push($labels, "(Blanks)", "(NonBlanks)"); array_push($vars, "(Blanks)", "(NonBlanks)"); } else { array_multisort($labels, SORT_ASC, SORT_STRING, $vars, SORT_NUMERIC, SORT_DESC); } } if ($js == "redirect_blank") { $js = "window.open(this.form.$name.options[this.form.$name.selectedIndex].value);"; } if ($js == "redirect") { $js = "location=this.form.$name.options[this.form.$name.selectedIndex].value;"; } if ($js) { if (!eregi("^onfocus",$js) && !eregi("^onblur",$js) && !eregi("^onclick",$js) && !eregi("^onmouseover",$js)) { $js = " onchange=\"$js\""; } } $returned_sb = "<select name=\"$name\" id=\"$name\" class=\"$class[0]\"$js $status>"; for ($i=0;$i<count($labels);$i++) { if ($class[1] && $i > 0) { $class[0] = $class[1]; } if (eregi("\*",$vars[$i])) { $vars[$i] = ereg_replace ("\*", "", $vars[$i]); $labels[$i] = ereg_replace ("\*", "", $labels[$i]); $returned_sb.= "<option class=\"$class[0]\" value=\"".trim($vars[$i])."\" selected=\"selected\">".strtotitle(trim($labels[$i]),$dont_title_case)."</option>"; } else { $returned_sb.= "<option class=\"$class[0]\" value=\"".trim($vars[$i])."\">".strtotitle(trim($labels[$i]),$dont_title_case)."</option>"; } } $returned_sb.= "</select>"; if ($return_as_variable == true) { return $returned_sb; } else { echo $returned_sb; } } } Quote Link to comment https://forums.phpfreaks.com/topic/155972-solved-showing-variable-in-drop-down/#findComment-821064 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.