PHP:电子邮件形式中不需要的空间


PHP: unwanted space in email form

每次用户在没有在表单字段中输入任何数据的情况下按下"发送"按钮时,都会在"名称"字段中添加一个空格。如果用户在该字段中键入内容并按下"发送"按钮而不填充其他字段,则还会在文本之前添加空格。

我发现,如果我删除"name"输入标记的php代码,空间就会消失。

我想找出是什么原因导致了这一点,而不是添加代码来清空字段,谢谢。

任何帮助我们摆脱这个空间的人都将不胜感激。

contact-form.php:

        <?php if( !empty($errors)) : ?>
            <div class="panel">  
                <ul><li><?php echo implode('</li><li>', $errors); ?></li></ul>
            </div>
            <script src="_scripts/scroll.js" type="text/javascript" charset="utf-8"></script>
        <?php endif; ?>
        <form action="testing-email.php" method="post">
            <label>Name *<input type="text" placeholder="Insert your name here.." name="name" autocomplete="off"<?php echo isset($fields['name']) ? ' value= " '. e($fields['name']) . ' " ' : ''?>>
            </label>
            <label>Email *<input type="email" placeholder="Insert your email here.." name="email" autocomplete="off"<?php echo isset($fields['email']) ? ' value=" '. e($fields['email']) . ' " ' : ''?>>
            </label> 
            <label>Message *<textarea placeholder="Insert your message here.." name="message" rows="8"><?php echo isset($fields['message']) ? e($fields['message']) : ''?></textarea>
            </label>
            <input type="submit" value="Send" class="sendEmail">
            <br>
            <p class="muted">* means a required field</p>
        </form>

testing-email.php:

    foreach($fields as $field => $data) {
       if(empty($data)) {
           $errors[] = 'The ' . ucfirst($field) . ' field is required.';
       }
     } 

main.css:

label, input, textarea {
display: block;
}
input, textarea {
padding: 5px;
font-size: 1em;
width: 100%;
box-sizing: border-box;
}
label input, label textarea {
font-size: 1em;
}
label {
margin-bottom: 14px;
text-align: left;
}
input[type="submit"], button {
width: 20%;
height: 50px;
font-size: 1.2em;
color: black;
font-weight: bold;
outline: none;
-webkit-border-radius: 50px;
border-radius: 50px;
border: 3px solid rgba(0,0,0,.2);
}
.sendEmail:hover {
background-color: black;
color: white;
letter-spacing: .1em;
font-weight: bold;
}
.muted {
color: rgba(0,0,0,.5);
text-align: left;
}
.panel {
color: red;
width: 50%;
text-align: left;
margin-bottom: 14px;
}

Jeroen是对的。您正在为您的价值添加空格:

value= " '. e($fields['name']) . ' " ' : ''

更改为:

value= "'. e($fields['name']) . '"' : ''

这行有一个额外的空间:

<label>Name *<input type="text" placeholder="Insert your name here.." name="name" autocomplete="off"<?php echo isset($fields['name']) ? ' value= " '. e($fields['name']) . ' " ' : ''?>>

把它换成这个:

<label>Name *<input type="text" placeholder="Insert your name here.." name="name" autocomplete="off"<?php echo isset($fields['name']) ? ' value="'. e($fields['name']) . '" ' : ''?>>