0

I want to keep selected text in my dropdown after submit. This is select inside my form

<form id="form1" method="post" action="">
<input type="text" name='search' id='search' />
 <?php
$db = mysql_connect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbname,$db);
$sql = mysql_query("SELECT * FROM adminklasifier");
while($row = mysql_fetch_array($sql)) {
    $clsfr = $row['klasifier'];
    $sql = mysql_query("SELECT * FROM adminklasifier");
        echo '<select name="cmake" id="cmake" onchange="val();" autofocus width="10">';
        echo '<option value="">-Pilih Domain Klasifikasi-</option>';
        while($row = mysql_fetch_array($sql)) {
            echo '<option ' . ($clsfr==$row['klasifier']) . ' value="'.$row['klasifier'].'">'.$row['klasifier'].'</option>'; 
    }
    echo '</select>';
}
?>
<input type="submit" id='button' name='button'></input>
</form>

i have analyzed other case in Keep values selected after form submission, but I haven't had success yet.

Can you give me a suggestion?

Community
  • 1
  • 1
  • Rather than doing this manually, you may want to look into a templating framework (like Smarty or Moustache) or a full-featured application framework (like Zend Framework, Symphony, Yii or Codeigniter). They will take a lot of the grunt work out of making a PHP application (and will make your code more secure to boot). – dethtron5000 Jul 09 '13 at 19:36
  • 1
    @dethtron5000: Learning a templating framework (or any framework) without first learning the basics will set you back more than doing it the other way around. – Madara's Ghost Jul 09 '13 at 19:40
  • @MadaraUchiha Potentially, yes, but not necessarily. A good framework can also help a motivated engineer learn OO techniques, separation of concerns, architecture, etc. Also a good framework can also help reduce risk of SQL/XSS injections and other security issues. Agree it's no substitute for a basic understanding of the language but I'd rather have someone learn from a framework than launch an insecure/zombied site... – dethtron5000 Jul 09 '13 at 19:51

2 Answers2

1

The posted user response will be in the $_POST array on your page. In your case, the value to the "cmake" form field will be available as $_POST['cmake']. When you iterate through your query result you can simply print "selected" into the appropriate option.

echo '<option ' . ($clsfr==$row['klasifier']) . ' value="'.$row['klasifier'].'"'.(($_POST['cmake'] == $row['klasifier']) ? 'selected=selected' : NULL).'>'.$row['klasifier'].'</option>';
dethtron5000
  • 10,363
  • 1
  • 31
  • 32
0

I always prefer to use a form helper to create my selects

function formSelect($name, $options, $value, $attributes = array()){
     $str =  "<select name='".$name."'";
     foreach($attributes as $k=>$v){
         $str.=" ".$k."='".$v."'";
     }
     $str.= ">";
     foreach($options as $optionLabel=>$optionValue){
           $str.= "<option value='".$optionValue."' ";
           if($value == $optionValue) $str.= "selected='selected'";
           $str.= ">".$optionLabel."</option>";
     }
     $str.= "</select>";
     return $str;
}

Then in your form you can so this.

<form id="form1" method="post" action="">
<input type="text" name='search' id='search' />
 <?php
$db = mysql_connect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbname,$db);

$options = array();
$sql = mysql_query("SELECT * FROM adminklasifier");

while($row = mysql_fetch_array($sql)) {
    $options[$row['klasifier']] = $row['klasifier'];
}

// add the default option
array_unshift($options, "-Pilih Domain Klasifikasi-");
// call the helper
$attributes = array("onchange"=>"val();","autofocus"=>"autofocus","width"=>"10");
echo formSelect("cmake", $options, isset($_POST["search"])?$_POST["search"]:"", $attributes);
?>
<input type="submit" id='button' name='button'></input>
</form>
Orangepill
  • 24,500
  • 3
  • 42
  • 63
  • I don't prefer any line that has `echo ""`, especially in a function – Royal Bg Jul 09 '13 at 20:10
  • @RoyalBg where do you see that? – Orangepill Jul 09 '13 at 20:17
  • in your code, echoing a function, which has assigned html to a variable... totally messed presentation with business logic. – Royal Bg Jul 09 '13 at 20:30
  • @RoyalBg What would your approach be? – Orangepill Jul 09 '13 at 20:32
  • plus the only business logic in my function is the business of building select html – Orangepill Jul 09 '13 at 20:35
  • According to me to have functions that generate html on given parameters is wrong. If you have a template which is managed by your designers, and they want to style your select or whatever the function builds, how would they know how to do it? The template (presentation) should have only loops and if's. Functions/method should return the result of the queries as arrays/objects, so in your view/template/presentation you are foreaching the method, closing the php and build the html – Royal Bg Jul 09 '13 at 20:42
  • Even assuming everything is in one file, there's no reason the query and the connection to be after the form starts, it can be in the beginning – Royal Bg Jul 09 '13 at 20:43
  • I agree that the db access has no business being in the presentation logic but every MVC I have worked with has the concept of a view helper (the implementation above is based off of Zend Frameworks' formSelect view helper). I think it would be messier for designers to try navigating a loop and testing against a post value to be able to restore the selected state of a select element then just using a view helper – Orangepill Jul 09 '13 at 20:55