Solved

PHP/JavaScript Form Listbox Value Retention

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 dynamically set the form action using jQuery.

791 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