Jump to content


Photo

How ajax with onchange select box return the selected="selected"

php ajax html

  • Please log in to reply
1 reply to this topic

#1 hitman47

hitman47

    Member

  • Members
  • PipPip
  • 14 posts

Posted 26 October 2013 - 01:24 PM

I want to create a real esatte website. This is part of my code. The user selects a category.Then ajaxhelp is called and sends the cariable category to  ajax.php.

 

There it gets the type of estate wuering the database. however i want to enable the selected="selected" option, in order in category php in id=category in the bottom to print the option the user has already selected. i don't know how to it. The selected option doesnt work

 

<script>

function ajaxhelp(str)
{
if (str=="")
  {
  document.getElementById("category").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("category").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","prosthiki_akinitou_form_ajax.php?category="+str,true);
xmlhttp.send();
}

</sqript>

 

//category.php

<tr >  
    <td><h4>Category:<small class="red">*</small></h4></td> <td ><select name="category"  onChange="ajaxhelp(this.value);">
      <?php
      
         $category_array = get_categories();  //estate_fns
        
     echo '<option  value=""><h4>- Choose Estate Category -</h4></option>';
     foreach ($category_array as $row)  {
           if($estate_array['catid'] === $row['catid'])
           { echo '<option  selected="selected" value="'. $row['catid'].'"><h4>' . $row['catname'].'</h4></option>';   }
           else { echo '<option  value="'. $row['catid'].'"><h4>' . $row['catname'].'</h4></option>';}
       }
       ?>
       </select> </td></tr>
    
     <?php

   
     echo '<tr >';  
           echo '<td><h4>Estate type:</h4></td> <td ><select  name="eidos" id="category"  >';  
         echo '</select> </td></tr>';    
    
       ?>    

 

//ajax.php

include ('estate_sc_fns.php');
$katigoria = $_GET['category'];  //get the value from ajax.php

            $estate_array = get_estate_details($_SESSION['estate_code']); //get the estate type the user already has entered from mysql
 
             $eidos_array = get_estate_type($category);  //get all possible estate types from mysql
         foreach ($eidos_array as $row)  {
           if($estate_array['estate_type'] === $row['estate_type'] )
           { echo '<option   selected="selected"  value="'. $row['estate_type'].'"><h4>' . $row['estate_name'].'</h4></option>';   }
           else { echo '<option   value="'. $row['estate_type'].'"><h4>' . $row['estate_name'].'</h4></option>';}
           }
  

But the selected="selected" doesn't work!

 

Please help how to do it?

 

 



#2 hitman47

hitman47

    Member

  • Members
  • PipPip
  • 14 posts

Posted 27 October 2013 - 03:58 AM

I want to create a real esatte website. This is part of my code. The user selects a category.Then ajaxhelp is called and sends the cariable category to  ajax.php.

 

There it gets the type of estate wuering the database. however i want to enable the selected="selected" option, in order in category php in id=category in the bottom to print the option the user has already selected. i don't know how to it. The selected option doesnt work

 

<script>

function ajaxhelp(str)
{
if (str=="")
  {
  document.getElementById("category").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("category").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","prosthiki_akinitou_form_ajax.php?category="+str,true);
xmlhttp.send();
}

</sqript>

 

//category.php

<tr >  
    <td><h4>Category:<small class="red">*</small></h4></td> <td ><select name="category"  onChange="ajaxhelp(this.value);">
      <?php
      
         $category_array = get_categories();  //estate_fns
        
     echo '<option  value=""><h4>- Choose Estate Category -</h4></option>';
     foreach ($category_array as $row)  {
           if($estate_array['catid'] === $row['catid'])
           { echo '<option  selected="selected" value="'. $row['catid'].'"><h4>' . $row['catname'].'</h4></option>';   }
           else { echo '<option  value="'. $row['catid'].'"><h4>' . $row['catname'].'</h4></option>';}
       }
       ?>
       </select> </td></tr>
    
     <?php

   
     echo '<tr >';  
           echo '<td><h4>Estate type:</h4></td> <td ><select  name="eidos" id="category"  >';  
         echo '</select> </td></tr>';    
    
       ?>    

 

//ajax.php

include ('estate_sc_fns.php');
$katigoria = $_GET['category'];  //get the value from ajax.php

            $estate_array = get_estate_details($_SESSION['estate_code']); //get the estate type the user already has entered from mysql
 
             $eidos_array = get_estate_type($category);  //get all possible estate types from mysql
         foreach ($eidos_array as $row)  {
           if($estate_array['estate_type'] === $row['estate_type'] )
           { echo '<option   selected="selected"  value="'. $row['estate_type'].'"><h4>' . $row['estate_name'].'</h4></option>';   }
           else { echo '<option   value="'. $row['estate_type'].'"><h4>' . $row['estate_name'].'</h4></option>';}
           }
  

But the selected="selected" doesn't work!

 

Please help how to do it?

prosthiki_akinitou_form_ajax.php = ajax.php this is not the problem






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com