nanjero05 nanjero05 - 7 months ago 11
PHP Question

Why is my textbox getting a ghost value? HTML5/PHP

I have a text box that seems to be getting a blank/space value when the form loads up. when i click submit and the form is validated it seems to be getting an additional space value. All other textboxes does not seem to be affected

Pics:

load up: http://prntscr.com/azmime

1st submit click: http://prntscr.com/azmit0

2nd submit click: http://prntscr.com/azmixw

These are the code related to the post code text box:

PHP:

<?php
$pCode = "";
$pCodeMsg = "";

$pCode = $_POST["postcode"];
if (!empty($pCode)) {
$pCodePattern = "/^(?:\d{4})?$/";
if (!preg_match($pCodePattern, $pCode)) {
$pCodeMsg = "<span> post code must be 4 digits </span>";
}
}
?>


HTML form:

<p>
<label for="postcode"> Postcode (default 2000): </label>
<input type="text" id="postcode" name="postcode" size=4 value="<?php echo $pCode; ?> "/>
<?php echo $pCodeMsg; ?>
</p>

Answer

<input type="text" id="postcode" name="postcode" size=4 value="<?php echo $pCode; ?> "/>

see the extra space before the close " of the value ??

Fixed:

<input type="text" id="postcode" name="postcode" size=4 value="<?php echo $pCode; ?>"/>