call function url page with ajax - php

I have tried several times but it didn't work. I want to call the function nama() on the dom_area.php page.
This is dom_area as target
<?php
function nama()
{
echo $_POST['data_area'];
}
?>
and this is the page where I call the function
<button id='acc'>asas</button>
<script>
$(document).ready(function() {
$('#acc').click(function() {
let data_area = 2;
let url_address = "<?= 'dom_area.php/nama()'; ?>";
$.ajax({
url:url_address,
type:'post',
data:{data_area:data_area} ,
}).done(function(output){
console.log(output);
// alert(data);
});
// console.log(url);
});
});
</script>

You can only cause a php file to be launched, NOT a specific function within that script
However if you pass a parameter to control what to do in the script you can achieve what you are trying to do
<button id='acc'>asas</button>
<script>
$(document).ready(function(){
$('#acc').click(function(){
let data_area = 2;
$.ajax({
url:'dom_area.php', // the script file name
type:'post',
// add a control parameter
data:{control:"Nama", data_area:data_area},
}).done(function(output){
console.log(output);
// alert(data);
});
// console.log(url);
});
});
</script>
Now in the PHP
<?php
function nama()
{
echo $_POST['data_area'];
}
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
// form was submitted
switch($_POST['control'])
case 'Nama':
nama();
break;
default:
echo json_encode(['status'=>'failed']);
}
}
?>

try this:
<?php include_once('dom_area.php') ?>
<button id='acc'>asas</button>
<script>
$(document).ready(function(){
$('#acc').click(function(){
let data_area = 2;
let url_address = "<?= json_encode(nama()); ?>";
$.ajax({
url:url_address,
type:'post',
data:{data_area:data_area} ,
}).done(function(output){
console.log(output);
// alert(data);
});
// console.log(url);
});
});
</script>

Please try this in call part.
<button id='acc'>asas</button>
<script>
$(document).ready(function(){
$('#acc').click(function(){
let data_area = 2;
$.ajax({
url:'dom_area.php/nama',
type:'post',
data:{data_area:data_area} ,
}).done(function(output){
console.log(output);
// alert(data);
});
// console.log(url);
});
});
</script>

Related

not returning anything in ajax

this is simple test for ajax and i want to send variable t in my index.php and get data(t) in my process.php and alert digit 15 when i click on button but my problem is not alerting anything
this is my index.php
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="jquery-3.2.1.min.js"></script>
<script>
$(document).ready(function () {
var t = 10;
$("#btn").click(function () {
$.ajax({
type:'post',
url:'process.php',
data:{
't':t
},
success:(function (response) {
alert(response);
}
})
})
})
</script>
<button id="btn">Click!</button>
this is my process.php
<?php
$res = $_POST['t'] + 5;
return $res
?>
Change codes like below:-
1.jQuery:-
$(document).ready(function () {
var t = 10;
$("#btn").click(function () {
$.ajax({
type:'post',
url:'process.php',
data:{'t':t},
success:function (response) { //remove (
alert(response);
}
});
});
});
2.Php:-
<?php
$res = $_POST['t'] + 5;
echo $res; //use echo rather than return
?>
Reason:-
return is used for returning a value from a function to another piece of PHP code.jQuery is not part of the execution of the PHP code on the server, so it has no idea what is really going on server side. jQuery is waiting for the rendered server response, which is what echo provides.
Note:- After doing These changes, Please check the browser console while running the ajax and see any error happen there? If yes share with us
i) Change your code with following code
$(document).ready(function () {
var t = 10;
$("#btn").click(function () {
$.ajax({
type:'post',
url:'process.php',
data:{
't':t
},
success:(function (response) {
alert(response);
}) //Close brace missing
})
})
});
2) Change return to echo
$(document).ready(function(){
$("#btn").click(function (){
var t = 10;
$.ajax({
type:'post',
url:'process.php',
data:{
t:t
},
success:(function (response) {
alert(response);
// you didnt close )
}) // close of success
})// close of ajax
})// close of click
}); // close of document
Process.php
<?php
$res = $_POST['t'] + 5;
echo $res;
?>
You should add jQuery like this
<script src="jquery-3.2.1.min.js"></script>
If you actually have downloaded and placed the file in the same directory.
You can include in from a CDN if you don't want to download it.
use this instead:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
And fix your JS code: (remove the extra '}' ...)
$(document).ready(function () {
var t = 10;
$("#btn").click(function () {
$.ajax({
type:'post',
url:'test.php',
data:{
't':t
},
success:(function (response) {
alert(response);
})
})
});
});

