Transcript Slide 1
Website Development with PHP and MySQL Saving Data What you will achieve this week! •Submitting data to the server •Saving data on a server using files Reminder of the general process : Customer browser web server scripting language database request service access page interpret set data get data get data return html present html What can we do with Forms? • Standard HTML tags which: – Collect user input – Specify where to send the input – Specify how to send the input to be processed • HTML tags simply provide an interface to allow user interaction • Input is handled by a separate script or program: – On the server • e.g. via PHP, ASP, CGI etc. – In the browser • e.g. JavaScript More Input Types • Radio buttons Choose ONE of the following: <input type="radio" name="choice" value="A" checked="checked" />A <input type="radio" name="choice" value="B" />B <input type="radio" name="choice" value="C" />C • Check boxes Choose ANY, ALL or NONE of the following: <input type="checkbox" name="A" value="yes" checked="checked" />A <input type="checkbox" name="B" value="yes" />B <input type="checkbox" name="C" value="yes" />C Even More Input Types • Drop-down menus • Text area Secret Stuff? • Passwords <p> Care to give us a password? <input type="password" name="my_password" /> </p> • Hidden fields <p> <input type="hidden" name="my_secret" value="hello" /> </p> Submit and Reset Buttons • The submit button sends form data to be processed <p> <input type="submit" value="submit" /> </p> • The reset button returns fields to original state <p> <input type="reset" value="reset" /> </p> How to get data from a form to a program • Specify how to send the form data – Specified with the method attribute – GET or POST <form method="get"> or <form method="post"> – If not specified – default is GET • Specify where to send the form data – Specified by the action attribute – Usually a script or program – can be local or full URL <form method="get" action="formscript.php"> <form method="get" action="http://www.hud.ac.uk/ resources/scripts/formscript.php"> The GET Method • Client makes an HTTP GET request for a script – Form data is appended to the script URL as name/value pairs – a querystring – The script has access to values sent in the querystring • In addition – Input data can be hard-coded directly in a URL • No need for a form – Results can be bookmarked The POST Method • Client makes an HTTP POST request for a script – Form data is sent as name/value pairs in the body of the request • In addition – Useful for large amounts of data • Browser may truncate a long querystring – Data is not visible in the URL – Results cannot be bookmarked Form Processing Scripts • Scripts are run by the web server via a web server application – CGI, PHP, ASP, ColdFusion etc… • Scripts can be written in almost any language – PHP, VBScript, Perl, CFML etc… – Language may depend on web server application(s) • Scripts receive form input from via GET or POST – Process input based on instructions in the script • • • • Create an e-mail message Perform database query Write information to a file Generate a web page Form Processing User submits form to web server Name: Arnold Comments: “Nice website!" Web server passes form data to processing script Send email? Return a web page? <html> ... </html> Submitting data to server through forms Simple examples that echo the submitted data back simpleTextBox.htm : customer webServer echoName.php enter name submit submit(echoName.php3) open($name) display <html> <head></head> a PHP page to process the form <body> <form method="POST" action="echoName.php"> <p>Name<input type="text" name="name" size="20"></p> <p><input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"></p> </form> </body> </html> a variable to pass to the PHP page test echoName.php calculator.htm : customer webServer calculatorResult.php enter numbers submit submit(calculatorResult.php3) open($num1,$num2) display <html> <head></head> <body> <form method="POST" action="calculatorResult.php"> <p>Number 1<input type="text" name="num1" size="20"></p> <p>Number 2<input type="text" name="num2" size="20"></p> <p><input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"></p> </form> </body> </html> test calculatorResult.php Saving data using files : customer nameAddress.htm webServer saveAddress.php nameAddress enter name enter address submit submit(saveAddress.php) open($name,$address) open write close display int fopen (string filename, string mode) mode may be any of the following: 'r' - Open for reading only; place the file pointer at the beginning of the file. 'r+' - Open for reading and writing; place the file pointer at the beginning of the file. 'w' - Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'w+' - Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'a' - Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. string sprintf (string format [, mixed args...]) The format string is composed of zero or more directives: ordinary characters (excluding %) that are copied directly to the result, and conversion specifications, each of which results in fetching its own parameter. b - the argument is treated as an integer, and presented as a binary number. c - the argument is treated as an integer, and presented as the character with that ASCII value. d - the argument is treated as an integer, and presented as a decimal number. f - the argument is treated as a double, and presented as a floating-point number. o - the argument is treated as an integer, and presented as an octal number. s - the argument is treated as and presented as a string. e.g. sprintf(“%d is the number of %s in a %y”, 12, “months”, “year”) results in “12 is the number of months in a year” <html> <head></head> <body> <form method="POST" action="saveAddress.php"> <p>Name <input type="text" name="name" size="40"></p> <p>Address <input type="text" name="address" size="60"></p> <p><input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"></p> </form> </body> </html> Test saveAddress.php Guestbook Application We need: • A form • A file (or database table) to hold the data entered via the form • Some means of reading the data from the file so that we can see it We will look into this case study further when we move onto databases. So, what have we got so far? A means of collecting data from the user A means of transmitting it to the server A means of recording it on the server