claro Posted January 17, 2013 Share Posted January 17, 2013 (edited) I've been working this code like in 4 hours just to look for the error. My problem is I can't get the selected values. It refresh and display the selected 'Select One' in options. echo "<label for='device'> Select Device </label>"; echo "<select name='device_name' onchange=\"reload(this.form)\"><option value=''>Select one</option>"; //echo "<option value=''>Select one</option>"; if (is_array($list)) { foreach ($list as $dname) { $dev_code = $dname[0]; $dev_name = $dname[1]; if ($dev_code == @$dev) { echo "<option selected value ='$dev_code'>$dev_name</option>"; } else { echo "<option value='$dev_code'>$dev_name</option>"; } } } echo "</select><br/>"; Any help is greatly appreciated. Thank you. Edited January 17, 2013 by claro Quote Link to comment https://forums.phpfreaks.com/topic/273270-selected-value-in-php-option/ Share on other sites More sharing options...
requinix Posted January 17, 2013 Share Posted January 17, 2013 And where do you define $dev? Quote Link to comment https://forums.phpfreaks.com/topic/273270-selected-value-in-php-option/#findComment-1406375 Share on other sites More sharing options...
claro Posted January 17, 2013 Author Share Posted January 17, 2013 echo "<label for='device'> Select Device </label>"; @$dev = $_GET['dev']; // Use this line or below line if register_global is off if(strlen($dev) > 0 and !ctype_alpha($dev)){ // to check if $cat is numeric data or not. echo "Data Error"; exit; } $dev is responding, I put it above my code. Quote Link to comment https://forums.phpfreaks.com/topic/273270-selected-value-in-php-option/#findComment-1406381 Share on other sites More sharing options...
Muddy_Funster Posted January 17, 2013 Share Posted January 17, 2013 why are you constantly supressing the lines that have $dev on them? Quote Link to comment https://forums.phpfreaks.com/topic/273270-selected-value-in-php-option/#findComment-1406384 Share on other sites More sharing options...
titan21 Posted January 17, 2013 Share Posted January 17, 2013 The at symbol (@) before $dev will suppress errors so remove that and see what errors it will generate as this may help you identify what the issue is. Don't think error suppression should really be used with variables IMHO. Quote Link to comment https://forums.phpfreaks.com/topic/273270-selected-value-in-php-option/#findComment-1406385 Share on other sites More sharing options...
claro Posted January 17, 2013 Author Share Posted January 17, 2013 Because my form's action is below, there is an error $_GET['dev'] when not set. But I removed it already but still not working. Quote Link to comment https://forums.phpfreaks.com/topic/273270-selected-value-in-php-option/#findComment-1406387 Share on other sites More sharing options...
Muddy_Funster Posted January 17, 2013 Share Posted January 17, 2013 Ok, is there an error or is there a warning (or perhaps a notice)? These are different things. Rather than suppression you should properly code to catch issues like that by checking the if(!isset(...)). Using !isset() will not throw an error/warning/notice if the variable has not been declared. To be fair, you really shouldn't touch error supression ar all unless you are already managing errrors on your own with custom code. We will need you to post up all the relevent code, including the form, and also a source view of the page once it's rendered in the browser. What you have in the OP is not enough for us to work with. Quote Link to comment https://forums.phpfreaks.com/topic/273270-selected-value-in-php-option/#findComment-1406391 Share on other sites More sharing options...
claro Posted January 17, 2013 Author Share Posted January 17, 2013 Ok, I hope this helps. <SCRIPT language="javascript"> function reload(form){ var val=form.device_name.options[form.device_name.options.selectedIndex].value; self.location='assign_sn.php?dev=' + val ; } </SCRIPT> </head> <body> <form method='post' action='assign_sn_action.php'> <?php $dev = $_GET['dev']; // Use this line or below line if register_global is off if(strlen($dev) > 0 and !ctype_alpha($dev)){ // to check if $cat is numeric data or not. echo "Data Error"; exit; } $device = new DevType(); $list = $device ->viewList(); echo "<label for='device'> Select Device </label>"; echo "<select name='device_name' onchange=\"reload(this.form)\"><option value=''>Select one</option>"; if (is_array($list)) { foreach ($list as $dname) { $code = $dname[0]; $name = $dname[1]; if ($code == $dev) { echo "<option selected value ='$code'>$name</option>"; } else { echo "<option value='$code'>$name</option>"; } } } echo "</select><br/>"; ?> <input type='submit' value='submit'> </form> Quote Link to comment https://forums.phpfreaks.com/topic/273270-selected-value-in-php-option/#findComment-1406393 Share on other sites More sharing options...
Muddy_Funster Posted January 17, 2013 Share Posted January 17, 2013 could you var_dump($list) and var_dump($dname) please? Quote Link to comment https://forums.phpfreaks.com/topic/273270-selected-value-in-php-option/#findComment-1406396 Share on other sites More sharing options...
claro Posted January 17, 2013 Author Share Posted January 17, 2013 I think I found the error now. But I don't know how did it happen. array(3) { [0]=> array(2) { [0]=> string(10) "HDD " [1]=> string( "Harddisk" } [1]=> array(2) { [0]=> string(6) "MBOARD" [1]=> string(11) "Motherboard" } [2]=> array(2) { [0]=> string(10) "MTOR " [1]=> string(7) "Monitor" } } The string count of my 'HDD' and 'MTOR'. Quote Link to comment https://forums.phpfreaks.com/topic/273270-selected-value-in-php-option/#findComment-1406397 Share on other sites More sharing options...
Muddy_Funster Posted January 17, 2013 Share Posted January 17, 2013 it has been padded with white space somwhere along the line. try using the trim() function to either clean up the database content or the variables Quote Link to comment https://forums.phpfreaks.com/topic/273270-selected-value-in-php-option/#findComment-1406401 Share on other sites More sharing options...
claro Posted January 17, 2013 Author Share Posted January 17, 2013 Tired eyes. Thank you thank you! Quote Link to comment https://forums.phpfreaks.com/topic/273270-selected-value-in-php-option/#findComment-1406404 Share on other sites More sharing options...
Muddy_Funster Posted January 17, 2013 Share Posted January 17, 2013 You're welcome. Quote Link to comment https://forums.phpfreaks.com/topic/273270-selected-value-in-php-option/#findComment-1406407 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.