peterxz
peterxz

Reputation: 874

Input field being rendered as plain text

Im using PHP to render a page. I have echo statements such as

echo "<input type=\"text\" name=\"numPlate\" id=\"numPlate\" onblur=\"caps(this.id);\" $focusPlate required value=\"" . isset($_POST['numPlate']) ? $_POST['numPlate'] : "" ."\">";

but the value is being output as plain text rather than in the input field. If i take the ternary out then the input field is being rendered, which tells me that causes the issue, but i really don't understand why. The problem is is that I have about 20 other fields on this page in a similar fashion, so it's not a very clean solution to evaluate each of these statements to a variable and have the respective variables input into the value property.

Are there any other ways I could achieve this? Thanks

Edit:

Rendered html:

<div class="row"><label for="plate">Rendszám*</label>GMS245</div>

As you can see the string is just a text.

Upvotes: 0

Views: 90

Answers (1)

imvain2
imvain2

Reputation: 15847

I tested it out and I believe my second suggestion could solve it. Wrap the ternary in parenthesis. From my test, it was trying to evaluate the true response to the ternary instead of setting it as the value or ignoring it. I also replaced the inner quotes with a single quote to clean up the code while still allowing the variables to be evaluated.

echo "<input type='text' name='numPlate' id='numPlate onblur='caps(this.id);' $focusPlate required value='" . (isset($_POST['numPlate']) ? $_POST['numPlate'] : "") ."'>";

Upvotes: 2

Related Questions