last day (15 days later) » 

10:42 PM
1
Q: PHP: selected not being written out to HTML

pee2peeI have the following <?php echo $d[17];?> <select class="form-control" name="text_06" id="text_06_<?=$this->instanceID?>" class="form-control"> <option value="--" <?php echo ($d[17]=='--' ? 'selected':'');?>>-</option> <option value="Full" <?php echo ($d[17] == 'Full' ? 'selected':'');?>>...

 
Could you check the output of var_dump($d[17]); please?
 
He said the output was "Full"
 
@iArcadia In a browser that could have a lot more to it. <span>, spaces, etc. var_dump gives the string length. If it isn't 4 that explains it.
 
Added the var_dump
When I view source or inspect the element, the "selected" isn't present which would (I think) discount JS deselecting it (but thanks for that site - a really useful resource)
I did yeah, did a Ctrl+U to view the actual source, refreshed and amended text to make sure I was getting latest version and prepending the URL in Chrome with "view-source:"
 
@user3783243 IE does have an inspect
 
10:42 PM
Tried in IE 11 and same happens
 
@pee2pee ideone.com/5WmpL0 - the code as we can see it is absolutely fine. The only thing I can think of is there's some magic __get method being called by <?=$this->instanceID?> which is altering the value of (a global/reference) $d.
@pee2pee Or as user3783243 suggests, your var_dump is after/before the value has changed.
 
I did var_dump before and after and it brings back the same value. I have amended the £d variable to something tragically unique as well.
 
@pee2pee And just to be absolutely certain, there's no code you've ommitted in your example above between your var_dump()s and what we can see?
 
Have added screenshots
 
@pee2pee Look for function __get in the file you're in, or in the files for any classes that your class is extending from.
 
10:42 PM
Using a heavily modified 3rd party codeigniter setup so can't access those functions - will have to raise a request with the company.
 
@pee2pee Actually never mind. Your second var_dump would fail if that was the cause.
 
var_dump($d[17] == 'Full') is true?
 
@pee2pee OK, seems silly we didn't ask this sooner. Can you put error_reporting(E_ALL); ini_set('display_errors', 1); at the top of your script?
 
It does return "bool(true)" and unfortunately the vendor won't let us add those lines to the scripts as permission is denied
 
Maybe they block ternary as well? Seems strange but this behavior also is strange. What about if you swap the full to this format 3v4l.org/GL0KL
 
10:42 PM
can you try using strict comparison (===), var_dump($d[17] === 'Full') maybe this will show something else
 
The code we can see looks absolutely fine. The only "suggestion" I have left doesn't tackle the problem, only the symptom: <?php if($d[17] == 'Full') { echo 'selected'; } ?>
 
maybe something else, what php version are you using?
 
can you try like below <?php $d[17] = 'Full';?> <select class="form-control" name="text_06" id="text_06_<?=$this->instanceID?>" class="form-control"> <option value="--" <?php echo ($d[17]=='--') ? 'selected':'';?>>-</option> <option value="Full" <?php echo ($d[17] == 'Full') ? 'selected':'';?>>The full review period</option> <option value="Half" <?php echo ($d[17] == 'Half') ? 'selected':'';?>>50% of the review period</option> <option value="None" <?php echo ($d[17] == 'None') ? 'selected':'';?>>None of the above</option> </select>
 
"===" doesn't work, PHP version is 5.6.31, ternary works elsewhere fine and that other way of doing it doesn't work either
100% something up with the framework the vendor is using. I done the same with "plain" PHP and it worked. Used their "widget" functionality and it doesn't. Will raise a request with them and find out what is happening
 
What happens if you hardcode the selected attribute on that option? Like so: <option value="Full" selected>The full review period</option>
 
10:42 PM
That works @T.A.C.Commandeur
 
Does it work for other options? What happens if you set $d[17] to 'Half'?
 
No luck still - it's definitely something to do with vendor specific code
 
The last thing I can think of might seem far fetched but I've seen it happen. Some times a string contains hidden characters (like end of line). This seems to happen when copying from the debug window of the phpStorm IDE. Re-typing the code by hand could fix it.
 
@TACCommandeur that’s the point of var_dump - it shows the length of the string
 

last day (15 days later) »