Jump to content

Ajax Dropdownlist Problem


warhead2020

Recommended Posts

Hi, i have this problem regarding ajax dropdownlist. Here is my code.

 

this is my ajax code

function getXMLHTTP() 
{ 
var xmlhttp=false;	
try
{
	xmlhttp=new XMLHttpRequest();
}
catch(e)	
{		

	try
	{			
		xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e1)
		{
			xmlhttp=false;
		}
	}
}

return xmlhttp;
}

function getIpt(strURL) 
{		

var req = getXMLHTTP();

if (req) 
{

	req.onreadystatechange = function() 
	{
		if (req.readyState == 4) 
		{
			// only if "OK"
			if (req.status == 200) 
			{						
				document.getElementById('institusidiv').innerHTML=req.responseText;						
			} 
			else 
			{
				alert("There was a problem while using XMLHTTP:\n" + req.statusText);
			}
		}				
	}			
	req.open("GET", strURL, true);
	req.send(null);
}					
}

function getKursus(strURL) 
{		

var req = getXMLHTTP();

if (req) 
{

	req.onreadystatechange = function() 
	{
		if (req.readyState == 4) 
		{
			// only if "OK"
			if (req.status == 200) 
			{						
				document.getElementById('kursusdiv').innerHTML=req.responseText;						
			} 
			else 
			{
				alert("There was a problem while using XMLHTTP:\n" + req.statusText);
			}
		}				
	}			
	req.open("GET", strURL, true);
	req.send(null);
}					
}

 

 

Here is my php code (ajaxIPT.php)

<?php
require_once("../../class/class.mysqldb.php");
$code = intval($_REQUEST['code']);
$kod_ins = $_REQUEST['kod_ins'];
$con = new MysqlDB;
$con->connectMysql();


if($code == 1)
{
$queryIpt = "select ctext from kod_ipt where kat_ipt = 1";
$resultQuery = $con->query($queryIpt);
?>
<select name="INSTITUSI" id="INSTITUSI">
<option>--Choose--</option>
<?php
	while($row=$con->fetch_array($resultQuery)) 
	{ 
?>
<option value="<?php echo $row['ctext']; ?>"><?php echo $row['ctext']; ?></option>
<?php 
	}
?>
</select>
<?php 
}
elseif($code == 2)
{
$queryIpt = "select ctext from kod_ipt where kat_ipt = 2";
$resultQuery = $con->query($queryIpt);
?>
    <select name="INSTITUSI" id="INSTITUSI">
    <option>--Choose--</option>
    <?php
        while($row=$con->fetch_array($resultQuery)) 
        { 
    ?>
    <option value="<?php echo $row['ctext']; ?>"><?php echo $row['ctext']; ?></option>
    <?php 
        }
    ?>
    </select>
<?php 
}
elseif($code == 3)
{
?>
<input name="INSTITUSI" type="text" id="INSTITUSI" size="30" value="" />
<?php 
}
elseif($code == 4)
{
?>
<input name="INSTITUSI" type="text" id="INSTITUSI" size="30" value="Test" /> 
<?php
}
?>

 

And this is my form

<table width="100%" cellspacing="1" class="main">
<tr>
      <th width="25%">
      <div align="left">
      <strong>A</strong>
      </div>
      </th>
      <th align="center" width="2%">
      <div align="center">:</div>
      </th>
      <td width="73%" class="bodystyle">
            <select name="KOD_INSTITUSI" id="KOD_INSTITUSI" onchange="getIpt('ajaxIPT.php?code='+this.value),tukarPeringkat(this.value);">
            <option value="">-Choose-</option>
            <?php
                     while($row=$con->fetch_array($resultIptQuery)) 
                     { 
            ?>
                      <option value = "<?php echo $row['id']; ?>"><?php echo $row['cat']; ?></option>
            <?php 
                       }
            ?>
            </select> 
      </td>
</tr>
<tr>
      <th>
      <div align="left">
      <strong>B</strong>
      </div>
      </th>
      <th align="center"><div align="center">:</div></th>
      <td class="bodystyle">
      <div id="institusidiv">
            <select name="INSTITUSI" id="INSTITUSI" onchange="">
            <option value="">-Choose-</option>
            </select>
      </div>                    
      </td>
</tr>
</table>

 

this code works perfectly. When i choose KOD_INSTITUSI, the INSTITUSI dropdownlist change. The problem now is, when i post the form, the INSTITUSI IS NULL. It is like, the result in INSTITUSI dropdownlist only "virtually/temporarily"(sorry if im using wrong word ;)) exist there not the real object. That's y when i post the form, the INSTITUSI dropdownlist post NULL. I think maybe it is because the way i send data from ajaxIPT.php to the ajax code?? Hope someone out there can help me. Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/181952-ajax-dropdownlist-problem/
Share on other sites

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.