Spatial Digger
Spatial Digger

Reputation: 1993

text into a text box from a query

I'm trying to present the result of a query into a text box. The $r->context_type returns the correct value, but it is not displayed in the text box. Here's the code snippet for the text box, where have I gone wrong?

echo "<input type='text' name='name1' id='' placeholder='$r->context_type' autocomplete='off' tabindex='4' class='txtinput' /> <br />";

Upvotes: 0

Views: 39

Answers (4)

Marcin Orlowski
Marcin Orlowski

Reputation: 75645

You need to pass the query thru htmlspecialchars() to avoid problems with its value being incorrectly interpreted by browser:

echo "<input type='text' name='name1' placeholder='" . htmlspecialchars($r->context_type) . "' autocomplete='off' tabindex='4' class='txtinput' /> <br />";

however for better readability I'd rather write either

$val = htmlspecialchars($r->context_type);
echo "<input type='text' name='name1' placeholder='{$val}' autocomplete='off' tabindex='4' class='txtinput' /> <br />";

or

printf("<input type='text' name='name1' placeholder='%s' autocomplete='off' tabindex='4' class='txtinput' /> <br />",

htmlspecialchars($r->context_type));

Also remove id='' as it makes no much sense.

Upvotes: 0

Sate Wedos
Sate Wedos

Reputation: 589

plcaeholder just a comment for TextBox. if you want to set value use it:

value='$r->context_type'

Upvotes: 0

Regolith
Regolith

Reputation: 2982

if you are trying to display it as a placeholder then try

echo "<input type='text' name='name1' id='' placeholder='".$r->context_type."' autocomplete='off' tabindex='4' class='txtinput' /> <br />";

Or if you want to display and edit it in input text then do this

echo "<input type='text' name='name1' id='' value='".$r->context_type."' autocomplete='off' tabindex='4' class='txtinput' /> <br />";

Note: to be able to edit use value insted of placeholder

Upvotes: 1

Difster
Difster

Reputation: 3260

I always prefer to concatenate, try this.

echo "<input type='text' name='name1' id='' placeholder='" . $r->context_type . "' autocomplete='off' tabindex='4' class='txtinput' /> <br />";

Also, are you sure you want the placeholder to get the variable and not value=

Upvotes: 1

Related Questions