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

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.