Click to See Complete Forum and Search --> : Need help with AJAX/PHP script


Ntrimgs
07-26-2011, 02:39 PM
I'm trying to do something that seems simple but it's not coming out right.

I am trying to make it so on a form that will go to Paypal, someone has to check a checkbox first and then an AJAX script will make the "Make Payment" button active and real so they can click on it and go directly to Paypal.

My problem is that I need to have an inactive button in its place, and then have it be replaced when someone checks the box. If someone checks off the box then the inactive button needs to pop back in.

The HTML checkbox code is this:


<div align="center">I agree <input type="checkbox" value="yes" onclick="itemSelect(this.value)" /></div>


The AJAX script is this:


var xmlhttp;

function itemSelect(item_type)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="type_selection.php";
url=url+"?type="+item_type;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("signup_btn").innerHTML=xmlhttp.responseText;
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}


The PHP script that is triggered is this:


<?php
$id = $_GET['type'];

if($id == "yes") {
?>
<input type="submit" value="MAKE PAYMENT" class="active" name="submit" />
<?php
}
?>


The initial inactive button is displayed in a simple div within the form, and this is where the AJAX script will direct the above script to replace it:


<div id="signup_btn"><img src="Images/inactive_btn.jpg" /></div>


So it seems simple enough but it's not working. Is this even feesible to do? Is there another method?