Question: PHP EXERCISE 4: RECEIVING VALUES FROM FORMS Here is a minimal HTML form to send the same values to the time1.php page as we previously

PHP

EXERCISE 4: RECEIVING VALUES FROM FORMS

Here is a minimal HTML form to send the same values to the time1.php page as we previously sent in the URL. Save this is a file called form1.html and upload it to the server alongside time1.php.

Form 1

First name

Last name

Save the following in a file called post.php.

Post Form Receiver

if ( isset( $_POST['firstname']) && isset( $_POST['lastname'])) {

print "

Hello ".$_POST['firstname']." ".$_POST['lastname']."!

";

} else {

print "

You didn't tell me your first and last names!

";

}

?>

This is the same code as used in time1.php but converted to receive post value instead of get. Change the form in form1.html to point at this script and use the post method.

Now try this script. Save it with the name autoform.php and view it in the web browser.

Post Form Receiver

if ( isset( $_POST['firstname']) && isset( $_POST['lastname'])) {

print "

Hello ".$_POST['firstname']." ".$_POST['lastname']."!

";

print "

Go again!

";

} else {

?>

First name

Last name

}

?>

Once you have experimented with this script, carefully examine the code and write an explanation of how it works in your report. This technique, where a PHP script both displays the form AND receives the input from it is very commonly used when building web applications.

Heres a similar form, but tagged up with w3css classes to make it look nice. It also includes a textarea message field. All three input fields have the required attribute. Find out what this does and mention it in your report.

Contact Form

Contact Form

Save this code in a file called contact-form.html. Note that this form is pointing at a script called sendemail.php.

The code for sendemail.php is below, but it needs fixing in various ways. Save it in a file called sendemail.php, make the required changes (see instructions after the code) and upload it together with contact-form.html to the web server.

Email Confirmation

Contact Confirmation Screen

// FIX THIS

$message = "This needs to be changed to be whatever the user enters.";

// CHANGE THE LINE BELOW TO AN EMAIL ADDRESS YOU RECEIVE MESSAGES AT.

$toaddress = "noOne@NoWhere"; // PLEASE don't leave noOne@NoWhere in this - it won't work...

// create a subject line for the email - THIS SHOULD GIVE THE NAME ENTERED INTO THE FORM

$subjectline = "Message from John Smith";

// set the from address for the email; this is required

$headers = "from:bus.support@lsbu.ac.uk "; // Daydream will send an email only if this header is present

$success = false;

// Send the email. The mail command is commented out. Remove the // to activate it.

// It is commented out because if the server does not support sending mails it might crash the script.

//$success = mail( $toaddress, $subjectline, $message, $headers);

// THIS TEXT SHOULD ALSO GIVE THE NAME ENTERED BY THE USER, NOT John Smith.

print "

Thank you John Smith, the following message has been sent to the website owner:

";

print "

";

print "

Subject: ".$subjectline."

";

print "

".$message."

";

print " ";

if ( !$success) {

print "

Error: Message not sent!

";

}

?>

You will need to fix the following (all highlighted in red in the code above);

The text of the message should come from the form input.

The value for the variable $toAddress should be a real email address you own. This is where the email will get sent.

The subject line should not say John Smith, but give the names the user entered on the form.

The thank you message should also not say John Smith, but the name entered on the form.

The mail command itself has been disabled by preceding it with the comment symbol //. Once you have fixed all the other problems listed above and tested that it looks right, remove the // before the mail command and hopefully you will actually receive the email at the address you gave for $toAddress.

EXERCISE 4: RECEIVING VALUES FROM FORMS

Required in your report (basic exercise).

An explanation of how the autoform.php script works.

An explanation of what the required attribute does in HTML form fields.

Reflection on the tasks and all activities involved.

Extended Tasks

Add other HTML form elements to the contact form and include their values in the email. Add select, radio-buttons and check boxes.

Follow the tutorial at http://www.w3schools.com/php/php_form_validation.asp and demonstrate that neither of the forms developed above are secure from users entering special characters.

Improve the forms in the light of this by use of htmlspecialcharacters() function.

Hey can you do the basic and Extended Tasks please?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!