I have some problem with passing value from javascript file to php file.
I make some calculations in JS, and after that i passing it to php.
This is a code of JS:
var price=100;// for example
$.ajax({
type: "POST",
url: "SecureRide.php",
data: { calculatedPrice: price }
});
And this is a code of PHP:
<?php
session_name("ridePrice");
session_start();
$_SESSION["calculatedPrice"]=$_POST["calculatedPrice"];
echo $_SESSION["calculatedPrice"];
?>
So what i do is a simple Ajax function that passes the value to php, very simple, but doesn't work!
When I do echo function there's nothing there.
Maybe there are another way to solve it?
Thank you very much!
Note : if you are put your ajax code in function and than call function from the "$(document).ready(function(){" then your code run perfectly
I write code in your .js file :
$(document).ready(function(){
functionName(price);
});
function functionName(price){
$.ajax({
url: 'SecureRide.php',
data:{calculatedPrice: price},
type: 'POST',
success:function(data)
{
alert(data);
},
error:function(data)
{
alert("mistake in your code");
}
})
}
And Code For PHP file for get .JS File Data
if(isset($_POST['calculatedPrice']) && !empty($_POST['calculatedPrice']))
{
$calculatedPrice1=$_POST['calculatedPrice'];
}
Related
Just playing around with ajax and php and I have a simple question.
These are the following relevant files.
file.php
<?php
$bla = $_GET['pid'];
echo $bla;
?>
HTML
HTML Code of example site URL: somesite.com/blabla.php?pid=3
(It contains a single button which when you click it is supposed to get the $_GET value from the URL which is 3)
<!DOCTYPE html>
<html>
<head>
<title>Some Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<button class="fa fa-icon" onclick="someFunction()"></button>
</body>
</html>
JS:
<script>
function someFunction(){
$.ajax({
method: 'POST', //I've tried 'GET' here too. doesnt make a difference
url: "file.php",
success: function(result)
{
alert(result);}
});
}
</script>
As noted by the commenters below: I've also tried the following
<script>
function someFunction(){
$.ajax({
method: 'POST', //I've tried 'GET' here too. doesnt make a difference
url: "file.php",
data: {pid: 3}, // added this line
success: function(result)
{
alert(result);}
});
}
</script>
The alert is blank there is no 3 on it when the php file has echo $bla which is $_GET['pid'] and the pid in the url is = 3.
Can someone please explain why that is the case? I dont think I understand what is happening behind the scenes of the above codes to figure out why.
Please note that I'm not trying to solve a particular problem or anything just trying to understand why $_GET is not working in this very specific case.
You're not sending any data in the AJAX request, hence there's nothing for the PHP to read and send back. To fix this include a data property in the request which contains the pid key and the required value, for example:
function someFunction(){
$.ajax({
method: 'GET',
url: "file.php",
data: {
pid: 3
},
success: function(result) {
console.log(result);
}
});
}
Note that I'm using method: 'GET' here, which is important as you're using $_GET in your PHP, so a POST request won't work. Also note that I'm using console.log() to debug the value. alert() shouldn't be used for debugging as it coerces data types.
I got this function in my html for a simply validation...
<script>
function pwdcheck(dataPwd) {
var parametros = {
"dataPwd" : dataPwd
};
$.ajax({
data: parametros,
url: 'check.php',
type: 'post',
beforeSend: function () {
$("#infomsg").html("checking, please wait...");
},
success: function (response) {
$("#infomsg").html(response);
}
});
}
and this check.php
<?php
$clave = 'cocacola';
if(trim($_POST['dataPwd'])==$clave) {
// redirect to some page
}else{
echo "Incorrect Password!";
}
?>
and the problem is that when I use it directly from this first html that I showed you... all works perfect, but when I load this html inside a div (#section), its stop working displaying the "checking, please wait..." message.
Someone could please help me?! Thanks!
Well I solved the problem! Thanks to all! you gave me ideas to try some things step by step...
The problem was that the php file was in another directory... and ajax seems to arrive in one way but php cant find a way to return I think... php echo dont cross directories :P
What I tried was this.
I had main.html who loads in his #section validate.html who was the one who has the ajax inside to communicate with check.php, validate.html and check.php were in another directory.
The solution was only moving the check.php to the same directory than main.html, and it worked even keeping validate.html in the other directory... weird no?! at least for me that I dont know so much :P
Thanks again to all, Leandro.
<script>
function pwdcheck(dataPwd) {
var parametros = {
"dataPwd" : dataPwd
};
$.ajax({
data: parametros,
url: 'check.php',
type: 'post',
dataType:'html',
beforeSend: function () {
$("#infomsg").html("checking, please wait...");
},
success: function (response) {
$("#infomsg").html(response);
}
});
}
</script>
I have a function.I want Data passing through ajax is store in php variable.I tried below code but not work please some one help me.
function moreinfo(prodid,catid,price,type,catname) {
url2="<?php echo $this- >getUrl('compatibility/compatiblelist/moredetails'); ?>";
$j.ajax({
url:url2,
type: 'POST',
data: {"prodid": prodid},
success: function(response) {alert(console.log(response));}
});
<?php
$ms = $_POST["prodid"];
echo $ms;
?>
}
<?php ?> tags only work when file name must be .php.
may you are using that process in .js file kindly change the ext
consider separating the file, .js and .php then you can include .js file
function moreinfo(prodid,catid,price,type,catname) {
url2="something.php";
$.ajax({
url:url2,
async:false,
type:'POST',
data: {prodid: prodid},
dataType:'html',
success: function(response) {alert(console.log(response));}
});
}
In your something.php
<?php
$ms = $_POST["prodid"];
echo $ms;
?>
I wanna pass a JavaScript variable in to php using Ajax... coz I wanna pass it onclick. I'm getting the onclick value to variable row. Help would b gratefull :) Thank You
<script type="text/javascript">
function updateTable(row) {
alert (row);
var row = (row);
}
</script>
Did u try this?
$.ajax({
url: 'do.php',
type: 'POST',
data: "userID=" + onlyID,
success: onMemberSucces,
error:onMemberError
});
function onMemberSucces(data) {
alert(data);
}
function onMemberError() {
alert("Error!!");
}
where do.php is your php file and onlyID is your javascript variable.
Is this what you were looking for?
I have a PHP program for counting user banner clicks. My banner link is something like this:
<a href="<?=$banner_url;?>" onclick="banner_click_count('<?=$banner_id;?>')"><img src=...>
When user clicks on image, it runs banner_click_count() function with $banner_id as parameter.
function banner_click_count($ban_id)
{
$.ajax({
type: "POST",
url: 'banner_click.php',
data: {banner_id: $ban_id}
});
}
At banner_click.php, I get the banner_id with $banner_id = $_GET['banner_id']);, search the database based on it. Find the record, then add 1 to banner_count column field. After that, redirect to banner_url.
When I run the program, I get Parse error: parse error, expecting T_VARIABLE' or '$'' on line $.ajax({
Addendum: the error is cleared with all your help, but when I click on the link it redirects to banner_url directly and does not run the AJAX function.
Addendum:I put the alert("hello"); at the top of ajax function and i got it. So it goes into function
1.You need to put your javascript function under <script> tag
2.you need to pass json string as post data
3.though you are passing your data as post so you will get this data in php as $_POST not $_GET
So change your function as below
<script>
function banner_click_count(ban_id)
{
$.ajax({
type: "POST",
url: 'banner_click.php',
data: {banner_id: ban_id}
});
}
</script>
// in your php use as below
echo $_POST['banner_id']
Make sure banner_id is in quotes and that you are including JQuery in your page.
And don't forget a success/error return.
$.ajax({
type: "POST",
url: 'banner_click.php',
data: {'banner_id': $ban_id},
success: function(s) {
console.log('success' + s);
},
error: function(e) {
console.log('error' + e);
}
});
Don't we need a return false before the function ends?
I found the solution. Thanks to all.
function banner_click_count(ban_id)
{
$.post(
"banner_click.php",
{
banner_id: ban_id
});
}