Member Avatar for TechySafi
<script language = "javascript">
      var XMLHttpRequestObject = false;

      if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new
          ActiveXObject("Microsoft.XMLHTTP");
      }

      function getData(dataSource, divID)
      {
        if(XMLHttpRequestObject) {
          var obj = document.getElementById(divID);
          XMLHttpRequestObject.open("GET", dataSource);

          XMLHttpRequestObject.onreadystatechange = function()
          {
             if (XMLHttpRequestObject.readyState == 4 &&
               XMLHttpRequestObject.status == 200) {
                 obj.innerHTML =
                   XMLHttpRequestObject.responseText;
             }
           }

           XMLHttpRequestObject.send(null);
        }
      }
    </script>

Here I call the ajax fuction on post.php :

<tr>
    <td colspan="2" width="250">Choose Subject: <select onchange="getData('post2.php?data='+this.value,
          '[B]mydiv[/B]')" style="width:250px; height:25px; margin-left:5px" name="sub">
	<?php
				include('db.php');
				$queryn=mysql_query("SELECT * FROM  `subjects`");
				echo "<option value=none>=========Select=========</option>";
				while($rown=mysql_fetch_row($queryn))
				
				{
				 echo "<option value=$rown[0]>$rown[1]</option>";
				}
				?> 
                </select></td>
  </tr

>

Here is the blank div where fetched data will be appeared

<tr>
    <td colspan="2" width="400"><div id="fake" style="float:left"> Choose Mother Topic: </div> <div id="[B]mydiv[/B]" style="float:left"> </div>
    </td>
</tr>

And here is the post2.php for

<?php
$sub=$_GET['data'];
include('db.php');
$run=mysql_query("SELECT * from mother_topics where s_id='$sub'");
echo "<select style=width:400px; height:25px; margin-left:5px name=mt>";
while($rows=mysql_fetch_row($run))
				{
				 echo "<option value=$rows[0]>$rows[2]</option>";
				}
echo "</select>";
?>

It works fine and as soon as I change the subject, a dropdown menu appears on mydiv.Pretty fine but when I'm submitting this form and trying to retrieve the value of select name=mt with $var=$_POST; on post.php its cant receive anything!! I need to find a way...please help.

If you still didnt get what Im talking about then go to techysafi.co.cc/login.php and use df and sdf as username and password(dummy), after login go to techysafi.co.cc/post.php .
Thanks

I have not understood your problem exactly i guess,and your sample site doesn't allow me to login as per your user name and password,i don't know why so.


What i am getting in your problem is like this,
1) You make a drop down dynamically through Ajax and psot2.php on change of the values of your drop down name="sub"

2) But when name="mt" is created,and a value is selected from this second drop down,you want to fire post event on any button or something and want to submit this drop down's selected value to a certain page.


So the solution to this would be here

<tr>
<td colspan="2" width="400"><div id="fake" style="float:left"> Choose Mother Topic: </div> <div id="mydiv" style="float:left"> </div>
</td>
</tr>

The above code that you have written may not be inside your form tag,so what happens is when you create a dropdown dynamically,the dropdown gets created but not inside the form tag,so a post method will not give you that value.

And secondarily please provide an 'Id' attribute to both of the drop downs,because sometimes this also causes the problem,as name attribute sometimes doesn't work as expected.

:)
Hope it helps you...

Member Avatar for TechySafi

Yeah, your points are right. But the problem is I can't retrieve the value from newly born drop-down. I guess Im creating that dropdown by echoing from another page, that is causing this problem. Could you suggest me any other way that can perform what I want to do...?

<form action="post.php" method="post">
    <td><textarea class="big" name="topicedit" cols="" rows=""></textarea></td>
  </tr>
  <tr>
    <td>Details:</td>
    <td><textarea class="big2" name="details" cols="" rows=""></textarea></td>
  </tr>
  <tr>
    <td colspan="2" width="250">Choose Subject: <select onchange="getData('post2.php?data='+this.value,
          'mydiv')" style="width:250px; height:25px; margin-left:5px" name="sub">
	<?php
				include('db.php');
				$queryn=mysql_query("SELECT * FROM  `subjects`");
				echo "<option value=none>=========Select=========</option>";
				while($rown=mysql_fetch_row($queryn))
				
				{
				 echo "<option value=$rown[0]>$rown[1]</option>";
				}
				?> 
                </select></td>
  </tr>
  <tr>
    <td colspan="2" width="400"><div id="fake" style="float:left"> Choose Mother Topic: </div> <div id="mydiv" style="float:left"> [B]Here the dropdown appears from post2.php[/B] </div>
    </td>
    <td>
    
     </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td align="center">
    <input type="submit" name="post" value="Post" /> <input type="reset" name="resetall" value="Reset All" /></td>
  </tr>
</table>
</form>

That div is inside the form tag so that is not a problem.

One thing i can suggest is to use ajax again to send the data to the desired page you wish.

But before trying this,put an alert in second drop down's onChange event,just to check that option's value are alerted or not. I am sure you know how to alert option values.

Member Avatar for TechySafi

javascript alert working fine....its showing the value on alert if I call an alert fuction onchange event.

But I don't understand why php is not taking the value?!

I will try to solve your problem,but why are you not thinking to use ajax to pass that data?

Member Avatar for TechySafi

I'm not good at these stuff....basically I hardly go beyond php but now seems its a must.

Okay I will try to find some alternative.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.