user569322
user569322

Reputation:

HTML PHP form entry problem with space?

Im doing this from my phone so feel free to edit the code for me. Thanks :L

So I have a form, and I'm trying to transfer data FROM a MYSQL database to a hidden field to be submitted in a form. However, I found that if the data in the DB had a space in it (ie Hello there) it only takes "Hello" and skips "there" completly. How would I fix this, and if its not fixable, is there a way around it? Thanks.

$result = mysql_query("SELECT * FROM ##### ORDER BY `id` DESC");

while($row = mysql_fetch_array($result)) {
  $test=$row['name'];
  $id=$row['id'];
  echo"<form method=post action=\"joingroup.php\"><input type=hidden name=test value=$test><input type=submit value=\"Submit\"></input></form>";
}

ANSWER: Put \" 's around the variable.

Upvotes: 0

Views: 8817

Answers (2)

anon
anon

Reputation:

If you have a value with a space in it, you must enclose that value within quotes in your HTML:

Try this:

echo "<form method=post action=\"joingroup.php\">
        <input type=\"hidden\" name=\"test\" value=\"$test\">
        <input type=\"submit\" value=\"Submit\">
      </form>";

Or better yet, use single quotes (and concatenation, since PHP variables aren't evaluated within single quotes like they are with double quotes):

echo '<form method="post" action="joingroup.php">
        <input type="hidden" name="test" value="' . $test . '">
        <input type="submit" value="Submit">
      </form>';

Or, for best results, don't echo out your HTML. Instead, drop out of PHP and jump back in just long enough to echo needed variables:

<?php
  // get values from DB, etc.
?>

<form method="post" action="joingroup.php">
  <input type="hidden" name="test" value="<?php echo $test; ?>">
  <input type="submit" value="Submit">
</form>

<?php
  // resume processing here
?>

Upvotes: 4

zokibtmkd
zokibtmkd

Reputation: 2183

Check the database fields if they are defined as VARCHAR and see the length of each field. Usually this might be the problem, I cannot think of anything else.

Upvotes: 0

Related Questions