Only one word is received when text variable passed via form

Good day,

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”.

Any advise would be greatly appreciated.

Thanks a lot.

I’m not sure how you expect anyone to tell you what you have done wrong if you don’t post your code.

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.

Hi Cups,

What I have is a variable $Purchased_Books which have the list of purchased books.
Here the information is ok, all books are in the variable.

Then I pass it using
<input type=“hidden” name=“Purchased_Books” value=<?php echo $Purchased_Books; ?> >

And in the following page add it to an e-mail:
$message_body .= "Libros adquiridos: " . $_POST[‘Purchased_Books’] . "

And receive oinly the first word of the first book.

Thanks a lot!!!

do an

echo $Purchased_Books; die();

and post the output or post the html in your browser generated by

<input type="hidden" name="Purchased_Books" value=<?php echo $Purchased_Books; ?> >

Your error should then be obvious.

Cups nailed it on the head.

Look at your value. There’s no quotes around it.

echo "blah" //Outputs blah. No quotes.

That’s the most probable cause, but there might be quotes built into the string itself and the cause of the problem would then be something else.

The string could be

"'list of book titles'"

That’s why I asked the op to post the value of the string or the html itself - to be certain of what the cause is :slight_smile:

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. :wink: fix the obvious problem first, and then check for other symptoms.

PS: He gave you an example of the contents of the string.

In the first post all the op gave was

“Several great book titles”

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 :slight_smile: