Ajax call not passing php variables - php

I have php variables $subscriber_phone and $agent_phone on a page where i make an ajax call to a call.php page after clicking a button with ID #call. I have the following ajax code.
The alert for customernumber gives undefined. The php variable is not getting passed through.
Ajax:
<script>
$.ajaxSetup({ cache: false });
var customernumber;
var agentnumber;
$(document).ready(function(){
$("#call").click(function(){
customernumber: "<?php echo $subscriber_phone;?>";
agentnumber: "<?php echo $agent_phone;?>";
alert(customernumber);
$.ajax({
type: "POST",
url: "/Call_Agent/call/call.php",
cache: false,
dataType : "text",
data: {customernumber : customernumber,agentnumber : agentnumber},
success: function(data) {
alert('ok');
},
error: function(result) {
alert('error');
}
});
});
});
</script>
Is there anything I am doing wrong??? Requesting help!!

The issue is at
customernumber: "<?php echo $subscriber_phone;?>";
This is object syntax in a non-object context. Use the equal sign instead.
$("#call").click(function(){
customernumber = "<?php echo $subscriber_phone;?>";
agentnumber = "<?php echo $agent_phone;?>";
alert(customernumber);

Related

pass sessionid through jquery ajax call to php

JS in (start.php)
$(document).ready(function()
{
$('#btn_1').click(function(){
$.ajax({
type: "POST",
url: "get_data.php",
data: 'func=getData1',
success: function(msg){
$('#div_1').html(msg);
}
});
$('#div_1').show();
})
});
PHP (somename.php)
<?php
session_start();
if(trim($_POST['func']) == "getData1")
{
echo "Test";
}
?>
How can i pass the sessionid from start.php through my ajax to the get_data.php file ?
And how can pass the complete URL "url: "get_data.php," to the js-File so that i can switch the php-files, that should be called from ajax ?
Store Session ID in javascript variable and send it through ajax call, like this:
var session_id = '<?php echo session_id();?>';
Complete code should be:
var data = {func:'getData1',session_id:session_id};
$('#btn_1').click(function(){
$.ajax({
type: "POST",
url: "get_data.php",
data: data,
success: function(msg){
$('#div_1').html(msg);
}
});
$('#div_1').show();
})
Updates
If you want to access php variable in external js file, define variable before including js file. Like:
<script type="text/javascript">
var session_id = '<?php echo session_id();?>';
</script>
<script src="./ajax.js" type="text/javascript"></script>
$(document).ready(function()
{
$('#btn_1').click(function(){
$.ajax({
type: "POST",
url: "get_data.php",
data: {func:'fuc_name',session:'<?php echo session_id();?>'},
success: function(msg){
$('#div_1').html(msg);
}
});
$('#div_1').show();
})
});
var session_id = '<?php echo session_id();?>';
$('#btn_1').click(function(){
$.ajax({
type: "POST",
url: "get_data.php",
data: {func:"getData1","session":session_id},
success: function(msg){
$('#div_1').html(msg);
}
});
$('#div_1').show();
})
});
Use json encode. By using json you can pass the php data to js. Changing the code as per below. Set session id after session starts.
$(document).ready(function()`{
$('#btn_1').click(function(){ ` $.ajax({ `type: "POST",` dataType:"json", `url: "get_data.php,` data: 'func=getData1'`success: function(msg){ ` $('#div_1').html(msg.id); `}
});
$('#div_1').show();
})
});
in php side echo the variable with json encode.
echo json_encode($id); `
Use json encode. By using json you can pass the php data to js. Changing the code as per below. Set session id after session starts.
$(document).ready(function()
'{
$('#btn_1').click(function(){ `
$.ajax({
type: "POST",`
dataType:"json", `url: "get_data.php,`
data: {func:'enter the data you want to pass'},
success: function(msg){
$('#div_1').html(msg.id);
}
});
$('#div_1').show();
})
});
in php side echo the variable with json encode.
echo json_encode($id); `

ajax is not getting executed

Hi i have ajax in my project, in other files, but for this one particular instance where I call scripts/claim.php and pass in an id parameter via GET it doesn't seem to work.
HTML
<input type="hidden" id="claimid" value =<?php echo $fetch_donate['id'];?>>
<input type="button" onclick="processclaim();" class="btn" value="claim - <?php if($donate_type=='generic'){ echo $ebase;} else { echo $fetch_donate['ebase'];}?> PP"></div>
PHP
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['id'])) {
....
}
Javascript
<script>
function processclaim() {
alert("hi");
var id=document.getElementById('claimid').value;
alert(id);
$.ajax({
type: "POST",
url: "scripts/claim.php",
data: {id: id},
dataType: "json",
success: function(data) {
window.location = "profile.php";
}
});
alert(id);
}
</script>
the alerts work, displays "hi" and the correct id that is passed.
You are probably getting an error back from your server, add that state to the Ajax call
$.ajax({
type: "POST",
url: "scripts/claim.php",
data: {id: id},
dataType: "json",
success: function(data) {
window.location = "profile.php";
},
error: function() {
console.error("ERROR!", arguments);
}
});
My guess is you are setting the dataType coming back as JSON and it is not being returned from the server as JSON.

Pass php value with ajax with ajax post

I'm using an ajax script to post a value to a PHP file.
I'm not able to pass the variable.
My variable is declared in PHP and I would like to pass it with ajax.
Here is the variable and my button:
<?php $employee_id= '3'; ?>
<input class="btn btn-danger" type="submit" value="Delete" id="delete-btn">
This is the Javascript:
<script>
$(document).ready(function () {
$("input#delete-btn").click(function(){
$.ajax({
type: "POST",
url: "delete.php", //
data: {id: '$employee_id'},
success: function(msg){
$("#thanks").html(msg)
},
error: function(){
alert("failure");
}
});
});
});
</script>
Here is the PHP code where I want to receive the value:
if (isset($_POST['id'])) {
$emp_id = strip_tags($_POST['id']);
echo $emp_id;
$query = "DELETE FROM `employee` WHERE id='$emp_id'";
$result = mysql_query($query) OR die(mysql_error());
echo 'You successfully deleted the user.';}
I know I'm doing something wrong around the data...
That is because your variable is in php but you are not using php to attach your variable to your ajax, try wrapping your variable in php tags and then make sure you use 'echo' to print the value of your variable into javascript.
data: {id: <?php echo '$employee_id'?>},
Your javascript code, as far as the client will see, will end up looking like this for them:
data: {id: '3'},
They won't see the php code, they will just see the end result as their javascript.
You need PHP tags around your variables:
<script>
$(document).ready(function () {
$("input#delete-btn").click(function(){
$.ajax({
type: "POST",
url: "delete.php", //
data: {id: <?php echo '$employee_id'; ?> }, // <---
success: function(msg){
$("#thanks").html(msg)
},
error: function(){
alert("failure");
}
});
});
});
</script>

$.ajax not execute jquery code from php

I'm trying to execute a jquery code from a php page called by ajax.
Change html of div.error
Here's the code(If there is an easier way just tell/post):-no $.load
HTML/jQuery: jquery.js already linked
<input type='text' id='test'>
<div id='error' style='display:inline-block;'>CHANGE HERE</div><bR>
<input type='submit' value='run' id='button'>
<script type='text/javascript'>
$('#button').click(function(){
$.ajax({
url: "test.php",
type: "POST",
data: "test="+ test,
success: function(jqXHR){
},
error: function() {
alert('error');
}
});
});
</script>
test.php
<script type='text/javascript'>
$('#error').html('working');
</script>
The test variable that you are using doesn't seem to be declared anywhere. Try hardcoding first. Also the {key:value} syntax is preferred as it ensures to properly url encode the value when sending to the server:
data: { test: 'some value' }
and if you want to use a variable, make sure you have declared it:
$('#button').click(function() {
var test = 'foo bar';
$.ajax({
url: 'test.php',
type: 'POST',
data: { test: test },
success: function(data) {
},
error: function() {
alert('error');
}
});
});
Also if you want the javascript to execute in your php script remove the <script> tags:
$('#error').html('working');
and specify the dataType parameter to script in your $.ajax call:
dataType: 'script'
Another possibility is to have your test.php script simply return the value:
working
and then inside the success callback refresh the DOM:
success: function(data) {
$('#error').html(data);
},
The JavaScript from inside test.php won't run because you've never told it to. It's not just gonna run because you loaded it. It needs to be in the DOM to run.
Try this:
$.ajax({
url: "test.php",
type: "POST",
data: "test="+ test,
success: function(data){
$(data).filter('script').appendTo('head');
},
error: function() {
alert('error');
}
});
Your test.php script should output what you want to display.
it should look something like:
echo "<html>";
echo "<div>working</div>";
echo "</html>";
so that it returns something. Right now, you are returning only javascript.

How can I get data after click on text with AJAX jquery?

I used this code for sending and returning result.
<script type="text/javascript">
$(document).ready(function() {
$('.special').click(function(){
var info = $(this).attr("rel");
//$(this).html(sku);
$.ajax({
type: "POST",
url:"../ajax/addSpecialFlag.php",
async: false,
data: {info:info},
success:function(result){
$(this).html(result);
}});
});
});
</script>
<b style="cursor: pointer" class="special" rel="<?=$v['number']."/*".$v['vid']; ?>">Special</b>
addSpecialFlag.php
<?php
echo $_POST['info'];
?>
This code should return "Info" in "<-b class='special' ->" but no returning result. Where is the problem ? Thanks in advance !
The problem should be that $(this) inside your success-handler is not the same as outside of the handler. Doing it like this should solve your problem:
$(document).ready(function() {
$('.special').click(function(){
var $this = $(this);
var info = $this.attr("rel");
$.ajax({
type: "POST",
url:"../ajax/addSpecialFlag.php",
async: false,
data: {info:info},
success:function(result){
$this.html(result);
}});
});
});
<b>? Umad? Please use <strong>.
I have tidied your code so the paranthesis match their line indents and adjusted your success handler. The issue was the success handler is in a different scope to the click function scope so you needed to refer to it in another way, i.e. by assigning it to another variable.
$(document).ready(function () {
$('.special').click(function () {
var info = $(this).attr("rel");
var _this = $(this);
//$(this).html(sku);
$.ajax({
type: "POST",
url: "../ajax/addSpecialFlag.php",
async: false,
data: {
info: info
},
success: function (result) {
_this.html(result);
}
});
});
});

Categories