英文:
How to get URL parameter inside javascript?
问题
我是新手学习JavaScript和PHP。
我尝试在JavaScript中发布带有URL参数的URL。我该如何实现?
这是我的页面URL:https://localhost/test/home.php?id=07
function showStackedVerticalChart() {
{ // 我想将上述URL参数添加到此URL中(id=07)
$.post("partials/get-chart-data.php?chart_type=vertical-bar&id=07",
function(data) {
var chartdata = {
labels: ["Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6", "Line 7", "Line 8", "Line 9", "Line 10",
"Line 11", "Line 12", "Line 13", "Line 14", "Line 15", "Line 16", "Line 17", "Line 18", "Line 19", "Line 20",
"Line 21", "Line 22", "Line 23", "Line 24", "Line 25", "Line 26", "Line 27", "Line 28", "Line 29", "Line 30", "Line 31", "Line 32"
],
datasets: data
};
var graphTarget = $("#stacked-vertical-chart");
var graph = new Chart(graphTarget, {
type: 'bar',
data: chartdata,
options: {
scales: {
xAxes: [{
barPercentage: 0.5,
stacked: true
}],
yAxes: [{
stacked: true
}]
}
}
});
});
}
}
我尝试将这段代码添加到脚本中。
$id = $_GET['id'];
$.post("partials/get-chart-data.php?chart_type=vertical-bar&' . $mo . '"
英文:
I'm new to JavaScript and PHP.
I'm trying to post url with URL parameter inside JavaScript. How can I achieve it?
Here is my page URL : https://localhost/test/home.php?id=07
<!-- language: lang-js -->
function showStackedVerticalChart() {
{ // I want to add above URL parameter to this URL (id=07)
$.post("partials/get-chart-data.php?chart_type=vertical-bar&id=07",
function(data) {
var chartdata = {
labels: ["Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6", "Line 7", "Line 8", "Line 9", "Line 10",
"Line 11", "Line 12", "Line 13", "Line 14", "Line 15", "Line 16", "Line 17", "Line 18", "Line 19", "Line 20",
"Line 21", "Line 22", "Line 23", "Line 24", "Line 25", "Line 26", "Line 27", "Line 28", "Line 29", "Line 30", "Line 31", "Line 32"
],
datasets: data
};
var graphTarget = $("#stacked-vertical-chart");
var graph = new Chart(graphTarget, {
type: 'bar',
data: chartdata,
options: {
scales: {
xAxes: [{
barPercentage: 0.5,
stacked: true
}],
yAxes: [{
stacked: true
}]
}
}
});
});
}
}
<!-- end snippet -->
I tried adding this inside the script.
$id = $_GET['id'];
$.post("partials/get-chart-data.php?chart_type=vertical-bar&'" . $mo . "' "
答案1
得分: 2
Rather than calling upon PHP to process and render a JavaScript variable, you can do it all in JavaScript - making use of the URLSearchParams API.
/*
The URLSearchParams API allows access to the querystring
- to get/set, etc., but does not parse the full URL so
the URL needs to be pre-processed to create the
querystring.
When using with the actual page URL, you can use
location.search instead.
*/
let pageurl = 'https://localhost/test/home.php?id=07';
let [ url, query ] = pageurl.split('?');
let args = new URLSearchParams( query );
console.log( 'The ID from the URL string is:', args.get('id') );
To add that into the above function, you could do something like:
function showStackedVerticalChart() {
let href = document.location.search();
let args = new URLSearchParams(href);
let id = args.has('id') ? args.get('id') : 0;
$.post(`partials/get-chart-data.php?chart_type=vertical-bar&id=${id}`,
function(data) {
var chartdata = {
labels: [
"Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6", "Line 7", "Line 8", "Line 9", "Line 10",
"Line 11", "Line 12", "Line 13", "Line 14", "Line 15", "Line 16", "Line 17", "Line 18", "Line 19", "Line 20",
"Line 21", "Line 22", "Line 23", "Line 24", "Line 25", "Line 26", "Line 27", "Line 28", "Line 29", "Line 30", "Line 31", "Line 32"
],
datasets: data
};
var graphTarget = $("#stacked-vertical-chart");
var graph = new Chart(graphTarget, {
type: 'bar',
data: chartdata,
options: {
scales: {
xAxes: [{
barPercentage: 0.5,
stacked: true
}],
yAxes: [{
stacked: true
}]
}
}
});
}
);
}
英文:
Rather than calling upon PHP to process and render a javascript variable you can do it all in Javascript - making use of the URLSearchParams api
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
/*
The URLSearchParams api allows access to the querystring
- to get/set etc but does not parse the full url so
the url needs to be pre-processed to create the
querystring.
When using with the actual page url you can use
location.search instead.
*/
let pageurl='https://localhost/test/home.php?id=07';
let [ url, query ]=pageurl.split('?');
let args=new URLSearchParams( query );
console.log( 'The ID from the url string is:', args.get('id') );
<!-- end snippet -->
To add that into the above function you could do something like
function showStackedVerticalChart() {
let href=document.location.search();
let args=new URLSearchParams( href );
let id=args.has('id') ? args.get('id') : 0;
$.post(`partials/get-chart-data.php?chart_type=vertical-bar&id=${id}`,
function(data) {
var chartdata = {
labels: [
"Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6", "Line 7", "Line 8", "Line 9", "Line 10",
"Line 11", "Line 12", "Line 13", "Line 14", "Line 15", "Line 16", "Line 17", "Line 18", "Line 19", "Line 20",
"Line 21", "Line 22", "Line 23", "Line 24", "Line 25", "Line 26", "Line 27", "Line 28", "Line 29", "Line 30", "Line 31", "Line 32"
],
datasets: data
};
var graphTarget = $("#stacked-vertical-chart");
var graph = new Chart(graphTarget, {
type: 'bar',
data: chartdata,
options: {
scales: {
xAxes: [{
barPercentage: 0.5,
stacked: true
}],
yAxes: [{
stacked: true
}]
}
}
});
}
);
}
答案2
得分: 0
获取 PHP 代码块中的 id 值,如下所示:
<?php $id = $_GET['id']; ?>
然后在 JS 中输出 id 变量,如下所示:
$.post("partials/get-chart-data.php?chart_type=vertical-bar&id=<?php echo $id;?>"
英文:
First, get the id value in the PHP code block like
<?php $id = $_GET['id']; ?>
Then echo the id variable inside JS like this
$.post("partials/get-chart-data.php?chart_type=vertical-bar&id=<?php echo $id;?>"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论