[英]Need help in passing value form JS to PHP
I have some problem with passing value from javascript file to php file. 我在将值从javascript文件传递到php文件时遇到一些问题。 I make some calculations in JS, and after that i passing it to php. 我在JS中进行了一些计算,然后将其传递给php。 This is a code of JS: 这是JS的代码:
var price=100;// for example
$.ajax({
type: "POST",
url: "SecureRide.php",
data: { calculatedPrice: price }
});
And this is a code of PHP: 这是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! 因此,我要做的是一个简单的Ajax函数,该函数将值传递给php,非常简单,但是不起作用! When I do echo function there's nothing there. 当我执行echo功能时,那里什么也没有。 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 注意:如果将ajax代码放入函数中,然后从“ $(document).ready(function(){”)调用函数,则您的代码可以完美运行
I write code in your .js file : 我在您的.js文件中编写代码:
$(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 和代码为PHP文件获取.JS文件数据
if(isset($_POST['calculatedPrice']) && !empty($_POST['calculatedPrice']))
{
$calculatedPrice1=$_POST['calculatedPrice'];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.