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?

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.