0

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!

Karb
  • 327
  • 3
  • 12
  • 2
    Try checking your web developer tools to see if the ajax is posting and what response you are getting. – chrislondon Jul 03 '13 at 15:07
  • Do you have jQuery lib included? – mishik Jul 03 '13 at 15:07
  • Also, specify the contentType for the $.ajax function. By default it sends it as `application/x-www-form-urlencoded; charset=UTF-8`. You need to use `application/json` or something of the like. – GJK Jul 03 '13 at 15:10
  • I've done it already, ajax is posting and I do get the exact value i need, And yes, Jquery included – Karb Jul 03 '13 at 15:11
  • Where do you expect output to occur here and where are you looking for it exactly? – deceze Jul 03 '13 at 15:13
  • is secureRide.php an example or is maybe the page on another domani? in this case you'll have to use jsonp for the request – nowhere Jul 03 '13 at 15:13
  • Do you firebug installed? Can you see the request going out and the response coming back? – Pitchinnate Jul 03 '13 at 15:14
  • secureRide.php in not example, it's a real page i use, it's in the same domain, in the same folder.How i can do it with jsonp? may be it will help me – Karb Jul 03 '13 at 15:17
  • http://stackoverflow.com/questions/11736431/make-cross-domain-ajax-jsonp-request-with-jquery . Last time i had a problem like yours this solved everything. – nowhere Jul 03 '13 at 15:22
  • do `var_dump($_SESSION);` what it gives ? – Moeed Farooqui Jul 03 '13 at 18:58

1 Answers1

1

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'];
  }        
BenMorel
  • 34,448
  • 50
  • 182
  • 322
MIlan Modh
  • 95
  • 9