JORd
JORd

Reputation: 109

php dosent echo the right value of a input

I want to concat a string in jquery, but when I post the string I see only a part of the mystringg.

My html is:

 <input type="text" style="display:none" name="mystring" id="mystring" value=""/>

I have some variables:

var ti="ti";
var low="low";
var moderate="moderate";
var high="high";
var title="title";

And I try to concat a string like:

var mystringg="";
mystringg+= "<\n"+ti+">";
mystringg+= "\n<LOW> "+low+" </LOW> ";
mystringg+= "\n<MODERATE> "+moderate+" </MODERATE> ";
mystringg+= "\n<HIGH> "+high+" </HIGH> ";
mystringg+="\n</"+title+">";

alert(mystringg); // here print the right value of the string
$('#mystring').val(mystringg);

In php:

$string  = $_POST['mystring'];
echo $string;

When I alert mystringg, I get the right string. But when I post it in an another php page, I only get a part of the value, actually I get only the value of the variables, not the manual values.

Upvotes: 1

Views: 41

Answers (1)

random_user_name
random_user_name

Reputation: 26160

You are getting the full values. The reason you don't think so is because you've wrapped them in tags, although not standard HTML tags.

If you var_dump($_POST['mystring']);, you'll see this:

string '<ti><LOW> low </LOW> <MODERATE> moderate </MODERATE> <HIGH> high </HIGH> </title>'

So, the full value is there.

But - when you view the echo of it (as your code does), the tags are being rendered as HTML tags (and thus don't show up), so it looks like this:

low moderate high

BUT, if you view source, you'll see that in fact it's all there:

<ti><LOW> low </LOW> 
<MODERATE> moderate </MODERATE> 
<HIGH> high </HIGH> 
</title>

If you want to see the "tags" when you echo them, then echo using htmlspecialchars like so:

echo htmlspecialchars( $_POST['mystring'] );

Upvotes: 1

Related Questions