Error Ajax request

When I perform a simple ajax request I get the following error
TypeError: 'toString' called on an object that does not implement interface HTMLAnchorElement.
This is my ajax script
<script type="text/javascript">
$(document).on("ready", function(){
$("#tabla_persona table tr").click(function() {
var cod = $( "#identificador",this);
alert(cod.html());
var parametros={
"cod":cod
};
$.ajax({
type: 'POST',
url: '<?php echo site_url("archivo/prueba"); ?>',
data: parametros,
success: function(resp) {
$("#tabla_usuario_individual").html(resp);
}
});
});
});
</script>
This is my controller
public function prueba(){
$this->load->view('datos_persona');
}
and my simple page to see the result
<a>
Prueba
</a>
Try changing:
var parametros={
"cod":cod
};
to:
var parametros={
"cod":cod.html()
};
Just need simple change.I think the following code will work.
public function prueba(){
echo $this->load->view('datos_persona',true);
}

How to get the return value of a php function when calling from jQuery?

I have a php function return a string value which will put into html file.
function getDirectionInfo($routeNumber) {
//some code here
$dirinfo = "<p> some text </p>";
return $dirinfo;
}
if (isset($_POST['getDirectionInfo'])) {
getDirectionInfo($_POST['getDirectionInfo']);
}
So in jQuery, I have a following function
$(".onebtn").click(function(){
$("#directioninfo").empty();
var routeNumber = $(this).text();
$.ajax({
url: "./systemView_Function.php",
type: "POST",
data: {"getDirectionInfo": routeNumber},
success: function(data) {
console.log("HIHIHIHI");
$("#directioninfo").append(data);
}
});
})
Now console.log prints the "HIHIHIHIHI", but jQuery does not append the data to html. Anyone know how to get the return value of php function when calling from jQuery?
Instead of return use:
echo json_encode($dirinfo);
die;
It's also good idea to add dataType field to your $.ajax() function params set to json, to make sure, that data in your success function will be properly parsed.
You just need to send the response back using echo
Use var routeNumber = $(this).val(); to get the button value
PHP:
<?php
function getDirectionInfo($routeNumber) {
//some code here
$dirinfo = "<p> routeNumber". $routeNumber." </p>";
return $dirinfo;
}
if (isset($_POST['getDirectionInfo'])) {
echo getDirectionInfo($_POST['getDirectionInfo']);
}else{
echo "not set";
}
AJAX & HTML
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$(".onebtn").click(function(){
$("#directioninfo").empty();
var routeNumber = $(this).val();
console.log("routeNumber = " + routeNumber);
$.ajax({
url: "systemView_Function.php",
type: "POST",
data: {"getDirectionInfo": routeNumber},
success: function(data) {
console.log("data = " + data);
$("#directioninfo").append(data);
}
});
})
});
</script>
</head>
<body>
<div id="directioninfo"></div>
<input type="button" value="12346" class="onebtn" />
</body>
</html>
Thank you for everyone. I have just found that I made a very stupid mistake in jQuery. I should use var routeNumber = parseInt($(this).text()); instead of var routeNumber = $(this).text(); So the following code work to get the return value of php function when calling from jQuery.
in php
function getDirectionInfo($routeNumber) {
//some code here
$dirinfo = "<p> some text </p>";
echo json_encode($dirinfo);
}
if (isset($_POST['getDirectionInfo'])) {
getDirectionInfo($_POST['getDirectionInfo']);
}
in jQuery
$(".onebtn").click(function(){
$("#directioninfo").empty();
var routeNumber = parseInt($(this).text());
$.ajax({
url: "./systemView_Function.php",
type: "POST",
data: {"getDirectionInfo": routeNumber},
dataType: "JSON",
success: function(data) {
$("#directioninfo").append(data);
}
});
})

