من احتیاج به یه مثال قابل فهم و یا توضیح راجب آژاکس Ajax در حالت POST دارم.
قسمت GET رو بلد هستم
واینکه نمیخوام غیر از جاوا و PHP چیزی داشته باشه ( مثل XML یا کدهای runtime )
(چجوری میشه با جاوا یه POST Request فرستاد؟ )
ممنون
من احتیاج به یه مثال قابل فهم و یا توضیح راجب آژاکس Ajax در حالت POST دارم.
قسمت GET رو بلد هستم
واینکه نمیخوام غیر از جاوا و PHP چیزی داشته باشه ( مثل XML یا کدهای runtime )
(چجوری میشه با جاوا یه POST Request فرستاد؟ )
ممنون
مسلما ممنون میشیم پاسخ رو ایجا بنویسید تا برای آیندگان مفید باشه :)
ایمیل من
سایت من
عضویت در جامعهی اهدای عضو
Direct PGP key: http://tinyurl.com/66q5cy
PGP key server: keyserver.ubuntu.com
PGP name to search: omidmottaghi
حتما جناب مدیر.
فرض میکنم آجاکس رو بشناسید در حد get
برای POST باید کاری کرد که بشه اطلاعات رو پست کرد البته من xml نمیخواستم ولی این روش از xmlhttprequest استفاده میکنه.
با کدزیر یک xmlhttprequest درست میکنیم:
<script language="javascript" type="text/javascript">
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object
خوب یک http داریم که برای ارسال اینجوری استفاده میشه:
http.open("POST",url,true);
http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
http.onreadystatechange = handleResponse;
http.send(data);
که data اطلاعاتی که میخوایم بفرستیم و url آدرسی که باید اطلاعات ارسال بشه
اینجا از تابع handleResponse استفاده کردیم برای چک کردن مراحل ارسال و دریافت پاسخ. که این تابع به این شکل هست:
function handleResponse() {
if (http.readyState == 4) {
result = http.responseText;
alert('result is: '+result);
}
}
برای حالت GET هم کافیه این تغییرات رو بدیم:
http.open("GET", str, true);
http.onreadystatechange = handleResponse;
http.send(null);
که str آدرس صفحه بهمراه اطلاعات میباشد.
بله اما من این کارو می کنم حروفی را که در فرم تایپ می کنم به صورت سرهم در دیتابیس ذخیره می کنه مثلا:
وقتی می نویسم: Ahmad Rezai در دیتابیس اینجوری ذخیره می شه: AhmadRezai
به نظر شما باید چه کار کرد؟
قسمتی از کتاب Ajax for Dummies
Passing Data to the Server with POST
When you pass data to a URL by using the POST method, it’s encoded internally
(in the HTTP request sent to the server), which makes sending data
more secure than with GET (although not as secure as using a secure HTTPS
connection to the server).
In the following sections, you see how using the POST method works.
Passing data by using the POST method in Ajax is a little different than using
GET. As far as the PHP goes, you can recover data sent to a PHP script by
using POST with the $_POST array, not $_GET. Here’s what that looks like
in a new PHP script, options3.php:
I’ve heard of rare PHP installations where $_POST wouldn’t work with Ajax
<?
header(“Content-type: text/xml”);
if ($_POST[“scheme”] == “1”)
$options = array(‘red’, ‘green’, ‘blue’);
if ($_POST[“scheme”] == “2”)
$options = array(‘black’, ‘white’, ‘orange’);
109 Chapter 3: Getting to Know Ajax
echo ‘<?xml version=”1.0”?>’;
echo ‘<options>’;
foreach ($options as $value)
{
echo ‘<option>’;
echo $value;
echo ‘</option>’;
}
echo ‘</options>’;
?>
applications when you use the POST method, in which case you have to use
$HTTP_RAW_POST_DATA instead. This technique gives you the raw data
string sent to the PHP script (such as “a=5&b=6&c=Now+is+the+time”),
and it’s up to you to extract your data from it.
How do you use the POST method in your JavaScript? It isn’t as easy as just
changing “GET” to “POST” when you open the connection to the server:
XMLHttpRequestObject.open(“POST”, url); //Won’t work by itself!
It isn’t as easy as that, because you don’t URL-encode your data when you
use POST. Instead, you have to explicitly send that data by using the
XMLHttpRequest object’s send method.
Here’s what you do. You set up the URL to open without any URL encoding
this way in the getOptions function, which is the function that communicates
with the server:
function getOptions(scheme)Then you configure the XMLHttpRequest object to use this URL. You do this
{
var url = “options3.php”;
.
.
.
}
by using the open method and by specifying that you want to use the POST
method:
function getOptions(scheme)To use the POST method, you should also set an HTTP header for the request
{
var url = “options3.php”;
if(XMLHttpRequestObject) {
XMLHttpRequestObject.open(“POST”, url);
110 Part II: Programming in Ajax
.
.
.
}
that indicates the data in the request will be set up in the standard POST way.
Here’s what that looks like:
function getOptions(scheme)Then you can connect an anonymous function to the XMLHttpRequest
{
var url = “options3.php”;
if(XMLHttpRequestObject) {
XMLHttpRequestObject.open(“POST”, url);
XMLHttpRequestObject.setRequestHeader(‘Content-Type’,
‘application/x-www-form-urlencoded’);
.
.
.
}
object’s onreadystatechange property as before to handle asynchronous
requests, as shown here:
function getOptions(scheme).
{
var url = “options3.php”;
if(XMLHttpRequestObject) {
XMLHttpRequestObject.open(“POST”, url);
XMLHttpRequestObject.setRequestHeader(‘Content-Type’,
‘application/x-www-form-urlencoded’);
XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
var xmlDocument = XMLHttpRequestObject.responseXML;
options = xmlDocument.getElementsByTagName(“option”);
listoptions();
}
}.
.
.
.
}
}
And now comes the crux. Instead of sending a null value as you would if you
were using the GET method, you now send the data you want the script to
get. In this case, that’s scheme = 1, like this:
function getOptions(scheme)
{
var url = “options3.php”;
if(XMLHttpRequestObject) {
XMLHttpRequestObject.open(“POST”, url);
XMLHttpRequestObject.setRequestHeader(‘Content-Type’,
‘application/x-www-form-urlencoded’);
XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
var xmlDocument = XMLHttpRequestObject.responseXML;
options = xmlDocument.getElementsByTagName(“option”);
listOptions();
}
}
XMLHttpRequestObject.send(“scheme=” + scheme);
}
}
There you go. Now this new version of the Ajax application, options3.
html, will use the POST method to send its data to options3.php, which
will return its data in XML format. Very neat.
If you want to use XML to send your data to the server-side program, the
POST method works, too. That’s because you don’t have to explicitly encode
the data you send to the server yourself, appending it to the end of an URL.
(Some servers have limits on how long URLs can be.)
To send your data as XML, you set a Request header so that the content type
of your request will be “text/xml” instead of “application/x-wwwform-
urlencoded”:
XMLHttpRequestObject.setRequestHeader(“Content-Type”, “text/xml”)
Then you can send your XML directly to the server by using the send
method, which goes something like this:
XMLHttpRequestObject.send(“<doc><name>limit</name><data>5</data></doc>”);
بابا یکی جواب ما رو بده؟
چه کارکنم من دارم از طریق روش پست آژاکس اطلاعات فرمم رو داخل بانک می ریزم اما تمامی حروف به هم چسبیده می شه و بعد داخل بانک ریخته می شه!!!!!!!!!!!
مثلا: Razi Emami اینجوری ثبت می شه: RaziEmami؟
فکر می کنید مشکل چیه؟
سلام .
من نیز برای ارسال از روش POST استفاده می کنم . اما در صفحه URL متغیرهای ارسالی را نمی شناسد . کد مورد استفاده را هم می گذارم لطفاً مشکل کار من را بگویید :
کد ایجاد XMLhttpRequest :
کد دریافت اطلاعات :
function getHttp()
{
var xmlHttp;
try
{
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")
}
catch(e)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
}
}
return xmlHttp;
}
کد ارسال اطلاعات :
var httpData=getHttp();
function AnswerData()
{
if(httpData.readystate==4)
{
if (httpData.status == 200)
alert(httpData.responseText);
//eval(what);
else
alert('There was a problem retrieving the XML data: ' +httpData.responseText);
}
function sendData()
{
stData='Data=1&MyName='+txtName.value+';
httpData.open('POST','MyWeb/InsertPage.php', false);
httpData.onreadystatechange = AnswerData;
httpData.setRequestHeader('Content-Type', 'application/x-www-formurlencoded; charset=UTF-8;');
httpData.send(stData);
}
سلام.ببخشید من می خوام با آژاکس و php و javascript کار کنم .این کدهای بالا مربوط به چه زبانی است؟ میشه مثالی از ثبت کردن در mysql (نرم افزار wamp)را برای ما بزنید.
ممنون.
wamp یا xamp یا easyPHP هیچ فرقی ندارند .
من خودم نسبت به اهمین پروژه به ترتیب از prototypr و mootools استفاده می کنم که اینجور دردسر هارو هم نداره .
با سلام خدمت عزیزان عزیز!
من تمامی موارد بالا رو رعایت کردم در حالت POST
اما از توی فرم چطور باید اطلاعات رو ارسال کنم؟
فایل js رو توی Header که یک فایل جدا هست فراخانی میکنم. مقدار Action از فرم ارسال اطلاعات رو هم به این صورت مینویسم ولی کاری انجام نمیده و اطلاعاتی ارسال نمیکنه
<form action="javascript:newdata()" method="post" >
دو روزه الاف همین موضوع هستم. اولش از سایت W3school یکم راجع به Ajax خوندم کلی خوشحال شدم که آسونه حالا گیر کردم دارم ناامید میشم که :D
قریب به 99.99% از مثال های نت رو هم خوندم ولی چیزی که میخواستم نبود
یکی از بزگواران میتونه این کدی که میخوام رو بنویسه
یک فرم ارسال اطلاعات. یک تکست محتوای یک مقداری حالا هر چی. و ارسال
و نهایتاً ارسال همین یک تکست باکس به یک فیلدی به دیتابیس. کد فرم و ای جکسش رو میخوام :(
PLEASE HELP ME خدا یک در دنیا صد در آخرت به شما عنایت بفرماید.
bodyContent = $.ajax({
url: "script.php",
global: false,
type: "POST",
data: ({id : this.getAttribute('id')}),
dataType: "html",
async:false,
success: function(msg){
alert(msg);
}
}
).responseText;
ممنون که وقت گذاشتید اما من چیزی نفهمیدم از این کد. کاملاً جدید بود برام!!!
ببینید من یک فایل AjaxFunction.js دارم. که تابع زیر رو توش نوشتم.
function newdata()و فایل PHP هم به این صورته.
{
var xmlhttp;
document.getElementById('response').innerHTML = "Just a second..."
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('insert_response').innerHT ML = 'new class added'
}
}
xmlhttp.open("get","newdata.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var text1 = document.getElementById('text1').value;
params="text1="+text1;
xmlhttp.send(params);
}
<?php
require("Header.php");
print '
<form action=" javascript:newdata() " method="post" >
<input type="text" id="text1" value="somevalue" />
<input type="hidden" id="action" value="insert" />
<input type="submit" />
</form>';
if($_POST['action']=='insert')
{
// SEND TO DATABASE...
}
require("Footer.php");
?>
من فایل AjaxFunction.js رو توی فایل Header.php فراخونی کردم. مشکل من اینجاست که نمیدونم اینکار درسته یا نه. و اینکه خب اگر درست باشه. چطور باید به تابع فایل js دسترسی داشته باشم. توی قسمت فرم که داخل فایل PHP نوشتم ( منظورم این قسمته ===><form action="javascript:newdata()" method="post" >این قسمت درسته؟
به این صورت که من نوشتم اصلاً تابع جاوااسکریپت از فایل js ( که توی هدر فراخونی کردم ) رو نمیشناسه.
من چه باید بکنم؟
به نظر من شما برعکس کار میکنی
شما صفحه خودتو ظراحیکن و فکر کن کلا سمت کلاینتکار می کنی بعد با جاوا اسکریپت+ آجاکس یک صفحه صدا بزن
و فکر کناون صفحه یک تابه هست که شما بهش ورودی میدی بعدخروجی و بازم سمت کلاینت پردازش میکنی
راستیاون کد اصلا چیز جدیدی نبود با jquery انجام بدید
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" >
$(function(){
$('#run').bind('click',function(){
$.ajax
({
type: "POST",
url: "ajax.php",
data: "num="+$("#number").val(),
success: function(result)
{
alert(result);
}
});
});
});
</script>
</head>
<body>
<div id="result" >
<input type="text" id="number" />
<input type="button" id="run" value="run"/>
</div>
</body>
</html>
<?php
$a = $_POST['num'];
echo ($a*2);
?>