Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

How to pass a variable with & characters in a post ajax request ?

Hello,

I have a problem with an app I have developed myself, it's a PHP / JS app.

I have a post request sent via ajax and it looks like this.

$.ajax({
	type: 'POST',
	url : 'index.php?param=value&otherparam=othervalue',
	data: 'id=' + id + '&description=' + description + '&' + reponses,
	success:function(msg) {
		...
	}
});

The problem is that the description can contain an html link like this one.

This is my super description.

Here is a link : <a href="https://mydomain.com/index.php?param2=value2&otherparam2=othervalue2">Click here</a>

End of the description ;).

The description variable contains the good value before ajax, but contains a troncated value after ajax in the PHP controller.

This is my super description.

Here is a link : <a href="https://mydomain.com/index.php?param2=value2

The problem is exactly the same if the & is anywhere else, all what is after the & is cut.

I thought perhaps about replacing the & by something else like a unique string (-(-)-) so that I'm sure it will be unique and it won't cause the problem. Then in the PHP controller I can replace (-(-)-) with &.

What do you think about this solution ?

Do you have any other suggestion / idea ?

Thanks for your help ;).

0 likes
5 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Why send it as an url and not as post data? That should solve it

1 like
vincent15000's avatar

@Sinnbeck I also noticed the same as you and I have already tried to change like this.

$.ajax({
	type: 'POST',
	url : 'index.php?param=value&otherparam=othervalue',
	data: {
		'id': id,
		'description': description,
		'responses': reponses,
	}
	success:function(msg) {
		...
	}
});

But it doesn't work.

Should it work like this ?

vincent15000's avatar

@Sinnbeck Yes it's the description. Now it works ... perhaps I had forgotten something while rewriting the data parameter.

Thank you @sinnbeck.

Please or to participate in this conversation.