Jump to content

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


hitman47

Recommended Posts

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?

 

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.