...

View Full Version : changing an AJAX $_GET to $_POST



jasonpc1
09-05-2010, 11:57 PM
I have found the following code and wish to have it do a POST instead of the GET

can someone tell me what needs changing so this does a POST

ajax.php


<?php
if (is_numeric($_GET['client_id'])) {
include("database.php");
$query="SELECT * FROM `client_addresses` WHERE `client_id` = '". db_input($_GET['client_id']) ."'";
$result=mysql_query($query);
?><select name="client_address">
<option>Select address</option><?
while( $res_array = mysql_fetch_assoc($result) ) {
echo "<option value=\"".$res_array['client_address']."\">".$res_array['client_address']."</option>";
} ?>
</select>
<?
}
?>


index.php


<html>
<head>
<script language="JavaScript" type="text/javascript">
function display_data(client_id) {
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null) {
alert ("Your browser does not support AJAX!");
return;
}
var url="ajax.php";
url=url+"?client_id="+client_id;
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
document.getElementById('employ_data').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function GetXmlHttpObject() {
var xmlhttp=null;
try {
// Firefox, Opera 8.0+, Safari
xmlhttp=new XMLHttpRequest();
}
catch (e) {
// Internet Explorer
try {
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlhttp;
}
</script>
</head>
<body>
<form>
<select name="client_name" onchange="display_data(this.value);">
<option>Select client</option>
<?php
include("database.php");
$customer_id = "11";
$query="SELECT * FROM `client_names`";// WHERE `customer_id` = '". $customer_id ."'
$result=mysql_query($query);
while( $res_array = mysql_fetch_assoc($result) ) {
echo "<option value=\"".$res_array['client_id']."\">".$res_array['client_id']."</option>";
}
?>
</select>
<div id="employ_data"><div>
</form>
</body>
</html>

Spudhead
09-07-2010, 03:30 PM
Well, I guess the first thing I'd have tried was changing:

xmlhttp.open("GET",url,true);
to

xmlhttp.open("POST",url,true);
... and see what happens.

But a more efficient way might be to avoid rolling your own AJAX code altogether and let jQuery do all the heavy lifting:


<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#client_name').change(function(){
var client_id = $(this).val();
$.post('ajax.php',{client_id:client_id}, function(response_text){
$('#employ_data').html(response_text);
});
});
});
</script>
</head>
<body>
<form>
<select id="client_name">
<option>Select client</option>
<?php
include("database.php");
$customer_id = "11";
$query="SELECT * FROM `client_names`";// WHERE `customer_id` = '". $customer_id ."'
$result=mysql_query($query);
while( $res_array = mysql_fetch_assoc($result) ) {
echo "<option value=\"".$res_array['client_id']."\">".$res_array['client_id']."</option>";
}
?>
</select>
<div id="employ_data"><div>
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum