Reputation: 165
i want to set url parameter of ajax to a function of class and return the value of that function but my problem it seems cant access to function and nothing returns
this is my script:
$('#send').click(function (e) { //event added
e.preventDefault(); //prevent normal for submit
$.ajax({
type:'post',
url:'localhost/shapeproject/controllers/Square/getArea',
data:{'edge':edge},
success:(function (response) {
alert(response);
})
});
});
this is my class:
<?php
class Square extends Shape
{
private $edge;
function __construct()
{
$this->edge = $_POST['edge'];
}
public function getArea()
{
return $this->edge * $this->edge;
}
public function getPerimeter()
{
return $this->edge * 4;
}
}
Upvotes: 0
Views: 56
Reputation: 106
you can add a page and name it ajax_handle.php. then link to that page in your ajax code like this
$('#send').click(function (e) { //event added
e.preventDefault(); //prevent normal for submit
$.ajax({
type:'post',
url:'ajax_handle.php',
data:{'edge':edge, getArea: true},
success:(function (response) {
alert(response);
})
});
});
now in your ajax_handle.php you can write this code.
if(isset($_POST['getArea']) && $_POST['getArea'] == true)
{
$obj = new Square();
$obj->getArea();
}
Upvotes: 1
Reputation: 316
You must have a front controller to manage this process.
For example: create a file called index.php
inside shapeproject
directory.
Then you can extract controller and function names from $_SERVER['REQUEST_URI']
.
You may need to use some .htaccess
directives like the example below:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
Read more about front controllers: An Introduction to the Front Controller Pattern
Upvotes: 0
Reputation: 10202
but you don't set any data... Add data
to the jquery function;
$.ajax({
type:'post',
url:'localhost/shapeproject/controllers/Square/getArea',
data: { edge: 'xyz' },
success:(function (response) {
alert(response);
})
});
Upvotes: 1