Jump to content

PHP - AJAX select with onchange event doesn't work


hitman47

Recommended Posts

Hallo.

 

I have a form

 

<form id="form2" method="post" action="insert_data.php>">

<select name="catid" id="catid" onChange="showkatigoria(this.value);">

<option  value="'. $row['catid'].'"><h4>' . $row['catname'].'</option> // something i need  with php, i dont' think it matters

<option  value="'. $row['catid'].'"><h4>' . $row['catname'].'</option>

<option  value="'. $row['catid'].'"><h4>' . $row['catname'].'</option>

//option value bla bla bla bla i have 4-5 options value.

</select>

 

My javascript:

 

 <script type="type/javascript">
        function showkatigoria(str)
        {
        if (str=="")
          {
          document.getElementById("catid").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("catid").innerHTML=xmlhttp.responseText;
            }
          }
        xmlhttp.open("GET","insert_data.php?category="+str,true);
        xmlhttp.send();
        }
    </script>

 

 

It doesn't work what ever i do. Please help! I don't know javascript, let alone ajax.

 

It's urgent. Please help!

Edited by hitman47
Link to comment
Share on other sites

Its easy. I have a form with many select options. Like this website: http://www.tospitimou.gr/en/index.jsp

 

Check it at the top where it says YOU ARE AT. IF you choose an area then in the select box that follows some options are shown. If you choose something differnet in teh first select different choises come up in the second.

 

Same thing in this site: http://www.realestatecorner.gr/

 

I foy choose something in the "Περιοχή:"  that means location in greek, then a new select option emerges, that was not shown in the beggining. If you choose another location, other options come up. How shall i do iti? I dont have a clue about ajax. With the code i gave you

Link to comment
Share on other sites

Thank you bro.

 

One question only. In the first link for example ajax sends the variable 'id' with post  to a different page than the original-mother select is. The mother select is in sections_demo.php and ajax sends the variable to ajax_city.php

 

From there it queries the dtabase and the extra option field is echoed. Why?

 

Could this be done within the same page sections_demo.php?

 

This is something i don't understand

Edited by hitman47
Link to comment
Share on other sites

From there it queries the dtabase and the extra option field is echoed. Why?

Because the output of the ajax request (ajax_city.php) is used to add the city options to the page for the selected country.

 

Could this be done within the same page sections_demo.php?

Yes. You just need to make sure when your php script receives the ajax request it only outputs the city options and not your whole webpage. For you to understand this more you'll want to research more about what is ajax and how it works.

Edited by Ch0cu3r
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.