Q695 Posted May 4, 2013 Share Posted May 4, 2013 (edited) I know the PHP is correct, but the HTML is adding extra information, and it's not pulling up the selected information based on the statement set. <select name="slot" size="1"> <option value="" <?php if ($item_slot=="0"){ echo "selected";} ?>>Pick One</option> <option value="1"<?php if ($item_slot=="1"){ echo "selected";} ?>>Amulet</option> <option value="2"<?php if ($item_slot=="2"){ echo "selected";} ?>>Head Gear</option> <option value="3"<?php if ($item_slot=="3"){ echo "selected";} ?>>Epaulets</option> <option value="4"<?php if ($item_slot=="4"){ echo "selected";} ?>>Shield</option> <option value="5"<?php if ($item_slot=="5"){ echo "selected";} ?>>Chest Gear</option> <option value="6"<?php if ($item_slot=="6"){ echo "selected";} ?>>Weapon</option> <option value="7"<?php if ($item_slot=="7"){ echo "selected";} ?>>Ring</option> <option value="8"<?php if ($item_slot=="8"){ echo "selected";} ?>>Boots</option> <option value="9"<?php if ($item_slot=="9"){ echo "selected";} ?>>Gloves</option> <option value="10"<?php if ($item_slot=="10"){ echo "selected";} ?> >Backpack</option> </select> output code <select size="1" name="slot"> <option value="">Pick One</option> <option value="1">Amulet</option> <option selected="" value="2">Head Gear</option> <option value="3">Epaulets</option> <option value="4">Shield</option> <option value="5">Chest Gear</option> <option value="6">Weapon</option> <option value="7">Ring</option> <option value="8">Boots</option> <option value="9">Gloves</option> <option value="10">Backpack</option> </select> Edited May 4, 2013 by Q695 Quote Link to comment https://forums.phpfreaks.com/topic/277610-why-is-my-code-adding-extra-charicters/ Share on other sites More sharing options...
requinix Posted May 4, 2013 Share Posted May 4, 2013 That output looks right to me... You know you're looking at it after it's been parsed and reassembled by the browser, right? If you want to see the actual output of the code you have to do a View Source. Quote Link to comment https://forums.phpfreaks.com/topic/277610-why-is-my-code-adding-extra-charicters/#findComment-1428116 Share on other sites More sharing options...
Solution Q695 Posted May 4, 2013 Author Solution Share Posted May 4, 2013 for some reason I had to reenter the page. Quote Link to comment https://forums.phpfreaks.com/topic/277610-why-is-my-code-adding-extra-charicters/#findComment-1428120 Share on other sites More sharing options...
mac_gyver Posted May 4, 2013 Share Posted May 4, 2013 you have too much code, to write, test, reenter, change, add items to, reuse for different things... by defining an array to hold your data, you can write/test generic code that you can reuse for different things simply by changing the data definition - <?php $options[1] = "Amulet"; $options[2] = "Head Gear"; $options[3] = "Epaulets"; $options[4] = "Shield"; $options[5] = "Chest Gear"; $options[6] = "Weapon"; $options[7] = "Ring"; $options[8] = "Boots"; $options[9] = "Gloves"; $options[10] = "Backpack"; ?> <select size="1" name="slot"> <option value="">Pick One</option> <?php $item_slot = 2; // test value foreach($options as $value=>$legend){ $selected = $item_slot == $value ? " selected='selected'": ''; echo "<option value='$value'$selected>$legend</option>\n"; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/277610-why-is-my-code-adding-extra-charicters/#findComment-1428123 Share on other sites More sharing options...
Q695 Posted May 4, 2013 Author Share Posted May 4, 2013 I originally tried doing the select with a function, but it failed, and wrote selected where the function was placed in the document. Quote Link to comment https://forums.phpfreaks.com/topic/277610-why-is-my-code-adding-extra-charicters/#findComment-1428216 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.