PDA

View Full Version : سوال: ajax



rash44
یک شنبه 18 مرداد 1388, 11:54 صبح
سلام
می خوام زمانی که کاربر هر یک از گزینه های compbox رو انتخاب کرد متن مربوط به همون گزینه از بانک خونده بشه و توی textarea نمایش داده بشه
میدونم که اینکار باید با ajax انجام بشه
لطفا یکی از دوستان اینکار رو برام انجام بده
ممنون



<table border="0" width="100%" dir="rtl" cellpadding="3">
<form method="POST" name="form">
<tr>
<td>
<table border="0" width="100%">
<tr>
<td width="67" bgcolor="#F9F9F9">
<font style="font-size: 9pt; font-weight:700" face="Tahoma" color="#003366">
نوع صفحه :</font></td>
<td>
<select size="1" name="type" class="inputtext">
<option selected value="1" style="background-color: #f9f9f9">صفحه اول وب سایت</option>
<option value="2" style="background-color: #ffffff">صفحه شخصی کارآموز</option>
</select></td>
</tr>
<tr>
<td width="67" bgcolor="#F9F9F9">
<font style="font-size: 9pt; font-weight:700" face="Tahoma" color="#003366">
عنوان :</font></td>
<td>
<textarea rows="3" name="title" cols="27" dir="rtl" class="inputtext"></textarea></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<p align="center">
<input type="submit" value="ثبت" name="button" class="button" onclick="return checkform();"></td>
</tr>
</form>
</table>




<?php
$type=$_POST['type'];
require_once('db.php');
$conect = db_connect();
$sql="SELECT `text` FROM `table` WHERE `type`='$type'";
$result = db_query($sql);
$table=mysql_fetch_array($result);
$text=$table['text'];
?>

Bandeye_khoda
یک شنبه 18 مرداد 1388, 15:57 عصر
با سلام
دوست عزیز اینم کدی که میخواستی

test.htm


<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<script type="text/javascript" src="ajax.js"></script>
<title>JavaScript about:blank context</title>
</head>
<table border="0" width="100%" dir="rtl" cellpadding="3">
<form method="POST" name="form">
<tr>
<td>
<table border="0" width="100%">
<tr>
<td width="67" bgcolor="#F9F9F9">
<font style="font-size: 9pt; font-weight:700" face="Tahoma" color="#003366">
نوع صفحه :</font></td>
<td>
<select size="1" name="type" id="type" class="inputtext" onchange="javascript:GetText()">
<option selected value="0" style="background-color: #f9f9f9"></option>
<option value="1" style="background-color: #f9f9f9">صفحه اول وب سايت</option>
<option value="2" style="background-color: #ffffff">صفحه شخصي کارآموز</option>
</select></td>
</tr>
<tr>
<td width="67" bgcolor="#F9F9F9">
<font style="font-size: 9pt; font-weight:700" face="Tahoma" color="#003366">
عنوان :</font></td>
<td>
<textarea rows="3" name="title" id="title" cols="27" dir="rtl" class="inputtext"></textarea></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<p align="center">
<input type="submit" value="ثبت" name="button" class="button" onclick="return checkform();"></td>
</tr>
</form>
</table>
</body>
</html>


ajax.js


function createXMLHttp(){
if (typeof XMLHttpRequest != "undefined") {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
var aVersions = [ "MSXML2.XMLHttp.5.0",
"MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0",
"MSXML2.XMLHttp", "Microsoft.XMLHttp" ];
for (var i = 0; i < aVersions.length; i++) {
try {
var oXmlHttp = new ActiveXObject(aVersions[i]);
return oXmlHttp;
} catch (e) {} }
} throw new Error("XMLHttp object could not be created."); }

function GetText()
{
var oXmlHttp= createXMLHttp();
var type;
type=document.getElementById('type').value;
if (type==0){
showdata('');
return;
}
type = 'type='+ escape(encodeURI(type))+"&";
oXmlHttp.open("POST","gettype.php",true);
oXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
oXmlHttp.setRequestHeader("Content-length", type.length);
oXmlHttp.setRequestHeader("Connection", "close");
oXmlHttp.onreadystatechange = function() {
if (oXmlHttp.readyState == 4) {
if(oXmlHttp.status == 200){
showdata(oXmlHttp.responseText);
} else {
showdata("Error: " + oXmlHttp.statusText);
}
}
};
oXmlHttp.send(type);
}
function showdata(Cdata){
document.getElementById('title').value = Cdata ;
}



gettype.php


<?php
$type=urldecode($_POST['type']);

/*
require_once('db.php');
$conect = db_connect();
$sql="SELECT `text` FROM `table` WHERE `type`='$type'";
$result = db_query($sql);
$table=mysql_fetch_array($result);
*/

if ($type==1)
echo 'صفحه اول';
else
echo 'صفحه کارآموز';
?>


فقط چند تا نکته حتما توی کنترل ها مقدار ID رو برابر با NAME تعریف کن (واسه firefox(
و ورودی کاربر رو برای sql injection و xss بررسی کن

موفق باشی

اینم فایل زمیمه

rash44
یک شنبه 18 مرداد 1388, 18:34 عصر
سلام
آقا ممنون
حالا اگر بخوام این کار رو برای چند تا فیلد انجام بدم همراه با 1 لودینگ باید به چه صورت عمل کنم ؟
مثلا 1 user name وارد کنم و با کلیک بر روی 1 دکمه اطلاعات username توی 1 سری فیلد چاپ بشه

Bandeye_khoda
دوشنبه 19 مرداد 1388, 02:02 صبح
متونی با تغییر در کدهایی که فرستادم و خلاقیت این کار رو به راحتی انجام بدی