27a 27a - 5 months ago 9
HTML Question

PHP - Cannot Assign String Value to Input with Pattern

I want to assign mysql_result strings to text inputs, but I can't figure out how the pattern attribute messes it up.

$id = $_GET['id'];
$q = mysql_query("SELECT * FROM employees WHERE id=".$id);

if(mysql_num_rows($q)!=0)
{
$address = mysql_result($q, 0, 3);
}


Sample result would be: 1034 Wildwood Street Stow, OH 44224.
My pattern regex only accepts alpha-numeric characters, period, comma, pound sign, hyphen, and spaces.

echo "<input type='text' class='form-control' name='address' value=".$address." pattern='[a-zA-Z0-9.,#\- ]+' title='Letters, numbers, pound sign, period, and comma only' required>";


The text input will only display 1034. I don't know why it ignores the space and the characters after it.

Answer

You need to quote the value attribute.

<?php
echo "<input type='text' class='form-control' name='address' value='$address' pattern='[a-zA-Z0-9.,#\- ]+' title='Letters, numbers, pound sign, period, and comma only' required>";
Comments