phppup Posted June 28, 2021 Share Posted June 28, 2021 If my form is submitted, but fails during PHP validation, then I want info that was already submitted to be available. <input type="text" name="username" value="<?php if(isset($_POST['username'];} { echo $username; } ?> > </div> This works fine and defines my goal. My troubles arose when I attempted a similar design with a drop-down menu. Making matters worse, that menu uses JavaScript. <script> document.write("<select name='menu'>"); for(i = 1; i < 20; i++) { document.write("<option value=' "+i+"'> " + i + "</options>"); } document.write("</select>"); </script> I've begun trying to add PHP into the <option> tag, but am not sure how (or if it's acceptable) to integrate the JS for the desired result. document.write("<option value=' "+i+"' <? if($_POST['menu'] == ?> +i+ <? echo selected";?> > " + i + "</options>"); I suppose I could swap the JS with PHP, but then I put unnecessary usage to the server. Best solution?? Quote Link to comment https://forums.phpfreaks.com/topic/312998-display-selected-after-failed-submit/ Share on other sites More sharing options...
requinix Posted June 28, 2021 Share Posted June 28, 2021 You cannot combine PHP code and Javascript code. Fundamental concept, that. You can use PHP code to output Javascript code. Such as a line of code that sets a variable. Then your Javascript code could use that variable to decide whether to mark the option as selected. But really, document.write? What is this? The '90s? Quote Link to comment https://forums.phpfreaks.com/topic/312998-display-selected-after-failed-submit/#findComment-1587631 Share on other sites More sharing options...
phppup Posted June 28, 2021 Author Share Posted June 28, 2021 It seems effective and it's what I'm accustom to. What is the new fangled option? Why use it other than to be more fashionable? Quote Link to comment https://forums.phpfreaks.com/topic/312998-display-selected-after-failed-submit/#findComment-1587632 Share on other sites More sharing options...
requinix Posted June 28, 2021 Share Posted June 28, 2021 2 minutes ago, phppup said: It seems effective and it's what I'm accustom to. What is the new fangled option? Why use it other than to be more fashionable? I believe people felt the same way about their horse and buggy when Ford introduced the car. Quote Link to comment https://forums.phpfreaks.com/topic/312998-display-selected-after-failed-submit/#findComment-1587633 Share on other sites More sharing options...
phppup Posted June 28, 2021 Author Share Posted June 28, 2021 What would be the car, in this instance? jQuery? The car has obvious advantages. (although there are far less deaths from buggy incidents per capita, so is the trade off worth it?) Not trying to be a smarty, but if you are referring to jQuery, I don't find it as straight forward as JS, so unless I'm missing some great advantage, I think I have enough troubles already. LOL As always, your insight and guidance are appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/312998-display-selected-after-failed-submit/#findComment-1587635 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.