-
March 24th, 2007, 03:00 PM
#1
Posting In Ajax?
Alright I'm new to ajax but I wanted to know how to go about this using POST I can't find examples and the ones I look at show I'm doing this right but it doesnt seem to be working maybe someone can catch my error.
Index
Code:
<html>
<head>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
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 ajaxPlayers = document.getElementById('ajaxPlayers');
ajaxPlayers.innerHTML = ajaxRequest.responseText;
}
}
var limit = document.getElementById('limit').value;
ajaxRequest.open("POST", "players.php", true);
ajaxRequest.send(limit);
}
//-->
</script>
</head>
<body>
<form method=POST>
<table align='center'>
<tr>
<td>
Max Limit:
</td>
<td>
<input type='text' id='limit' onChange=''>
</td>
</tr>
<tr>
<td colspan=2>
<input type='button' value='Retrieve' onClick='ajaxFunction()'>
</td>
</tr>
</table>
</form>
<div id='ajaxPlayers'>Your Results</div>
</body>
</html>
players.php
PHP Code:
<?
include("config.php");
$limit = $_POST['limit'];
echo "limit: $limit";
$query = mysql_query("SELECT * FROM `accounts` ORDER BY `id` ASC LIMIT $limit");
echo("<table>");
while($row = mysql_fetch_array($query))
{
$id = $row['id'];
$username = $row['username'];
$password = $row['password'];
$email = $row['email'];
echo("
<tr>
<td>
$id
</td>
<td>
$username
</td>
<td>
$password
</td>
<td>
$email
</td>
</tr>
");
}
echo("</table> <br> Posts:");
print_r($_POST);
?>
I want to get the limit to pass through but it just doesnt post any ideas?
I know how to do it with GET but I want to learn how to do it with POST.
-
March 24th, 2007, 03:07 PM
#2
Re: Posting In Ajax?
Basically, you use send to pass a GET line.
Code:
var parameters = "limit=value&otheritem=itsvalue";
ajaxRequest.open('POST', url, true);
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", parameters.length);
ajaxRequest.setRequestHeader("Connection", "close");
ajaxRequest.send(parameters);
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 24th, 2007, 03:13 PM
#3
Re: Posting In Ajax?
thanks it worked.. but i dont understand whats the difference between what i did and yours?
-
March 24th, 2007, 03:15 PM
#4
Re: Posting In Ajax?
The key isn't that part. The key is the parameters you are passing. Notice my variable isn't the name or id of an HTML element. It is a formatted string.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 24th, 2007, 03:20 PM
#5
Re: Posting In Ajax?
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", parameters.length);
ajaxRequest.setRequestHeader("Connection", "close");
but that is still required... it doesnt work without it... is there a reason?
-
March 24th, 2007, 03:29 PM
#6
Re: Posting In Ajax?
You are missing my point. You are trying to pass var limit = document.getElementById('limit').value as your parameters. You need to make the parameters from your variable limit.
Code:
var limit = document.getElementById('limit').value;
var parameters = "limit=" + limit;
ajaxRequest.open('POST', url, true);
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", parameters.length);
ajaxRequest.setRequestHeader("Connection", "close");
ajaxRequest.send(parameters);
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 24th, 2007, 03:31 PM
#7
Re: Posting In Ajax?
I got the point lmao, I had tried that in previous attempts as well, but the reason it didnt work was because i didnt have the setRequestHeaders, and I don't really understand what that does.
-
March 24th, 2007, 03:37 PM
#8
Re: Posting In Ajax?
A header is what tells a page how to read the data. The headers here tell the page that it is form data to process. If we gave it a header of Content-type: application/pdf, it would have thought the data to be a PDF file.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 24th, 2007, 03:38 PM
#9
Re: Posting In Ajax?
I see... thank you very much.
-
March 24th, 2007, 03:38 PM
#10
Re: Posting In Ajax?
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|