Jump to content

Display selected AFTER failed submit


phppup
 Share

Recommended Posts

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??

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.