Solved

PHP/JavaScript Form Listbox Value Retention

Posted on 2007-03-20
2
1,360 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
2 Comments
 
LVL 9

Accepted Solution

by:
under_dog earned 500 total points
Comment Utility
 <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
Comment Utility
ah ha, didn't think of that. Thanks. :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now