Hello Experts,
I am developing application in which i use ajax.but i need to pass more than one variable to url for further processing. I can't use

var url="availabilitycheck.php?t="+value  //it works
var url="availabilitycheck.php?t="+value+"&hid="+hd1+"&chkin="+chkin; //not valid

How can i send other variables.??
I tried using session that works but it does not work when i integrated it to joomla.
So is there any way to pass more variable in url as querystring?

Dani AI

Generated

Common causes for the “can’t pass more than one variable” symptom are easy to miss: a missing question mark, literal HTML entities like & ending up inside the JavaScript string, un-encoded values (spaces, ampersands), or undefined variables. As corrected, the query portion must start with ?; ’s concatenation will work if the pieces exist; and ’s debugging tips are useful for finding which variable is undefined.

Prefer a small helper that builds a safe query string instead of hand-concatenating. Modern browsers provide URLSearchParams (which encodes values automatically) and libraries provide equivalents (for example $.param in jQuery). Example:

const params = new URLSearchParams({ t: value, hid: hd1, chkin: chkin });
const url = 'availabilitycheck.php?' + params.toString();

For larger or sensitive payloads, send data in the request body (POST) rather than the URL. Fetch can send form-encoded bodies without changing server-side plumbing:

fetch('availabilitycheck.php', {
  method: 'POST',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  body: new URLSearchParams({ t: value, hid: hd1, chkin: chkin })
});

Important practical notes and gotchas:

  • Always URL-encode values (URLSearchParams/encodeURIComponent) so spaces and & don’t break the string.
  • In HTML markup & is correct, but inside a JavaScript string literal the & character must be used.
  • Avoid putting secrets or long data in query strings (some browsers/servers have ~2000-character limits); use POST for large payloads.
  • If sessions fail when integrating into Joomla, native $_SESSION can clash with Joomla’s session handling. Use Joomla’s session API (JFactory::getSession() / JSession) or ensure cookies/domains match; also confirm requests are same-protocol and same-host so cookies are sent.

For quick debugging, log the final URL (or inspect Network tab) and check server-side $_GET/POST to see what actually arrives.

Recommended Answers

All 4 Replies

Chintan,

Your code should work providing value , hd1 and chkin exist. Otherwise javascript will generate an error.

I generally build search strings like this in an array, then use Array.join() to put the pieces together.

var searchString = [];
searchString.push("t=" + value);
searchString.push("hid=" + hd1);
searchString.push("chkin=" + chkin);
var url = "availabilitycheck.php" + searchString.join('&');

By doing it this way, you get a more informative line number should a javascript error arise.

Airshow

Try this

var url=("availabilitycheck.php?t="+value+"&hid="+hd1+"&chkin="+chkin);

Just noticed a missing ? in my post above

Line 5 hould be

var url = "availabilitycheck.php?" + searchString.join('&');

Airshow

As Airshow said, if value of any of your variable t, hd1 and chkin does not exist, it will break your JavaScript. If you have Firefox, you should install firebug to aid you in debugging (firebug will show you where the error occurs and from what possible reason). If you use IE, there will be a small yellow icon with exclamation at the bottom left if there is any error in your script. You need to double click on that icon to see somewhat not useful information of the error... But at least you know that there is an error...

If you know and may not expect to see some variables on the server side (and have a mechanism to validate and handle on your server side), you could add something like...

// modified from Airshow's code
var searchString = [];
if(!isNaN(value) || value) { searchString.push("t=" + value); }
if(!isNaN(hd1) || hd1) { searchString.push("hid=" + hd1); }
if(!isNaN(chkin) || chkin) { searchString.push("chkin=" + chkin); }
var url = "availabilitycheck.php?" + searchString.join('&');
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.