nsilva
nsilva

Reputation: 5614

Passing jQuery Value to PHP

I am calculating the distance between two places using jQuery and I want to pass this value (pickup_distance) for use in PHP.

I am wanting to send this value to tariff.fare.controller.php for use in the following function:

private static function getFare($int_terminate) {

    // Function

    if pickup_distance(<-- jQuery Value) > 5 {

        // Do Something

    }

}

How could I go about doing this? I'm aware I can do this via AJAX but being quite new to programming I'm not quite sure how I can do this.

Any help would be much appreciated!

Upvotes: 1

Views: 11877

Answers (3)

Naryl
Naryl

Reputation: 1888

using Jquery it's quite easy:

var your_var_value=1200;
$.post("your_php_script.php", {var_value: your_var_value}, function(data){
    alert("data sent and received: "+data);
});

then in your PHP script you get the variable like this:

$distance=$_POST['var_value'];

And what you echo in "your_php_script.php" is returned as the data variable.

Upvotes: 4

Husman
Husman

Reputation: 6909

Best way to do it is to use jQueries built in Ajax functionality. Either use .ajax or .post and send in your required parameters to your PHP script.

Upvotes: 1

Iesus Sonesson
Iesus Sonesson

Reputation: 854

As jQuery is client side code and PHP is Server side code, the variables must somehow be passed to the server.

There are a few decent ways of doing this, by far the most common is GET and POST variables. then you can pick them up in php and do whatever you wish with it.

A very simple example is to have an iframe/php image whatever and just load the src with JavaScript or jquery file.php?EEEE=YYY then fetch that variable $_GET['EEEE']

Upvotes: 1

Related Questions