user14919988
user14919988

Reputation:

PHP is truncating variable in HTML input

I've ran into an interesting quirk with a PHP script I'm running. Whenever I set a PHP variable with multiple words, and set the value of an HTML input to that variable, the contents of the variable is cut off to just the first word. Why is this? Any workarounds?

To my understanding, the phrase "Sentence Typed In PHP" should appear in the first input, but it only shows the first word, "Sentence" - What am I missing??

PHP:

<?php

  $title = 'Sentence Typed In PHP';

  echo "This is the title variable: " .$title;

  echo "<br><input name='title' id='title' type='text' autofocus='autofocus' required value=".$title.">";
  echo "<br><input type='text' value='Sentence Typed in HTML'>";


 ?>

Output:

enter image description here

Upvotes: 0

Views: 376

Answers (3)

abifarisa
abifarisa

Reputation: 11

Try this :

<?php

  $title = 'Sentence Typed In PHP';

  echo 'This is the title variable: ' .$title;

  echo '<br><input name="title" id="title" type="text" autofocus="autofocus" required value="'.$title.'">';
  echo '<br><input type="text" value="Sentence Typed in HTML">';
 ?>

Upvotes: 0

Nikita TSB
Nikita TSB

Reputation: 460

HTML attr value must be in quotes

value='"$title"'

Upvotes: 0

Cornel Raiu
Cornel Raiu

Reputation: 2995

You are missing a set of quotes:

echo "<br><input name='title' id='title' type='text' autofocus='autofocus' required value='".$title."'>";

Upvotes: 5

Related Questions