简体   繁体   中英

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!

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.

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']

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM