How to send ANY text via jquery ajax method to php script?

When text entered in textarea is html entity, php $_POST[]/$_GET[] sees it as empty string.

Here is the code:

<head>
	<script type="text/javascript" src="/Engine/js/jquery-1.4.4.min.js"></script>
	<script type="text/javascript">
		$(document).ready(function(){
			$("button").click(function(){
				$.ajax({
					type: "GET",
					url: "get.php",
					data: "ta=" + $("#ta").val(),
					success: function(data){
						alert(data);
					}
				});
			});
		});
	</script>
</head>

<body>
		<textarea id="ta"></textarea>
		<button>Send it</button>
</body>

and the get.php:

<?php
	echo $_GET['ta'];
?>

type in textarea "&lt;b&gt;dog&lt;/b&gt;", press the button and empty string will be alerted. Why and how to fix it so that every text will be successfuly delivered to php script?

Thnx in advance

Dani AI

Generated

Short version: the problem is an unencoded ampersand. HTML entities like "&lt;" start with a literal "&", and when you build a raw query string by concatenation that ampersand is treated as a parameter separator, so PHP sees ta as empty. As discovered, giving jQuery a data object works because jQuery URL-encodes values before sending.

Why it breaks: an application/x-www-form-urlencoded string uses & to split name=value pairs. If the value contains & (or =, +, ?, #, spaces, etc.) and you don't percent-encode it, the server will parse the pieces as separate parameters.

Practical fixes (pick one):

  • Encode the value yourself before concatenating:

    data: "ta=" + encodeURIComponent(document.getElementById('ta').value)
  • Let jQuery build/encode the string for you:

    data: $.param({ ta: $('#ta').val() })
  • Or pass an object to data (what you already tried) — jQuery will URL-encode automatically for GET or POST.

Extra tips: prefer POST for large blobs (GET URLs have practical length limits). Avoid double-encoding: don’t pre-encode if jQuery will encode again. Always sanitize/escape on output (to prevent XSS) and, when debugging, inspect the raw request (for example $_SERVER['QUERY_STRING'] or logging php://input) so you can see exactly what the server received.

Recommended Answers

All 4 Replies

with post for me it works

$(document).ready(function(){
			$("button").click(function(){
				
				$.ajax({
					type: "post",
					url: "http://localhost/ubagynas/test/testas",
					
					data: {ta: $("#ta").val()},
					success: function(data){
						alert(data);
					}
				});
			});
		});

how abt GET? have you tried GET?

Hey, I've got it. Actually it is very strange, but the problem was

data: "ta=" + $("#ta").val()

in this way when you input in textarea "&lt;b&gt;dog&lt;/b&gt;" it alerts nothing.

However when I use

data: {ta: $("#ta").val()}

everythings works fine! :]

Thnx!

cool :) I was lazy to try with get, because I tried on codeigniter framework, and in it if you want $_get, you have to configure something :)

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.