Solved

PHP/JavaScript Form Listbox Value Retention

Posted on 2007-03-20
2
1,377 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 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