Sending mail from a form that have a requirement

Hy. I have made a form in html, bootstrap, php and I used Swift mailer setups for sending the e-mails.
In my form, I have a requirement that asks the respondent if he needs Visa, in the case that he answers “YES” another form appears. If he answers “NO”, then he is able to submit the form. My question it is: How can I construct the message body to appear in accordance with this condition? I mean, when the respondent chooses “NO”, the message to contain only the information asked for and if he choose YES, then the message to contain also the rest of the information?
In this moment the mail isn’t sending. (I used the same syntax php for sending mails from Contact US form and it works).
Thank you for you time.

[code]

Venue:

Koblenz, Germany

<div class="col-sm-6">
  <label for="venue" class="control-label">Dates:</label>
  </br>
  <label>
    <p> 22nd - 28th April 2017 </p>
  </label>
</div>

Full name:

Date of birth:
a Gender: Choose.. Female Male
Country:
Email:
Phone number:

Residential address (Street, City, Postal code, Country):

Level of English(bad, medium, good, excellent):
Occupation or profession:

Other remarks or questions:

Do you need a visa to come to course venue? YES NO

If you need visa, please complete the following data:

<label for="firstName" class="control-label">Full name(as written in the passaport):</label>
<div class="form-group">
  <div class="col-sm-6 padding-top-10">
    <input type="text" class="form-control" name="first_name" data-parsley-pattern="^[a-zA-Z ]+$" id="first_name" placeholder="First" />
  </div>
  <div class="col-sm-6 padding-top-10">
    <input type="text" class="form-control" name="last_name" data-parsley-pattern="^[a-zA-Z ]+$" id="last_name" placeholder="Last" />
  </div>
</div>

<div class="form-group">
  <div class="col-sm-4 padding-top-10">
    <label for="dateofBirth" class="control-label">Date of birth:</label>
    <input type="text" class="form-control" name="dateof_birth" data-parsley-trigger="keyup" data-parsley-pattern="/^(\d{1,2})(\/|.)?(\d{1,2})(\/|.)?(\d{4})$/" placeholder="MM.DD.YYYY" data-date-format="MM.DD.YYYY" id="dateof_birth" />
  </div>
  <div class="col-sm-4 padding-top-10">
    <label for="placeofBirth" class="control-label">Place of birth:</label>
    <input type="text" class="form-control" name="placeof_birth" data-parsley-pattern="^[a-zA-Z ]+$" id="placeof_birth" placeholder="your place of birth" />
  </div>
  <div class="col-sm-4 padding-top-10">
    <label for="nationality" class="control-label">Nationality:</label>
    <input type="text" class="form-control" name="nationality" data-parsley-pattern="^[a-zA-Z ]+$" id="nationality" placeholder="your nationality" />
  </div>
</div>

<div class="form-group">
  <div class="col-sm-3 padding-top-10">
    <label for="passaport" class="control-label">Passaport number:</label>
    <input type="text" class="form-control" name="passaport" data-parsley-trigger="keyup" data-parsley-minlength="7" data-parsley-maxlength="10" id="passaport" placeholder="your passaport number" />
  </div>
  <div class="col-sm-3 padding-top-10">
    <label for="dateofIssue" class="control-label">Date of issue:</label>
    <input type="text" class="form-control" name="dateofIssue" data-parsley-trigger="keyup" data-parsley-pattern="/^(\d{1,2})(\/|.)?(\d{1,2})(\/|.)?(\d{4})$/" placeholder="MM.DD.YYYY" data-date-format="MM.DD.YYYY" id="dateofIssue" />
  </div>
  <div class="col-sm-3 padding-top-10">
    <label for="placeofIssue" class="control-label">Place of issue:</label>
    <input type="text" class="form-control" name="placeofIssue" data-parsley-pattern="^[a-zA-Z ]+$" id="placeofIssue" placeholder="place of issue" />
  </div>
  <div class="col-sm-3 padding-top-10">
    <label for="dateofExpiry" class="control-label">Date of expiry:</label>
    <input type="number" class="form-control" name="dateofExpiry" data-parsley-trigger="keyup" data-parsley-pattern="/^(\d{1,2})(\/|.)?(\d{1,2})(\/|.)?(\d{4})$/" placeholder="MM.DD.YYYY" data-date-format="MM.DD.YYYY" id="dateofExpiry" />
  </div>
