<form method="post" action="" id="infoform">
<label for="first">
First Name</label>
<input type="text" name="first" id="first" class="field validate[required]">
</br>
<label for="surname">
Last Name</label>
<input type="text" name="surname" id="surname" class="field validate[required]"> </br>
<label for="email">
Email</label>
<input type="text" name="email" id="email" class=" field email validate[required,custom[email]]">
</br>
<div class="radios">
<input id="test2" checked="checked" type="radio" name="message" value="register" />
Register for Updates</input>
<input id="test1" type="radio" name="message" value="msgs" />
Send a Message</input>
</div>
<div class="msg">
<label for="message" id="messagelabel">
Message</label>
<textarea type="text" name="message" id="message"></textarea>
</div>
<input id="submit" type="submit" value=""><div class="test3">
<a class="sbmt">Register</a></div>
<div class="test4">
<a class="sbmt">Send</a></div>
</input>
</form>
If the radio box ‘test1’ is checked, then the div msg appears. This is then sent via ajax to an email php script:
<?php
$EmailTo = "matt@explosivetitles.com";
$adminSubject = "Civitas Message";
$firstName = Trim(stripslashes($_POST['first']));
$Surname = Trim(stripslashes($_POST['surname']));
$Email = Trim(stripslashes($_POST['email']));
$Message = Trim(stripslashes($_POST['message']));
$adminheaders = 'From: "Civitas website" <info@civitas.com>';
$adminBody .= "First Name: ";
$adminBody .= $firstName;
$adminBody .= "\n";
$adminBody .= "Surname: ";
$adminBody .= $Surname;
$adminBody .= "\n";
$adminBody .= "Email: ";
$adminBody .= $Email;
$adminBody .= "\n";
$adminBody .= "Message: ";
$adminBody .= $Message;
$adminBody .= "\n";
$adminMail = mail($EmailTo, $adminSubject, $adminBody, $adminheaders);
if ($adminMail){
echo "true";
}
else{
echo "false";
}
?>
However I only want the “message” part of the email to be sent if the message box is showing. How can I do this?
PHP does not look at the
idof an element, instead, when aPOSTorGETmethod is sent, it looks at thenameattribute. In the code you have written, you have two input elements,textareaand theradio inputwith the same name, “message”. When both are sent, the$_POSTvariable becomes an array and uses thenameattribute as an index for the value submitted, and PHP arrays cannot have duplicate indexes, therefore to over come this problem you rename one. For yourradio inputI suggest thenameattribute be set totype.Also, your code hurts my eyes, so I rewrote it.