SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure how you expect anyone to tell you what you have done wrong if you don't post your code.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.
    Code:
    // 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:
    Code:
    // 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.

    PHP Code:
    // 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.

  4. #4
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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'] . "\n";

    And receive oinly the first word of the first book.

    Thanks a lot!!!

  5. #5
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do an

    PHP Code:
    echo $Purchased_Books; die(); 
    and post the output or post the html in your browser generated by

    PHP Code:
    <input type="hidden" name="Purchased_Books" value=<?php echo $Purchased_Books?> >
    Your error should then be obvious.

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sir_Arcturua View Post
    <input type="hidden" name="Purchased_Books" value=<?php echo $Purchased_Books; ?> >
    Cups nailed it on the head.

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

    PHP Code:
    echo "blah" //Outputs blah. No quotes. 
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post

    There's no quotes around it.
    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

    Code:
    "'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

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    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.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  9. #9
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •