Return PHP code result to AJAX responseText

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rpk2006
Forum Newbie
Posts: 4
Joined: Wed Sep 16, 2009 3:53 am

Return PHP code result to AJAX responseText

Post by rpk2006 »

Code: Select all

 
<?php
    $SelectedCountry = $_GET['value'];
    include("connection.php");
    OpenConnection();
    $result = mysql_query("Select state from statemaster where countryid = (select countryid from countrymaster where country = '" . $SelectedCountry . "')");
    return $result;
?>
 
When the above code returns result, how to get in Ajax xmlhttp.responseText
amargharat
Forum Commoner
Posts: 82
Joined: Wed Sep 16, 2009 2:43 am
Location: Mumbai, India
Contact:

Re: Return PHP code result to AJAX responseText

Post by amargharat »

I consider your script as ajaxresult.php and doing with the help of prototype
call below javascript function wherever you want

Code: Select all

<script language="javascript" type="text/javascript" src="prototype.js"></script>
<script language="javascript">
function getAjaxResult()
{
    var URL = "ajaxresult.php" //provide your php script path
    
    new Ajax.Request
    (
        URL,
        {
            method: "get",
            onSuccess: function (response)
            {
                $("resultContainerID").innerHTML = response.responseText; //resultContainerID is the container where you want show your result e.g. <div id="resultContainerID"><!--Your result will come here--></div>
            }                       
        }
    );
}
</script>
download prototype.js from below link
http://www.games2playnow.com/js/prototype.js
rpk2006
Forum Newbie
Posts: 4
Joined: Wed Sep 16, 2009 3:53 am

Re: Return PHP code result to AJAX responseText

Post by rpk2006 »

I also want to know how to include:

<select> </select> within JavaScript. I want to populate <select> with response.responseText.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Return PHP code result to AJAX responseText

Post by jackpf »

Haven't read this whole thread...but glancing at your last post, do you want createElement() and appendChild()?

Using those functions you can append elements (like <select>...) to the DOM.
rpk2006
Forum Newbie
Posts: 4
Joined: Wed Sep 16, 2009 3:53 am

Re: Return PHP code result to AJAX responseText

Post by rpk2006 »

Please illustrate. I attempted following code but it is not working:

Code: Select all

 
function stateChanged()
{
    if (xmlhttp.readyState==4)
    {
        document.getElementById("ddState").innerHTML=xmlhttp.responseText;
        var xmlDoc=xmlhttp.responseXML.documentElement;
        document.getElementById("ddState").innerHTML=
        xmlDoc.getElementsByTagName("ddState")[0].childNodes[0].nodeValue;
    }
}
 
amargharat
Forum Commoner
Posts: 82
Joined: Wed Sep 16, 2009 2:43 am
Location: Mumbai, India
Contact:

Re: Return PHP code result to AJAX responseText

Post by amargharat »

please follow the way i shown you
rpk2006
Forum Newbie
Posts: 4
Joined: Wed Sep 16, 2009 3:53 am

Re: Return PHP code result to AJAX responseText

Post by rpk2006 »

I modified the entire AJAX code as below:

Code: Select all

 
function showUser(str)
{
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="FillState.php";
    url=url+"?q="+str;
    getAjaxResult(url);
}
 
 
function getAjaxResult(URL)
{
   document.write(URL);
    new Ajax.Request
    (
        URL,
        {
            method: "get",
            onSuccess: function (response)
            {
                $("ddState").innerHTML = response.responseText;
            }                      
        }
    );
}
 
Please note the ShowUser function above gets response from this line:

echo "Country<select name=ddCountry id=ddCountry onchange=JavaScript:showUser(this.value);>";

I think I am not able to absorb what you suggested. Please make corrections accordingly.
Post Reply