PDA

View Full Version : سوال: اجرای دو تابع php توسط آژاکس



IMANAZADI
دوشنبه 10 شهریور 1393, 17:12 عصر
با سلام
یک مشکلی واسم پیش اومده
در یک صفحه دو button داریم
میخوام زمانیکه وقتی button 1 کلیک شده تابع a در class.php اجرا بشه و خروجی چاپ بشه و وقتی button 2 کلیک شده تابع b در class.php اجرا بشه و خروجی چاپ بشه
فقط با استفاده از آژاکس (توجه فقط جاوا اسکریپت نه جی کوئری)


صفحه html


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript">


function ajax()
{
var xmlhttp=null;
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

url='class.php';

xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('output').innerHTML=xmlhtt p.responseText;
}
}

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


}


</script>
</head>


<body>


<input type="button" value="button 1" onClick="ajax()">
<input type="button" value="button 2" onClick="ajax()">


<div id="output"></div>
</body>
</html>





فایل class.php


<?php
function a()
{
echo 'a';
}


function b()
{
echo 'b';
}
?>

saeedvir
سه شنبه 11 شهریور 1393, 07:27 صبح
صفحه html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript">


function ajax(getinpt)
{
var xmlhttp=null;
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

url='class.php';

xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('output').innerHTML=xmlhtt p.responseText;
}
}

if (getinpt==1) {
xmlhttp.open("GET",url+'?num=1',true);
}

if (getinpt==2) {
xmlhttp.open("GET",url+'?num=2',true);
}

xmlhttp.send();


}


</script>
</head>


<body>


<input type="button" value="button 1" onClick="ajax(1)">
<input type="button" value="button 2" onClick="ajax(2)">


<div id="output"></div>
</body>
</html>


فایل php :


<?php

$x = $_GET['num'];
$x=intval($x);


function a()
{
echo 'a';
}


function b()
{
echo 'b';
}


if ($x ==1) {
a();
}elseif ($x ==2) {
b();
}else{
die('Hi !');
}

?>