?
Solved

PHP/JavaScript Form Listbox Value Retention

Posted on 2007-03-20
2
Medium Priority
?
1,388 Views
Last Modified: 2012-06-27
I've got an HTML form that is submitting data to PHP. I'm working on what happens when a user submits the form, but it fails validation. So the form needs to re-display with the values the user entered using session variables. It was easy with the other form elements, but I'm having a problem with the list boxes.

I'm trying to use JavaScript to cycle through the listbox and look for a value that matches PHP's session variable. I'm kind of hung up on the code. Help, please? btw - If there is an easier way of doing this that would be great. Thanks.

<td valign="top">Manages Others: </td>
<td>
  <select name="manages_others" tabindex="12">
    <option value="" selected="selected">Please Select One</option>
    <option value="1">Yes</option>
    <option value="0">No</option>
  </select>
</td>
<script type="text/javascript">
  var obj = document.all.add_job.manages_others;
  var index = obj.selectedIndex;
  var index_value = obj.options[index].value;
                         
  for (i=0; i < obj.length; i++) {
    if (index_value == <?php print($_SESSION['manages_others']); ?>) {
      //I've had all sorts of code here and played with the condition above, but I think I'm a little confused on how list boxes work in JS                                    
     }
  }       
</script>
0
Comment
Question by:damijim
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Accepted Solution

by:
under_dog earned 2000 total points
ID: 18756366
 <select name="manages_others" tabindex="12">
    <option value=""<?php if ($_SESSION['manages_others'] == "") { echo ' selected'; } ?>>Please Select One</option>
    <option value="1"<?php if ($_SESSION['manages_others'] == "1") { echo ' selected'; } ?>>Yes</option>
    <option value="0"<?php if ($_SESSION['manages_others'] == "2") { echo ' selected'; } ?>>No</option>
  </select>

You don't need the javascript ;)
0
 
LVL 1

Author Comment

by:damijim
ID: 18756377
ah ha, didn't think of that. Thanks. :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question