vendredi 17 avril 2015

PHP form: How not to send name of input if value empty?

i am searching already since two days. I have a simple PHP contact form. Is it possible to "not" send the "name" of an text field, if it´s empty (no value)?


for example i have:





<form action="{form-action}" method="post">
<input type="hidden" name="send" value="1" />

<label for="Name">{Name}</label>
<input type="text" name="Name" id="Name" maxlength="100"/>

<label for="Optional">{Optional}</label>
<input type="text" name="Optional" id="Optional" maxlength="100"/>

<input type="submit" value="{submit}" />

</form>



PHP:





<?php

if (!isset ($_POST['send'])) {

#Form has not been sent yet
#Create contact form
// ....


} elseif ($_POST['send'] == 1) {
#Form has been sent

#Check user input
$noerrors = true;
$msg = '';

if ($_POST['Name'] == '') {
$noerrors = false;
$msg .= "Please input Name";
}

if ($noerrors == false) {
#Errors have been found
echo $msg;
} else {
#No errors, create and send mail
if (is_array($_POST)) {
foreach ($_POST as $key => $value) {
if ($key != 'send') {
$mail_body .= "<span>$key</span><span>$value</span>";
}
}
}

#Get mailer from settings
//...

#Display message after mail is sent
echo "Thank you!";
}
}
?>



The result in the E-Mail for example:


Name (Example) Optional


But I would like to not show the name of the Input if there is no value. Is this possible? Does someone know how?


Aucun commentaire:

Enregistrer un commentaire