Solved

PHP/JavaScript Form Listbox Value Retention

Posted on 2007-03-20
2
1,381 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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

631 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