tags:

views:

359

answers:

7

I am trying to display a element from mysql table in a input box type=text. The data is a string value. But i can see only the first word of the entire string.

but if i echo the element, i get the full string value.

My code looks like this:

  echo "Title: <input type=\"text\" name=\"title\" value=".$row['Title']."></input><br>";

Please let me know what am i doing wrong here.

Best Zeeshan

A: 

Maybe you need to resize the form element.

<input type="text" size="50">

This is a text input with a 50 character width.

Echo your line with nl2br() to see if it contains a newline. If it contains newlines you'll want to strip them out as mentioned, to be able to display the text on one line.

echo nl2br(row['Title']);
jjclarkson
changing the size on input box is not helping
Zeeshan Rang
A: 

You probably have a \n character in your string. input type="text" can't handle that. You could either strip it out and replace it with a space: str_replace("\n", " ", $subject) or remove it entirely. Another cause would be a quote " inside your string. htmlspecialchars($subject) is a good solution to get rid of those characters that would mess up your html.

CtlAltDel
My thought exactly :)
altCognito
if i echo($row['Title']);i get the whole string.
Zeeshan Rang
Your problem is the html is being mangled by the contents of $row["Title"]; Try: htmlentities($row["Title"]) instead of the plain variable.
CtlAltDel
Oh, except that we were wrong, unless we mis-edited his code, he's forgotten to wrap it. :(
altCognito
doh. ohwell, still a good idea to escape the quotes.
CtlAltDel
problem fixed. i did not close the text in "" as explain @Julian. thanks
Zeeshan Rang
+1  A: 

style="width:auto;" on the input element doesn't seem to do the trick, so I'm guessing you'll have to resize the field using Javascript. Is this a field the user will be editing?

inkedmn
problem fixed. i did not close the text in "" as explain @Julian. thanks
Zeeshan Rang
+4  A: 

You haven't enclosed the text in quotes in the resulting HTML, try this:

echo "Title: <input type=\"text\" name=\"title\" value=\"".$row['Title']."\"></input><br>";

or better still

echo 'Title: <input type="text" name="title" value="'.$row['Title'].'"></input><br>';

which avoids having to escape the double quotes.

Julian
Thanks alot Julian. echo "Title: <input type=\"text\" name=\"title\" value=\"".$row['Title']."\"></input><br>"; works. But the other one gives an error. But it is working now.
Zeeshan Rang
Note that control characters such as `\n` are output as the 2 characters `\` and `n`, not the newline control character when you use single quoted strings.
Shadow
+1  A: 

I'd start by rewriting it to:

?>
  <div>
  <label>Title: 
    <input type="text" name="title" value="<?php echo htmlspecialchars($row['Title'])?>">
  </label>
  </div>
<?php

So that it is:

  • More readable
  • Safe from quote marks in the data breaking things
  • More accessible (thanks to better markup, including a label element)

Then, if problems were still occurring, examining the generated html rather than the PHP. Each level of code generation you remove yourself from the problem, the harder you make life for yourself.

You are comparing how the browser renders the HTML generated by the PHP with the PHP. Look at the HTML, work out what is wrong with it (validator.w3.org can help), then work out how the PHP is generating it.

David Dorward
problem fixed. i did not close the text in "" as explain @Julian. thanks
Zeeshan Rang
+1  A: 

It appears you're not wrapping your value attribute in quotes, and without quotes, if $row['Title'] contains a space, only up to that space will be set as the value. Try:

echo 'Title: <input type="text" name="title" value="' . $row['Title'] . '" /><br />';
Daniel Vandersluis
+1  A: 

You're missing quotes around the attribute value:

echo "<input type=\"text\" name=\"title\" value=\"" . htmlspecialchars($row['Title'])  . "\"><br>"

Also you should use htmlspecialchars incase the title contains " or <>. Finally, there is no </input>

Greg