</div>

<label for="address" class="control-label padding-top-10">Residence address:</label>
<div class="form-group">
  <div class="col-sm-12">
    <input type="text" class="form-control" data-parsley-minlength="10" data-parsley-trigger="keyup" name="residence_address" id="residence_address" placeholder="address" />
  </div>
</div>

<div class="form-group">
  <div class="col-sm-4 padding-top-10">
    <label for="occupation" class="control-label">Occupation:</label>
    <input type="text" class="form-control" name="occupation" data-parsley-pattern="^[a-zA-Z ]+$" id="occupation" placeholder="your occupation" />
  </div>
  <div class="col-sm-8 padding-top-10">
    <label for="addressOfEmployer" class="control-label">Address of employer:</label>
    <input type="text" class="form-control" name="addressOfEmployer" data-parsley-minlength="10" data-parsley-trigger="keyup" id="addressOfEmployer" placeholder="address of your employer" />
  </div>
</div>
[/code]

I have also a JS part:

function yesnoCheck() { if (document.getElementById('yesCheck').checked) { document.getElementById('ifYes').style.display = 'block'; } else { document.getElementById('ifYes').style.display = 'none'; } }

And the php page:
[php]<?php
require_once(‘vendor/autoload.php’);

define(‘EMAIL_TO’, ‘[email protected]’ );

$firstName = filter_input(INPUT_POST, ‘firstName’);
$lastName = filter_input(INPUT_POST, ‘lastName’);
$dateofBirth=filter_input(INPUT_POST, ‘dateofBirth’);
$gender=filter_input(INPUT_POST, ‘gender’);
$country=filter_input(INPUT_POST, ‘country’);
$email = filter_input(INPUT_POST, ‘email’);
$phone= filter_input(INPUT_POST, ‘phone’);
$address=filter_input(INPUT_POST, ‘address’);
$englishLevel=filter_input(INPUT_POST, ‘englishLevel’);
$profession=filter_input(INPUT_POST, ‘profession’);
$remarks=filter_input(INPUT_POST, ‘remarks’);

$first_name = filter_input(INPUT_POST, ‘first_name’);
$last_name = filter_input(INPUT_POST, ‘last_name’);
$dateof_birth=filter_input(INPUT_POST, ‘dateof_birth’);
$placeof_birth=filter_input(INPUT_POST, ‘placeof_birth’);
$nationality=filter_input(INPUT_POST, ‘nationality’);
$passaport=filter_input(INPUT_POST, ‘passaport’);
$dateofIssue=filter_input(INPUT_POST, ‘dateofIssue’);
$placeofIssue=filter_input(INPUT_POST, ‘placeofIssue’);
$dateofExpiry=filter_input(INPUT_POST, ‘dateofExpiry’);
$residence_address=filter_input(INPUT_POST, ‘residence_address’);
$occupation=filter_input(INPUT_POST, ‘occupation’);
$addressOfEmployer=filter_input(INPUT_POST, ‘addressOfEmployer’);

$data= "Name: " . $firstName . ’ ’ . $lastName . “\n” .
"Date of birth: " .$dateof_birth . “\n”.
"Gender: " .$gender . “\n”.
"Country: " . $country . “\n” .
"Email: " . $email. “\n” .
"Phone: " .$phone . “\n”.
"Address: " .$address . “\n” .
"Level of English: " .$englishLevel . “\n” .
"Occupation or profession: " .$profession . “\n” .
"Other remarks or questions: " .$remarks . “\n”.

"Name: " . $first_name . ' ' . $last_name . "\n" .
"Date of birth: " .$dateof_birth . "\n" .
"Place of birth: "  .$placeof_birth . "\n" .
"Nationality: " .$nationality . "\n" .
"Passaport: " .$passaport . "\n" .
"Date of issue: " .$dateofIssue . "\n" .
"Place of issue: " .$placeofIssue . "\n" .
"Date of expiry:" .$dateofExpiry . "\n" .
"Residence address: " .$residence_address . "\n" .
"Profession: " .$occupation . "\n" .
"Address of employer: " .$addressOfEmployer . "\n" ;

