Page 1 of 1

Ajax and php

Posted: Wed Mar 05, 2008 1:20 am
by simonj2
Hi,

I'm building a form that uses ajax to populate a list of tafes (educational institutes) based on which state (in Australia) you select. The form uses GET to get the information from ajax and POST to post the information to the next page. And after 3 hours of trying to figure out why it doesn't work, I'm turning to you guys for help! Basically, the ajax works, as in the select box shows up when I select a state, however, when I post it, all information other than the select box gets posted.

***** PLEASE USE THE

Code: Select all

TAG WHEN POSTING *****[/color]

Here's some code:

 form start:

Code: Select all

<form action="register.php" method = "post" enctype="multipart/form-data">  
ajax setup:

Code: Select all

<? 
 
echo "<script language=\"javascript\" type=\"text/javascript\">
<!--
//Browser Support Code
function ajaxFunction(selected){
    var ajaxRequest;  // The variable that makes Ajax possible!
   
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject(\"Msxml2.XMLHTTP\");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject(\"Microsoft.XMLHTTP\");
            } catch (e){
                // Something went wrong
                alert(\"Your browser broke!\");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            var ajaxDisplay = document.getElementById('ajaxDiv');
            ajaxDisplay.innerHTML = ajaxRequest.responseText;
        }
    }
    var fac1 = selected.selectedIndex;
    var face = selected[fac1].text;
    ajaxRequest.open(\"GET\", \"showtafe.php?fac=\" + face, true);
    ajaxRequest.send(null);
}
 
//-->
</script>";
 
 ?>
list of states:

Code: Select all

 <? echo "<select name=\"state\" id=\"state\" onChange=\"ajaxFunction(this);\">"; ?>
                    <option value="Select" selected>Select</option>
                    <option value="ACT">ACT</option>
                    <option value="NSW">NSW</option>
                    <option value="NSW">NT</option>
                    <option value="NSW">QLD</option>
                    <option value="NSW">SA</option>
                    <option value="NSW">TAS</option>
                    <option value="NSW">VIC</option>
                    <option value="NSW">WA</option>
                    </option>
                  </select>
Then showtafe.php has the following text:

Code: Select all

<?
 
include 'db.php';
 
$faculty = $_GET['fac'];
 
$faculty = mysql_real_escape_string($faculty);
 
$findfac = mysql_query("SELECT * FROM tafes where state = '$faculty' order by tafe_name ")
or die(mysql_error()); 
 
//$findfac2 = mysql_fetch_array($findfac);
 
    if (($faculty != "All") && ($faculty != "Select")){
 
    echo "<select name=\"tafes\">";
    while($row = mysql_fetch_assoc($findfac))
    {
    echo "<option value=$row[tafe_id]>$row[tafe_name]</option>";
    }
    echo "</select><br>";
}else if ($faculty == "Select") {
    echo "Please select a state first";
}
 
?>


then back in my original form I have:


Code: Select all

echo "<div id='ajaxDiv'>The Relevant TAFEs will display here</div>";


_______
And that's it. I'm worried that possibly inserting into the middle of a post form won't work? But I can't find any mention of that thus far. With all the other Ajax I've done (including reusing the above code) I've been using forms that submit data via GET, and this makes me think this might be part of the problem. I'd really appreciate any response :)



Simon

Re: Ajax and php

Posted: Sat Mar 08, 2008 2:43 pm
by yacahuma
well, ajax still a lot of Mumbo Jumbo for me. enever I want to do something like that I just use a hidden frame

page1.php

Code: Select all

 
<head>
<script>
function loadPositions()
{
 var number = document.expbuilder.industry.selectedIndex;
 parent.positionFrame.location.href = 'pos_iframe.php?iid=' + document.expbuilder.industry.options[number].value;
}
</script>
</head>
<html>
<form name="expbuilder" >
<select name="industry" onchange="loadPositions(this.form)>
....
</select>
 
<select name="position">
</select>
</form>
<iframe name="positionFrame" src="pos_iframe.php" style="width:0px; height:0px; border: 0px;"></iframe>
 
</html>
 
 
pos_iframe.php

Code: Select all

 
<?
require_once 'start_session.php';
$iid = HtmlPage::get('iid','POST:GET','');
$pid = HtmlPage::get('pid','POST:GET','');
 
require_once 'lib/PositionMgrLib.php';
$pmgr = new PositionMgr();
$pmgr->cn = $CONNECTION_GLB;
//$pmgr->cn->debug=true;
list($rs,$industry_positions,$m1,$m2) = $pmgr->getAllKV($iid);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Hidden Position Frame</title>
</head>
<body>
<? if (intval($iid) > 0) {?>
<script language="JavaScript"><!--
 
theSel = parent.document.forms['expbuilder'].position;
theSel.options.length = 0; // removes all options.
theSel.options[theSel.options.length] = new Option('Select Position','');
<? $i=1;foreach ($industry_positions as $k=>$v){ ?>
      theSel.options.length = (<?=$i++?>);
      theSel.options[theSel.options.length] = new Option('<?=$v?>','<?=$k?>');
            <? if (intval($pid)>0 && $k==$pid){ ?>
            theSel.options[<?=$i-1?>].selected = true;
            <?}?>
<?}?>  
//--></script>
<?}?>
</body>
</html>
 
 

This code works ,you just have to change it to whatever you need. Basically the iframe_page just receives the parameters of the first combo box and fill the second. In your case the first combo will be the city .