PHP多字段联系表单问题
作者:互联网
我的php联络表单有问题(是的,我知道经典的问题).
我在PHP中相当陌生,我找不到代码中损坏的内容.
这是PHP:
<?php
// Email address verification
function isEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
if($_POST) {
// Enter the email where you want to receive the message
$emailTo = 'contact@must-assurances.com';
$clientEmail = addslashes(trim($_POST['email']));
$name = addslashes(trim($_POST['name']));
$entreprise = addslashes(trim($_POST['entreprise']));
$message = addslashes(trim($_POST['message']));
$antispam = addslashes(trim($_POST['antispam']));
$jesuis = addslashes(trim($_POST['jesuis']));
$tel = addslashes(trim($_POST['tel']));
$siren = addslashes(trim($_POST['siren']));
$array = array('emailMessage' => '', 'entrepriseMessage' => '', 'messageMessage' => '', 'antispamMessage' => '', 'jesuisMessage' =>'', 'nameMessage' =>'', 'telMessage' =>'', 'sirenMessage' =>'' );
if($siren == '') {
$array['sirenMessage'] = 'SIREN/SIRET invalide';
}
if(!isEmail($clientEmail)) {
$array['emailMessage'] = 'Email invalide';
}
if($name == '') {
$array['nameMessage'] = 'Nom manquant';
}
if($entreprise == '') {
$array['entrepriseMessage'] = 'Entreprise manquante';
}
if($tel == '') {
$array['telMessage'] = 'Numéro de téléphone manquant';
}
if($siren == '') {
$array['sirenMessage'] = 'SIREN/SIRET invalide';
}
if($message == '') {
$array['messageMessage'] = 'Message manquant';
}
if($antispam != '12') {
$array['antispamMessage'] = 'Mauvaise réponse';
}
if($jesuis == '') {
$array['jesuisMessage'] = 'Merci de préciser si vous êtes une entreprise, un courtier, un investisseur, un professionnel ou autre';
}
if(isEmail($clientEmail) && $entreprise != '' && $message != '' && $antispam == '12' && $jesuis != '' && $name !='' && $tel !='' && $siren!='') {
// Send email
$message = "Je suis un(e) ".$jesuis."\n \n ".$message."\n \n ".$name."\n \n ".$tel."\n \n ".$entreprise."\n \n ".$siren;
$headers = "From: " . $clientEmail . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail .
$headers = array("Content-Type: text/html; charset=UTF-8");
mail($emailTo, $entreprise . " (Formulaire inscription distributeur MUST Assurances)", $message, $headers);
}
echo json_encode($array);
}
?>
这是HTML片段:
<form role="form" action="contact-mia.php" method="post" accept-charset='UTF-8'>
<div class="form-group col-md-3">
<label for="name">Votre nom :</label>
<input type="text" name="name" placeholder="Votre nom..." class="contact-name form-control" id="name">
</div>
<div class="form-group col-md-3">
<label for="email">Votre adresse e-mail :</label>
<input type="text" name="email" placeholder="Email..." class="contact-email form-control" id="email">
</div>
<div class="form-group col-md-3">
<label for="name">Votre numéro de téléphone :</label>
<input type="text" name="tel" placeholder="Numéro de téléphone..." class="contact-tel form-control" id="tel">
</div>
<div class="form-group col-md-3">
<label for="entreprise">Entreprise :</label>
<input type="text" name="entreprise" placeholder="Entreprise..." class="contact-entreprise form-control" id="entreprise">
</div>
<div class="col-md-6 form-group">
<label for="jesuis">Je suis : </label>
<select class="form-control" name="jesuis" id="jesuis">
<option value="">Selectionnez...</option>
<option value="entreprise">Une entreprise</option>
<option value="courtier">Un courtier</option>
<option value="investisseur">Un investisseur</option>
<option value="professionnel">Un professionnel</option>
<option value="autre">Autre</option>
</select>
</div>
<div class="form-group col-md-6">
<label for="entreprise">Numéro de SIREN/SIRET :</label>
<input type="text" name="siren" placeholder="SIREN/SIRET..." class="contact-siren form-control" id="siren">
</div>
<div class="col-md-12 form-group">
<label for="message">Message :</label>
<textarea name="message" placeholder="Message..." class="contact-message form-control" id="message"></textarea>
</div>
<div class="col-md-12 form-group">
<label for="antispam">Question Antispam : 7 + 5 = ?</label>
<input type="text" name="antispam" placeholder="Votre réponse..." class="contact-antispam form-control" id="antispam">
</div>
<div class="col-md-6 col-md-offset-3 form-group centered">
<button type="submit" class="btn">Envoyer</button>
</div>
</form>
关于$message语法的另一个问题是以下代码:
$message = "Je suis un(e) ".$jesuis.'<br />'"Nom : ".$name.'<br />'"Telephone : ".$tel.'<br />'"SIREN : ".$siren.'<br />'"Entreprise : ".$entreprise.'<br />' "Message : ".$message;
‘和’可能有问题吗?
你有什么想法 ?
谢谢 !
洛伊奇
解决方法:
您的PHP方面在IF语句中有一个错误:
进一步查看包含以下内容的行:$headers
您的代码:
if(isEmail($clientEmail) && $entreprise != '' && $message != '' && $antispam == '12' && $jesuis != '' && $name !='' && $tel !='' && $siren!='') {
// Send email
$message = "Je suis un(e) ".$jesuis."\n \n ".$message."\n \n ".$name."\n \n ".$tel."\n \n ".$entreprise."\n \n ".$siren;
$headers = "From: " . $clientEmail . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail .
$headers = array("Content-Type: text/html; charset=UTF-8");
mail($emailTo, $entreprise . " (Formulaire inscription distributeur MUST Assurances)", $message, $headers);
}
更正的代码:
if(isEmail($clientEmail) && $entreprise != '' && $message != '' && $antispam == '12' && $jesuis != '' && $name !='' && $tel !='' && $siren!='') {
// Send email
$message = "Je suis un(e) ".$jesuis."\n \n ".$message."\n \n ".$name."\n \n ".$tel."\n \n ".$entreprise."\n \n ".$siren;
$headers = "From: " . $clientEmail . " <" . $clientEmail . ">\r\n";
$headers.= "Reply-To: " . $clientEmail . "\r\n";
$headers.= "Content-Type: text/html; charset=UTF-8\r\n";
// Wrap mail function in if statement to check if it worked
if (@mail($emailTo, $entreprise . " (Formulaire inscription distributeur MUST Assurances)", $message, $headers)) {
echo 'Successfully sent email';
} else {
echo 'Failed to send';
}
}
为了帮助调试代码,确保已启用PHP错误,您可以使用以下代码在PHP代码的顶部执行此操作:
error_reporting(E_ALL);
ini_set('display_errors', 'On');
标签:contact-form,php 来源: https://codeday.me/bug/20191118/2026375.html