I have a page when a PHP variable $books hold a list of books (text).
I need this variable in another page, so I pass it using a type=“hidden” in a form.
The problem is when I print the variable in the receiving page, it shows only the first word!
For instance, if originally $books variable was “Several great book titles”, when it is received in the other page, its value is only “Several”.
This sounds like a common problem caused by poorly constructed html and an unquoted string. You should look at the source code of your html pages carefully for clues about what is going wrong.
// what you probably have
<input type=hidden value=Several great book titles />
// what you should have
<input type=hidden value="Several great book titles" />
Briefly, these are acceptable to most browsers:
// a string without a space
<input type=hidden value=Several />
// a number
<input type=hidden value=23 />
Which might explain why sometimes it seems to just work, and other times it does not.
// you can quote with single quotes if you want to:
echo "<input type=hidden value='Several great book' titles />". PHP_EOL ;
// OR **
echo '<input type=hidden value="Several great book" titles />'. PHP_EOL ;
// OR
echo '<input type=hidden value=\\'Several great book\\' titles />'. PHP_EOL ;
// OR **
echo "<input type=hidden value=\\"Several great book\\" titles />". PHP_EOL ;
This gets especially messy when you start outputting JS and its own quoting needs, then look at using heredoc
** these might be the only ones to pass very strict x/html validation.
Except that the problem wouldnt occur if that were the case. So… yes, you’re doing a test for pneumonia when they’re sitting in the ER with their arm cut off. fix the obvious problem first, and then check for other symptoms.
PS: He gave you an example of the contents of the string.
and from that I can’t tell with 100% certainty if the double quotes are part of the string or not. They probably aren’t and so the cause of the problem would be elsewhere (possibly in code that hasn’t been posted yet), but to be sure I asked the op to post the actual value of the string or the actual generated html.
Either way the point is moot because I suspect the op got the solution on another website