How can i load php file after sending ajax data

So I have 2 files file1.php with all the php and ajax, and file2.php with only php.
$_POST["there_id"] will be sent from file1.php page to file2.php through ajax.
file1.php code:
<div class="list_items">
<li><p>content here ...</p>
</div>
<div class="content_area">
//load ajax content here
</div>
$(".box").on("click", function(e) {
e.preventDefault();
var there_id = $(this).attr("id");
$.ajax({
url: "indexnew.php",
type: "POST",
data: { there_id : there_id}
});
});
file2.php code:
<div id="file2content>
<?php
$there_id = $_POST['there_id'];
$user_id = 5;
$fetch_data = regular_query("SELECT * FROM contents WHERE
(user_by = :me or user_by + :them) AND (user_to = :me or user_to = :them)",
["me" => $user_id, "them" = $there_id], $conn);
foreach ($fetch_data as $data) : ?>
<li><p>database data here</p>
<?php
endforeach;
?>
</div>
now what i want to do is when file2.php is done with php and list items are available i want to load file2contents to content_area on file1.php.
$(".box").on("click", function(e) {
e.preventDefault();
var there_id = $(this).attr("id");
$.ajax({
url: "indexnew.php",
type: "POST",
data: { there_id : there_id},
success: function(data) {
$('.content_area').html(data);
}
});
});
However, content_area should really be an id rather than a class so that it's unique.
Since you're just returning html, you can simplify it using the .load() function.
$('.box').on('click', function(e) {
e.preventDefault();
var there_id = $(this).attr('id');
$('.content_area').load('indexnew.php', {there_id: there_id});
});
success: function(data) { $(".content_area").html(data); }
adding the above to your AJAX call should do what you want if I understand you correctly.

execute php file in jquery after a time delay in sequence

i have one jquery function which i need to work in following way
1. call a php file
2. log the values return by php file
3. have time delay
again repeat the above process for number of time
for this purpose i write bellow jquery and php file
<script type="text/javascript">
$(document).ready(function() {
listsValues="1,10,20";
n=0;
msgids = listsValues.split(',');
$.each(msgids, function() {
html="TEST MESSAGE";
n++;
setTimeout(function() {
$.ajax({
type: "POST",
url: "test1.php",
cache:false,
data:"id="+ msgids[n],
dataType:'json',
success: function(json)
{
var foo = json.foo;
uemail = foo.split(',');
console.log(uemail)
}
});
}, 1000);
});
});
</script>
and here is test1.php
<?php
$id=$_POST['id'];
$val="";
for($i=1;$i<=$id;$i++) {
$val.=$i;
}
$return["foo"] =$val;
print stripslashes(json_encode($return));
?>
but this is not working as a way i want, when i execute this script
i can see in firebug test1.php file executes (called) for 3times and after that values are written in console
as i said waht i want was
1. execute test1.php file
2. write value in console
3. give delay
then repeat
Thanks
I think you are searching something like this:
<script type="text/javascript">
function request(n) {
$.ajax({
type: "POST",
url: "test1.php",
cache:false,
data:"id="+ msgids[n],
dataType:'json',
success: function(json)
{
var foo = json.foo;
var uemail = foo.split(',');
console.log(uemail);
setTimeout(
function() {
request(n);
}
, 1000
);
}
});
}
$(document).ready(function() {
var listsValues="1,10,20";
var n=0;
var msgids = listsValues.split(',');
$.each(msgids, function() {
var html="TEST MESSAGE";
n++;
request(n);
});
});
</script>

Categories