'trouble with php server adding variable to my contact forms
Hi I have added some input variables in my contact form and I can't find how to add them to my server.php I attempted multiple things such as adding $phone and other variables I just don't know where to import them from the answer might be closer than I expected.
here my original contact form
<div class="form-group">
<div class="input-group">
<label for="name" class="input-group-addon">
<span class="glyphicon glyphicon-user"></span>
</label>
<input type="text" placeholder="Nom" class="form-control col-xs-12" id="name" name="name" data-error="Valid Name" required>
</div>
<span class="help-block with-errors"></span></div><!-- phone--><div class="form-group">
<div class="input-group">
<label for="phone" class="input-group-addon">
<span class="glyphicon glyphicon-earphone"></span>
</label>
<input type="tel" placeholder="Telephone or email" class="form-control col-xs-12" data-error="Valid phone" id="phone" name="phone" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}">
</div>
<span class="help-block with-errors"></span></div><!-- Who to contact --><div class="form-group">
<div class="input-group">
<label for="person" class="input-group-addon">
<span class="glyphicon glyphicon-user"></span>
communiqué avec: <b>(optionnel)</b>
</label>
<input list="persons" name="person" id="person">
<datalist id="personnes">
<option value="person1">
<option value="person2">
<option value="person3">
<option value="person4">
</datalist>
</div>
<span class="help-block with-errors"></span>
</div>
<!-- Custom time --><div class="form-group"><div class="input-group"><label for="appt" class="input-group-addon"><span class="glyphicon glyphicon-time"></span>
Calling time:<b>(optional)</b></label><input type="time" placeholder="E-mail or phone" id="appt" name="appt"></div>
<span class="help-block with-errors"></span></div>
<!-- Message --><div class="form-group">
<div class="input-group"><label for="message" class="input-group-addon"><span class="glyphicon glyphicon-comment"></span></label><textarea class="form-control" rows="4" id="message" name="message incorrect" placeholder="Message" data-error="Message" required></textarea></div>
<span class="help-block with-errors"></span></div>
<div><button type="submit" id="submit" data-error="enter message" class="btn btn-custom btn-block">Send</button></div>
Here my original server .php
<?php
$errorMsg = "test";
// NAME
if (empty($_POST["name"])) {
$errorMSG = "Name is required! ";
} else {
$name = $_POST["name"];
}
// EMAIL
if (empty($_POST["mail"])) {
$errorMSG .= "Email is required ";
} else {
$mail = $_POST["mail"];
}
// MESSAGE
if (empty($_POST["message"])) {
$errorMSG .= "Message is required ";
} else {
$message = nl2br($_POST['message']);
}
// Phone
if (empty($_POST["phone"])) {
$errorMSG .= "phone is required ";
} else {
$phone = $_POST["phone"];
}
// Change to your E-mail && Subject
$EmailTo = "[email protected]"; // change to your E-mail here
$Subject = "contact form"; // change to the subject you want
// prepare email body text
$Body = '<b>Name:</b> '.$name.' <br><b>E-mail</b> '.$mail.' <p>' .$message.'' .$phone.'</p>';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: ' . $mail . "\r\n";
// send email
$success = mail($EmailTo, $Subject, $Body, $headers);
// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
echo "Something went wrong :(";
} else {
echo $errorMSG;
}
}
Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
