user1299028
user1299028

Reputation: 315

How do i pass a variable from a form through to a php page in an ajax script?

I have this form:

<form onsubmit="serverconnect('Div1', 'query.php'); return   
false;" 
Minimum Discount: <br />
<input type="radio" value="20%" name="discount" /> 20% <br />
<input type="radio" value="15%" name="discount" /> 15% <br />
<input type="radio" value="10%" name="discount" /> 10% <br />
<input type="radio" value="5%" name="discount" /> 5% <br />
<input type="submit" value="Refine Search" />
</form>

On submit, the form calls the following ajax script

<script language="javascript" type="text/javascript">


var xmlhttp = false;


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


function serverconnect(divID, datalocation)
{
var1=$clickvalue;
if(xmlhttp)
{
    var obj = document.getElementById(divID);
    xmlhttp.open("GET", datalocation);

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

}
</script>

I need to get the form data through to my query.php page (which is what the ajax is outputtng) to modify its output, without using the jquery library.

So is there a way to pass the form data through the ajax function to the page that the function calls?

Upvotes: 0

Views: 85

Answers (1)

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

Yes you can pass form data using ajax. As you are using GET method only you have to embed your data with the page like this

<input type="radio" value="20%" name="discount" id="discount" /> 20% <br />


var obj = document.getElementById(divID);
var disc = document.getElementById("discount");
xmlhttp.open("GET", datalocation + "?disc="+disc);

and in query.php use $_GET['disc'] for getting get data.

Upvotes: 4

Related Questions