if( $firstName && $lastName && $dateofBirth && $gender && $country && $email && $phone && $address && $englishLevel && $profession && $remarks ) {
// Create the Transport
$transport = Swift_SmtpTransport::newInstance(‘localhost’, 25)
->setUsername(‘user’)
->setPassword(‘password’)
;

$mailer = Swift_Mailer::newInstance($transport);

//http://swiftmailer.org/docs/sending.html
// Create the message

$message = Swift_Message::newInstance()

// Give the message a subject
->setSubject('From CRCE ROMANIA - The Power of Nonformal form')

// Set the From address with an associative array
->setFrom(array('[email protected]' => 'CRCE ROMANIA'))

// Set the To addresses with an associative array
->setTo(array(EMAIL_TO))

// Give it a body
->setBody($data, 'text/plain');


	
$result = $mailer->send($message);	
header("Location: index.php?pagina=success");

} else {
var_dump(“xxx”);

}

[/php]

It looks like you have the question to display: none; - In my opinion I would have turned on and turn it off in with javascript. I would also do that with the visa input fields have display: block then turn it off via JavaScript. That way a person who turns off JavaScript will still be able to access the Visa portion.

As for you problem, you’ll need to find a way to get PHP to communicate with JavaScript and vice versa. I personally would use Ajax and Json, but someone here might have a better solution?

You have a lot going on causing some detail overload, I think.

One thing that stands out to me,
[php]


Do you need a visa to come to course venue?

YES


NO

[/php]

The selection has the same value… So, there is no way for the code to know what was selected.

[php]


Do you need a visa to come to course venue?

YES


NO

[/php]

Now, you have given it a value that it can determine what you want it to do. However, because you don’t give a default value, they could submit the form without answering this question. My version defaults the answer to ‘no’.

Next, you need to separate out the value of $data.

Since the top portion is always required,

[php]$data= "Name: " . $firstName . ’ ’ . $lastName . “\n” .
"Date of birth: " .$dateof_birth . “\n”.
"Gender: " .$gender . “\n”.
"Country: " . $country . “\n” .
"Email: " . $email. “\n” .
"Phone: " .$phone . “\n”.
"Address: " .$address . “\n” .
"Level of English: " .$englishLevel . “\n” .
"Occupation or profession: " .$profession . “\n” .
"Other remarks or questions: " .$remarks . “\n”.[/php]

We are good with leaving that portion as is. Now we need to check the value of yesno to see if we need to include the last section.
[php]
if ( $_POST[‘yesno’] == ‘yes’) {
$data .= "Name: " . $first_name . ’ ’ . $last_name . “\n” .
"Date of birth: " .$dateof_birth . “\n” .
"Place of birth: " .$placeof_birth . “\n” .
"Nationality: " .$nationality . “\n” .
"Passaport: " .$passaport . “\n” .
"Date of issue: " .$dateofIssue . “\n” .
"Place of issue: " .$placeofIssue . “\n” .
“Date of expiry:” .$dateofExpiry . “\n” .
"Residence address: " .$residence_address . “\n” .
"Profession: " .$occupation . “\n” .
"Address of employer: " .$addressOfEmployer . “\n” ;
}[/php]

Now, we are conditionally adding to the $data variable, only if they answered yes to the question.

Lastly,
[php] $result = $mailer->send($message);
header(“Location: index.php?pagina=success”);[/php]

You aren’t actually checking if the mail was sent. The value stored in $result will give you a integer value on whether the message was processed successfully.

[php]
if ( $mailer->send($message) ) {
header(“Location: index.php?pagina=success”);
} else {
// there was a problem sending the message. Do you want to let the user know?
}
[/php]

For the sake of auditing and a host of other reasons, like the being an issue with a mail server, I like to store things in databases. In the event of something happening, you can always get the entered data back from a database if a message was not received.It is entirely up to you, just something to think about.

Sponsor our Newsletter | Privacy Policy | Terms of Service