I'm having a problem with a HTML form and php.
I'm trying to make the default-value of a form-text field to be a pre-defined string. For this I'm using a reference. However, whenever I try to place a string into the form using PHP or references in any kind shape or form, only the first word gets added.
Here is a picture to describe the situation:
Does anyone know why this is happening, and/or a way to work around this issue?
Actual code:
<?php
Echo "<input type=\"text\" name=\"Address\" value=\"one two three\"><br>";
?>
<?php
$str ="one two three";
Echo "<input type=\"text\" name=\"Address\" value=" . $str . "><br>";
?>
<input type="text" name="Address" value=<?php echo "one two three";?>><br>
<input type="text" name="Address" value=<?php $str = "one two three"; echo $str;?>><br>
<input type="text" name="Address" value="one two three"><br>
You don't quote the echoed words.
Basically you do this:
<input type="text" value=one two three />
But you need to do that:
<input type="text" value="one two three" />
Just add "" around the PHP.
BTW: You need to escape these if you don't want to get XSSed. These attacks are quite scary. Read about it here: https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)
The problem was the quotes
when you was doing the echo on the php, you need to simple-quote it to make it works.
Html was understanding that two
and three
was additional parameters of the <input>
because it wasnt quoted on the resulting html.
Use this way:
<?php
Echo "<input type=\"text\" name=\"Address\" value=\"one two three\"><br>";
?>
<?php
$str ="one two three";
Echo "<input type=\"text\" name=\"Address\" value=" . $str . "><br>";
?>
<input type="text" name="Address" value='<?php echo "one two three";?>'><br>
<input type="text" name="Address" value='<?php $str = "one two three"; echo $str;?>'><br>
<input type="text" name="Address" value="one two three"><br>
Its because you dont set it in "" or '' marks for the html.
The 2. Line: ...\"Address\" value=" . $str . ">
....";
for example outputs for the value attribute: value=one two three
The interpreter now thinks two and three are propertys and not part of value.
Same with the other cases.
I think in this value attribute of input type having a string with whitespace. HTML treat it as separate attributes of input text.
To resolve this problem, u can use single quote at begining and end of value attribute.
Look this example. Hope you will like this. I just modified your code.
<?php
echo "<input type=\"text\" name=\"Address\" value=\"one two three\"><br>";
?>
<?php
$str ="one two three";
echo "<input type=\"text\" name=\"Address\" value='" . $str . "'><br>";
?>
<input type="text" name="Address" value='<?php echo "one two three";?>'><br>
<input type="text" name="Address" value='<?php $str = "one two three"; echo $str;?>'><br>
<input type="text" name="Address" value="one two three"><br>
In this case it will get One Two Three in every textbox.