简体   繁体   中英

Passing parameters to a PHP file through URL using javascript

The data we want to send: parameter code with value A123, parameter price with value 12.45, and parameter date with value 12-3-2016. Assume that the server-side data processing file is called computePrice.php, and the domain of the server is www.mydomain.com.

My guess is www.mydomain.com?code=A123&price=12.45&date=12-3-2016

You can take those URL values using $_GET. Do this like so:

<?php
$code = $_GET['code'];
$price = $_GET['price'];
$date = $_GET['date'];
?>

Make sure when you redirect to that page you run a urlencode() on each variable. For example:

<?php
$code = "A123";
$price = "12.45";
$date = "12-3-2016";
$url = "www.mydomain.com?code=".urlencode($code)."&price=".urlencode($price)."&date=".urlencode($date);
header('Location: '.$url);
?>

You're going to want to use the URL www.mydomain.com/computePrice.php?code=A123&price=12.45&date=12-3-2016

This will pass all of these values to computePrice.php

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