I am creating a form that sends the form's details to my email.
My custom field is:
My function.php
function getHotel_contact_number() {
global $wp_query;
$postid = $wp_query->post->ID;
$getHotel_contact_number = get_post_meta($postid, 'hotel_contact_number', true);
$getHotel_contact_number;
}
And my form is:
<?PHP
$errors = array();
if (isset($_POST["submit"])) {
$to = "myemail@email.com";
$subject = "This is my subject";
$hotel = get_permalink();
$hotel_contact_nmbr = getHotel_contact_number();
$headers = array('From: '.$_POST['sendername'].' <'.$_POST['senderEmail'].'>');
//Check the name and make sure that it isn't a blank/empty string.
if(empty($sender)){
//Blank string, add error to $errors array.
$errors['sendername'] = "Please enter your name!";
}
if(empty($senderEmail)){
//Blank string, add error to $errors array.
$errors['senderEmail'] = "Please enter your email!";
}
$mailBody = "<h3>Hotel Details</h3><br/>
$hotel<br/>
$hotel_contact_nmbr<br/>"
$mail_sent = wp_mail( $to, $subject, $mailBody, $headers );
}
if ($mail_sent) { ?>
<h1 style="color: #007f00;">Request sent.</h1>
<?php
} else {
?>
<form id="" name="" action="<?php echo get_permalink(); ?>" method="post">
<div class="label-input-wrapper">
<div class="form-label">Name</div>
<div class="form-input">
<input type="text" name="sendername" value="<?PHP if(!empty($errors)) { echo $sender;} ?>" />
<div class="error-msg">
<?php if(isset($errors['sendername'])) { echo '<span style="color: red">'.$errors['sendername'].'</span>'; } ?>
</div>
</div>
</div>
<div class="label-input-wrapper">
<div class="form-label">E-Mail</div>
<div class="form-input">
<input type="email" name="senderEmail" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" required value="<?PHP if(!empty($errors)) { echo $senderEmail;} ?>" />
<div class="error-msg">
<?php if(isset($errors['senderEmail'])) { echo '<span style="color: red">'.$errors['senderEmail'].'</span>'; } ?>
</div>
</div>
</div>
<input type="submit" value="Submit" name="submit">
</form>
<?php
}
?>
But when the form is submitted I am getting just a blank value for hotel contact number
. What I am doing wrong? How can get the proper custom filed value when the form is submitted?
Anyhow it's working fine if I use hidden fields for this as I mentioned here: Have a look at the bottom area of that question where I mention about hidden fields.