-2

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

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
              }]
            }
          }
        });
      });
  }
}

I tried adding this inside the script.

$id = $_GET['id'];
$.post("partials/get-chart-data.php?chart_type=vertical-bar&'" . $mo . "' "
2

2 Answers 2

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
                        }]
                    }
                }
            });
        }
    );
}
Sign up to request clarification or add additional context in comments.

Comments

0

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;?>"

2 Comments

Just beware of XSS here. You might want to sanitize the var
Should be urlencode($id)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.