Pass php value with ajax with ajax post - php

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>

Related

Ajax call not passing php variables

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);

Problem in calling a variable from server to client

I want to learn ajax.
Basically I begin to understand it, but I have one problem
here is my js code
$('#click').on('click',function () {
var a="good";
$.ajax({
url:'/ggg.php',
method:'post',
data:{
info:a
},
success:display_data
})
});
here is my html code
<input type="submit" value="do it" id="click">
here is my php code
<?php
echo $_POST['info'];
that's what I see in console.log
and the word "good" should appear
Use type instead of method. Also success might be a function.
$('#click').on('click',function () {
var a="good";
$.ajax({
url:'/ggg.php',
type: 'post',
data:{
info:a
},
success: function (data) {
//document.write(data)
console.log(data)
}
})
});
Reference

Send values to PHP

I have a button within my HTML that, when you click it, I need to have it run a query in a PHP file which would then echo the results.
I've tried the following, however, when I click the button, it does nothing. What's wrong?
HTML/Ajax:
<?php
$reply_id = 15;
?>
<html>
<body>
<center><a class="btn show-more">Show more comments</a></center>
<div id="resultcomments"></div>
<script type="text/javascript">
var id = $reply_id;
$(document).ready(function() {
$(".show-more").click(function() {
$.ajax({
url: 'assets/misc/test.php',
type: "POST",
data: ({id_variable: id}),
function(data) {
$("#resultcomments").html(data);
}
});
});
});
</script>
</body>
</html>
PHP (located in assets/misc/test.php):
<?php
$replyid= $_POST['id_variable'];
echo $replyid;
// query would go here
?>
since $reply_id is PHP variable, for using it inside javascript you need to do like:
var id = <?php echo $reply_id; ?>;
....
and change:
data: ({id_variable: id})
to
data: {id_variable: id}
like:
$.ajax({
url: 'assets/misc/test.php',
type: "POST",
data: {id_variable: id},
success: function(data) {
$("#resultcomments").html(data);
}
});
You're not assigning your AJAX success function to a key, so I don't think it's ever firing. (You could try a console.log() or a debugger to check.) Try this instead:
$.ajax({
url: 'assets/misc/test.php',
type: "POST",
data: {id_variable: id}
}).done(function (data) {
$("#resultcomments").html(data);
});
Try this in javascript
var id = <?php echo $reply_id; ?>;
No brackets needed for data attribute in ajax,
data: {id_variable: id},
Try this for data attribute

ajax request not going through javascript

trying to send POST request to script via ajax, not calling the script
<script>
function claimitem() {
var val=document.getElementById('itemid').value;
alert(val); //shows as 4 correct
$.ajax({
type: "POST",
url: "scripts/claim.php",
data: {id: val},
dataType: "json",
success: function(data) {
alert("success"); //does not show
}
});
window.location = "profile.php";
}
</script>
here i am calling the javascript function
<input type="submit" onclick="claimitem()" class="btn" value="claim - <?php echo $ebase;?> PP">
my claim.php looks like this what is wrong with my ajax call???
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['id'])) {
...
}
When you call $.ajax, you're starting an asynchronous task. This task, involving a request, doesn't stop the script so the following line is immediately executed.
As the following line replaces the page, it ends the script, including the ajax request. You must call this line in the callback to let the time for the ajax request :
$.ajax({
type: "POST",
url: "scripts/claim.php",
data: {id: val},
dataType: "json",
success: function(data) {
alert("success"); //does not show
window.location = "profile.php";
}
});
code looks ok better add some alert to error as if some problem with your ajax helper.
something like.
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
Hope you have added this to your head tag
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
then try to change input type="button"
<input type="button" onclick="claimitem()" class="btn" value="claim - <?php echo $ebase;?> PP">

$.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.

Categories