Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2011
    Location
    Sunshine State
    Posts
    80
    Thanks
    18
    Thanked 0 Times in 0 Posts

    AJAX Dropdown Choice Not Staying "selected"

    I think this is an AJAX question more than a PHP question...if I'm mistaken, please feel free to move

    So this is the page I'm working on: AmeriLife & Health Services - Your Future Today!

    Before I start loading up useless code let me describe the issue: depending on the state selected, the "Select an office" dropdown is populated. The issue that I'm having is that I cannot get the offices list to generate an error if submitted with an invalid value, nor can I get the office selected to stay selected if there's an error somewhere else on the form (VA, TX & TN have the shortest office lists).

    AJAX:
    Code:
    function showOffices(str)
    {
    if (str=="")
      {
      document.getElementById("offices").innerHTML="";
      return;
      } 
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("offices").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","offices.php?offices="+str,true);
    xmlhttp.send();
    }
    Offices dropdown:
    PHP Code:
    <p>
    <label for="input011">Select an office: <br /><span style="font-size:12px;color:#333;">(FL, GA, NC, SC, TN, TX, VA only)</span></label>
    <select name="input011" id="offices">
    <option value="000">Please select an office</option>
    </select>
    <?php (isset($errors['input011']) ? '<br /><span class="error">'.$errors['input011'].'</span>' ''); ?>
    </p>
    Offices.php (VA office only):
    PHP Code:

    if (isset($_GET['offices']) && $_GET['offices'] == "VA" ) {    
        echo 
    '<option value="171" alt="AmeriLife &amp; Health Services of the Virginias, LLC, 3090 Electric Road, Suite C, Roanoke VA 24018" title="AmeriLife &amp; Health Services of the Virginias, LLC, 3090 Electric Road, Suite C, Roanoke VA 24018"'.(isset($_POST['input011']) && $_POST['input011'] == '171' ' selected="selected"' '').'>AMLH of the Virginias - Roanoke, VA</option>';

    Please let me know if you need more code or if you need more information...thanks in advance!

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 594 Times in 593 Posts
    You have nothing in your select drop down to be selected.
    You don't need name="input011"
    You do need an onchange in the dropdown to call your javascript: onchange="showOffices(this.value)" and pass the value of the selection.
    The following code don't work, because you always pass something to it. But if you did pass "" it would mess up your dropdown so I eliminated it.
    Code:
    if (str == ""){
    	document.getElementById("offices").innerHTML="";
    	return;
      }
    Changed were the ajax return is printed. You don't want that in the dropdown unless it's another selection to be made and then you want it in another dropdown.
    Also changed offices.php and used a switch.
    The HTML:
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    </head>
    <body>
    <p>
    <label for="input011">Select an office: <br />
    <span style="font-size:12px;color:#333;">(FL, GA, NC, SC, TN, TX, VA only)</span></label>
    
    <select id="offices" onchange="showOffices(this.value)">
    	<option value="">Please select an office</option>
    	<option value="FL">FL</option>
    	<option value="GA">GA</option>
    	<option value="NC">NC</option>
    	<option value="TN">TN</option>
    	<option value="TX">TX</option>
    	<option value="VA">VA</option>
    </select>
    <div id="address"></div>
    </p>
    
    
    <script type="text/javascript">
    function showOffices(str){
    	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
    		xmlhttp=new XMLHttpRequest();
    	}else{// code for IE6, IE5
    		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	xmlhttp.onreadystatechange=function(){
    		if (xmlhttp.readyState==4 && xmlhttp.status==200){
    			document.getElementById("address").innerHTML = xmlhttp.responseText;
    		}
    	}
    	xmlhttp.open("GET","offices.php?offices="+str,true);
    	xmlhttp.send('');
    }
    </script>
    </body>
    </html>
    And offices.php:
    PHP Code:
    <?php
    if (isset($_GET['offices'])){
        
    $state $_GET["offices"];
        switch (
    $state)
        {
        case 
    "VA":
          echo 
    "AmeriLife &amp; Health Services of the Virginias, LLC<br> 3090 Electric Road, Suite C<br> Roanoke VA 24018<br>
                AMLH of the Virginias - Roanoke, VA"
    ;  //DOES THIS BELONG???
          
    break;
        case 
    "GA":
          echo 
    "Some Place In Georgia";
          break;
        }
    }

    ?>
    Last edited by sunfighter; 03-17-2014 at 06:19 PM.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •