Is there a syntax error in Javascript when using a string and variable with the GET method?

Is there a way to send a value using the get method?

In JavaScript, we need to use the + symbol to concatenate strings. But my issue goes beyond this simple problem. If I attempt the following:

Let's say;

var sID = 16;

var rID = 17;

EDIT-2: I actually need the string to be in an open method like in this script.

<script type="text/javascript">

        function loadName() {
            var xmlhttp;
            xmlhttp = new XMLHttpRequest();

            xmlhttp.onreadystatechange = function()
            {
              $("#btn_add_friend").attr("title", "xxx");
                    document.getElementById("btn_add_friend").innerHTML = xmlhttp.responseText;

            };
            var sID = <?php echo $_SESSION["SES_USER_ID"];?>; //value:16
            var rID = <?php echo $profile_owner_id;?>;       //value:17

            xmlhttp.open("GET", "request.php?sender_id=" + sID + "&receiver_id=" + rID, true);
            xmlhttp.send();
        }

</script> 

When this is added to the browser address line, double quotes appear. I want to avoid seeing double quotes and still be able to use the sID and rID variables. The script works without the variables, but adding them causes it to fail.

The desired format on the browser's address line should be:

/request.php?sender_id=16&receiver_id=17

How can this be resolved?

Answer №1

It appears that there is an excess of quotation marks in your code. This revised version should work:

var url = "request.php?sender_id=" + sID + "&receiver_id=" + rID;

UPDATE

You have a syntax error at this point:

"request.php?sender_id="+sID+"&receiver_id="+rID+"

Simply remove the trailing +":

xmlhttp.open("GET", "request.php?sender_id=" + sID + "&receiver_id=" + rID, true);

Double quotes will not appear in the URL as they serve as string delimiters. Everything between them is considered part of the string (e.g. "this is my string"); They are not included in the actual string output.

UPDATE2

Don't forget to use echo for your PHP variables:

var sID = <?php echo $_SESSION["SES_USER_ID"];?>;
var rID = <?php echo $profile_owner_id;?>;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best method for loading multiple HTML files into a Div container?

Recently, I made the decision to improve the look of an online manual I have been working on for my company by incorporating Bootstrap. The manual is structured with a tree-view that contains titles linking to HTML files with information and CSS stylesheet ...

What are some ways to maintain code efficiency when working with AJAX requests?

Looking at the code below, I am making two identical Ajax requests with only one line of difference. Is there a way to consolidate this into a function to maintain DRY (Don't Repeat Yourself) code? $('.searchable').multiSelect({ selecta ...

In Google Chrome, the edges of hexagons appear jagged and not smooth

I have encountered an issue in Chrome where I created a hexagon group using HTML and CSS. While it displays fine in Firefox, the edges of the hexagons appear distorted in Chrome. Here are my code snippets: HTML <div class="col-sm-12 margin-left-100" i ...

Unable to modify div style using a JS function

I am attempting to show different divs based on the button clicked, with all starting with a display style of "none" except for one default div called "atualizacoes". After clicking a button, all divs should be set to display="none", and then the specific ...

Prevent the SnackBar from extending to full width in case of a smaller window size

Is there a solution to prevent the SnackBar from spanning the entire width of the screen when the window is narrow? ...

JavaScript xPath is ineffective at providing a return value

Trying to work through an issue with xPath and I am new to this concept. Any guidance or assistance in JavaScript would be greatly appreciated. Here is the simple HTML document that I have: <!DOCTYPE html> <html> <head> < ...

Utilize AJAX in JavaScript file

I am encountering an issue with the following code: function inicioConsultar(){ $(function(){ $('#serviciosU').change(function(){ if ($('#serviciosU').val()!= "-1") { $.ajax({ url: "@Url. ...

What are some ways to customize the default Head tag in Next.js?

After using create-next-app to create a basic page, I decided to style the index.js page. I added a navigation bar within the header and now I'm trying to customize it. I've been wondering if there's a way to style the custom Head tag in Ne ...

Form duplicates messages sent

I have made some adjustments to a free form, and it seems to be functioning properly. However, I have encountered an issue where the email is being sent twice and the message is written twice after completion. I attempted to replace the button and input ...

Update the value of a td element when a select option is changed in another td element within the same table row

I have a table set up as follows: Header1 | Header2 | Header3 | Header 4 | Header 5 Row 1 |<span>Some Text</span> | <select></select> Row 2 Row 3 . . . . Rows generated dynamically. My objecti ...

Hiding timestamps on an interactive transcript: A step-by-step guide

I am in the process of creating personalized interactive ebooks based on a similar website found here: This website utilizes a VTT file along with an audio file. Although the JavaScript code requires time stamps, I would like to hide them for better read ...

The HTML unit is unable to locate the form for unknown reasons. My suspicion is that it could be due to the presence of Angular

Struggling with using HTMLunit to login and upload a file. Successfully logged in but unable to locate the form, despite it being present on the HTML page. Here is the JAVA CODE snippet: public static void main(String[] args) { WebClient webClient = ...

Troubleshooting Bootstrap 3.0: Issues with nav-tabs not toggling

I have set up my navigation tabs using Bootstrap 3 in the following way: <ul class="nav nav-tabs pull-right projects" role="tablist" style="margin-top:20px;"> <li class="active"><a role="tab" data-toggle="tab" href="#progress">In Pr ...

Tips for overcoming a script error within the body of a Next.js project

I encountered an error in my _document.js file when trying to add a script to the body of my document. Here is the specific error message that was returned: https://i.stack.imgur.com/QG5zb.png ./pages/_document.js Error: x Expected '}', got &a ...

Tips for looping through a JSON object?

Similar Question: How to extract a specific value from a nested JSON data structure? I am looking to loop through a two-dimensional JSON object, whereas I already know how to do so for a one-dimensional JSON object. for (var key in data) { alert(data ...

Error: Unable to submit data as the function this.submitData is not recognized

Having trouble calling an async function in the mounted() lifecycle hook of Vue.js? Keep getting the error message: Uncaught TypeError: this.submitData is not a function. Here's the code snippet in question: <template> <section class=&quo ...

While working on a project in React, I successfully implemented an async function to fetch data from an API. However, upon returning the data, I encountered an issue where it was displaying as a

I am working with React and TypeScript and have the following code snippet: const fetchData = async () => { const res: any = await fetch("https://api.spotify.com/v1/search?q=thoughtsofadyingatheist&type=track&limit=30", { met ...

Using Javascript libraries on a Chromebook: A comprehensive guide to getting started

Doing some coding on my chromebook and wondering if it's possible to download and utilize libraries such as jQuery. Would really appreciate any assistance with this! ...

There seems to be an issue with the Alexa skill's ability to provide a response after another

I am currently developing an Alexa skill that involves a multi-step dialog where the user needs to respond to several questions one after the other. To begin, I am trying to kick things off by implementing a single slot prompt. I am checking if the slot is ...

Will the .replaceWith() method alter the code visible to search engines?

After successfully modifying the content of specific H1 elements to not return the value from a global variable using the following code; <script type="text/javascript"> $(document).ready(function() { $("H1").filter(function() { return $(this).text ...