Question
M5 Dynamic Content (Take 2) PLEASE NOTE: You may find that the Processing Forms section of Chapter 7 of Programming PHP (assigned in this module's
M5 Dynamic Content (Take 2) PLEASE NOTE: You may find that the "Processing Forms" section of Chapter 7 of Programming PHP (assigned in this module's readings) is critical for this assignment. You should read it prior to attempting this assignment. In this assignment, you will be developing a new version of the dynamic web form from assignment 3.2. This time, you'll use PHP to dynamically display the selected form, instead of Javascript. The advantage of using a server-side script, like PHP, to dynamically display the appropriate form is that it will work regardless of whether JavaScript is turned on or off in the user's browser. In addition, you'll write your own PHP script to to both validate the form data and also send out the email, instead of using the form-to-email script that I provided for your use earlier in the course. As in assignment 3.2, this assignment has two separate but inter-related tasks, and I recommend you work on the assignment in order. Part I: Dynamic Content via PHP In the first half of this assignment, you will create a "self-processing" PHP page that will initially just display a simple form with a drop-down select box that lets the user choose between the feedback and help request forms. When the user makes a selection, instead of using JavaScript to dynamically build or reveal the appropriate submission form, the form will be submitted back to the same page on the server for further processing. The PHP page will use a PHP script to check if the user has submitted a choice, and if so it will dynamically display the appropriate form. Start by creating a new document with the name "assignment5-2.php". Other than the name ending in .php instead of .html, this should have the regular XHTML contents (your XML declaration, DTD statement, XML namespace, < Head > and < Body > sections, etc.). Like assignment 3.2, there should be an initial form on the web page with a drop down select box that provides the user with two choices: a feedback form or a help request form. However, unlike assignment 3.2, this form must have two key changes: Use the "get" method for the form Use an action of "assignment5-2.php" (or whatever filename you use for the PHP file, so that it is effectively submitting itself back to itself). Now, when that form gets submitted by the user, it will send the form's contents (ie, the user's choice) back to the server as described in Chapter 7 of the PHP text, and you will be able to access the form's contents through the $_GET array. The next step is to add a PHP script to this same page that checks if the $_GET array has a value for your select box defined. If your select field contents don't exist in the $_GET array, then the user hasn't submitted a choice yet, and your page should not display either type of form. If a value for your select field is present in the $_GET array, however, it will be set to either the help or feedback value. In that case your PHP code should then display the appropriate form and associated instructions. Displaying the chosen form should now take place only via your PHP code, and not using the JavaScript we used in module 3. I suggest you re-read the document "Creating Dynamic Pages with PHP" in last the module's Readings area to see how your existing XHTML code for the forms can be inserted into the PHP code. You can insert the XHTML code for your forms (including your form element, fields, text labels, instructions, etc) into the middle of your PHP script as described in that document. Finally, you must change the action attribute of your help and feedback forms to use the new PHP form-to-email page that you will be creating in part two (below), instead of using the form-to-email script I provided for use earlier in the course. Instead of having an action attribute of "http://cdlwebsysdev.esc-atsystems.net/WSD/form-to-email.php", change them to be just "form-to-email.php". Note that this assumes that your new "form-to-email.php" page, which you'll create in part two, below, will be placed in the same directory on the web server as your assignment5-2.php file. If you decide you want your "form-to-email.php" file kept in a different directory than your "assignment5-2.php" file, you'll need to specify the full path in the action attributes. NOTE: If you are having trouble getting your forms to work, here's a troubleshooting hint: use the "View Source" capability in your browser to see the actual XHTML code that is being sent to the browser from the PHP script. Looking at the PHP code itself won't always help determine why something isn't working right in the browser, since it is only the final XHTML code that gets sent to the browser which will determine whether the form works. Part II: Form Processing in PHP for Data Validation and Emailing Next, you need to create a new PHP page that will be used to capture the contents from the help or feedback forms, validate the data, and then (if valid), email it to the designated people. This new PHP page will effectively replace the form-to-email script I provide for your use in the earlier assignments. Start by creating a new document with the name "form-to-email.php". Other than the name ending in .php instead of .html, this should have the regular XHTML contents (your XML declaration, DTD statement, XML namespace, < Head > and < Body > sections, etc.). In this new PHP page, you will need to have PHP code to perform data validation, and then send the email if the submitted information is valid. You will be able to access the contents of the submitted form through the $_POST array. Write PHP code to perform all the required data validation: check for the required fields, check the format of data when appropriate, etc. This processing page should then either notify the user of invalid data with instructions on what needs to be fixed, or send valid data to the appropriate recipients in an email, as shown here: Sending Email with PHP To send an email on this PHP server, you will need to define five variables: $to should have the email address of the primary recipient. $cc should have the email address of any other recipients (like the instructor's email, when you are ready to submit the assignment). $subject should have an appropriate subject for the email, in the standard format of "WSD: Assignment #.# - Web Form for Firstname Lastname" $from should have the user's email address as submitted from the help or feedback form. $message should be a string containing the full text of the body of the email. Once you've defined those five variables, you can then send the email using the following function call: mail($to, $subject, $message, "From: $from CC: $cc"); The first three variables listed above can either be defined directly in the PHP script of your form processor page, or you can extract them from hidden fields in your forms (like we've been doing in earlier assignments, where the form-to-email script set $to based on the ToAddress hidden field, etc.). The final two variables must be based on the data the user submits via your form. When you are preparing to send the email, you will have to add the contents of all the appropriate fields in your submitted form to the $message string for the mail function. Your evaluation will consider whether or not your final email is easily readable, so you should include labels and effective use of white spaces and newlines ( ) when you are concatenating your fields together into the message string. And please, when testing your code, make sure the instructor's email is not in the $cc variable! Preventing Spam Emails Your PHP script is also required to have the following code to prevent spammers from using this server as a mail relay. I recommend you put this code near the top of your page, but it MUST come prior to the mail() function described above Stop the script if it is being called from an external URL Get the referring URL $referer = strtolower($_SERVER['HTTP_REFERER']); Define the domain name of this page $this_url = "http://cdlwebsysdev.esc-atsystems.net/"; If the referring URL and the URL of this page don't match then display a message and don't send the email. if ($this_url != substr( $referer, 0, 19) ) { echo " Error! You do not have permission to use this script from another URL. "; exit; } When your assignment is complete, add your instructor's email to the list of cc recipients, then submit one sample feedback and one sample help request so that I will receive a copy of them in an email generated by your form-to-email script. Finally, add a link to this page to your WSD Portal page, then come back here and use the form below to submit a link to your assignment and the attached PHP files. It is critical that you attach a copy of your PHP files because I cannot see your code in a web browser. Please see the "Student How-To's" module for instructions on How to submit your assignment. Expert Answer
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started