Identify and correct the error in each of the following PHP code examples:
a) <?php print( "Hello World" ); >
b) <?php
$name = "Paul";
print( "$Name" );
?>
Describe how input from an HTML5 form is retrieved in a PHP program.
Describe how cookies can be used to store information on
a computer and how the information
can be retrieved by a PHP script. Assume that cookies are not
disabled on the client.
a) The correct code is:
<?php
print "Hello World";
?>
b) The correct code is:
<?php
$name = "Paul";
print $Name;
?>
2)
How to retrieve form data sent via GET
When you submit a form through the GET method, PHP provides a superglobal variable, called $_GET. PHP uses this $_GET variable to create an associative array with keys to access all the sent information ( form data ). The keys is created using the element's name attribute values.
The $_GET Method Script: get-method.php
// Check if the form is submitted if ( isset( $_GET['submit'] ) ) { // retrieve the form data by using the element's name attributes value as key $firstname = $_GET['firstname']; $lastname = $_GET['lastname']; // display the results echo '<h3>Form GET Method</h3>'; echo 'Your name is ' . $lastname . ' ' . $firstname; exit; }
The GET method
<form action="get-method.php" method="get"> <input type="text" name="firstname" placeholder="First Name" /> <input type="text" name="lastname" placeholder="Last Name" /> <input type="submit" name="submit" /> </form>
Now let's take a look at the code ...
The PHP isset() function is used to determine if a variable is set and is not null.
Firstly, the isset() function checks if the form has been submitted by using the element's name attribute value "submit" (name="submit") as key and pass it to the $_GET[] superglobal variable. This is because the form data are stored in the $_GET[] superglobal array by PHP when it is submitted through the GET method.
Then the form field, first name and last name form data are retrieved by using the same method, passing their respective name attribute values into the $_GET['name as key'] array parameter, and each is assigned to a variable name that was used to display the results.
Using the POST
The form POST method sends information via HTTP header. All name/value pairs sent through this method is invisible to anyone else since all the information are embedded within the body of the HTTP request.
When you submit a form to a server through the POST method, PHP provides a superglobal variable called $_POST. The $_POST variable is used by PHP to create an associative array with an access key ($_POST['name as key']). The key is created automatically by PHP when the form is submitted. PHP uses the form field element name attribute (name="unique-name-here") to create the key.
The $_POST Method Script: post-method.php
// Check if the form is submitted if ( isset( $_POST['submit'] ) ) { // retrieve the form data by using the element's name attributes value as key $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; // display the results echo '<h3>Form POST Method</h3>'; echo 'Your name is ' . $lastname . ' ' . $firstname; exit; }
The POST method form
<form action="post-method.php" method="post"> <input type="text" name="firstname" placeholder="First Name" /> <input type="text" name="lastname" placeholder="Last Name" /> <input type="submit" name="submit" /> </form>
3)
Creating Cookies
Let’s now look at the basic syntax used to create a cookie.
<?php setcookie(cookie_name, cookie_value, [expiry_time], [cookie_path], [domain], [secure], [httponly]); ?>
HERE,
Let’s now look at an example that uses cookies.
We will create a basic program that allows us to store the user name in a cookie that expires after ten seconds.
The code below shows the implementation of the above example “cookies.php”.
<?php setcookie("user_name", "Guru99", time()+ 60,'/'); // expires after 60 seconds echo 'the cookie has been set for 60 seconds'; ?>
Output:
the cookie has been set for 60 seconds
Retrieving the Cookie value
Create another file named “cookies_read.php” with the following code.
<?php print_r($_COOKIE); //output the contents of the cookie array variable ?>
Output:
Array ( [PHPSESSID] => h5onbf7pctbr0t68adugdp2611 [user_name] => Guru99 )
Note: $_COOKIE is a PHP built in super global variable.
It contains the names and values of all the set cookies.
The number of values that the
$_COOKIE array can contain depends on the memory size set in php.ini.
The default value is 1GB.
Identify and correct the error in each of the following PHP code examples: a) <?php print(...
How can I print the Database table in PHP please ? here I have form for name and email, also I have php code that allow user to add his name and email to my database, all I need to print my final database when the user click on submit. <form action="" method="post"> <label>Name :</label> <input type="text" name="name" required="required" placeholder="Please Enter Name"/><br /><br /> <label>Email :</label> <input type="email" name="email" required="required" /><br/><br /> <input type="submit" value=" Submit " name="submit"/><br /> </form>...
Write a PHP script that obtains a URL and its description from user and stores the information into a database using MySQL. Create and run a SQL script with database named URL and a table named Urltable. The first field of the table should contain an actual URL, and the second, which is named Description, should contain a description of the URL. Use www.deitel.com as the first URL, and input Cool site! as its description. The second URL should be...
Identify a logical error in the following code and fix it. public class test1 { public static void main(String[] args){ int total; float avg; int a, b, c; a=10; b=5; c=2; total=a+b+c; avg=total/3; System.out.println("Average: "+avg); } } Answer: Write the output of the program below. import java.util.Scanner; public class test2 { public static void main(String[] arg){ int psid; String name; Scanner input=new Scanner(System.in); System.out.println("Enter your PSID"); psid=input.nextInt(); System.out.println("Enter your...
Question 1 The code used to output messages to the screen begins with run # print output Flag this Question Question 2 The code used to begin a comment in the Python program source code is # Hello * Comment Flag this Question Question 3 A variable name is like a(n) input typed on a keyboard address in computer memory where values can be stored output to a computer screen value assigned to an address in computer memory Flag this...
1. Print out information of PHP use phpinfo() function. 2. Write a program that check and print odd / even numbers (from number 1 to 100 using for/while loop). Display the results within an HTML table with 2 columns as shown below: NUMBERS RESULTS 1 ODD 2 EVEN 3 ODD HINT: use <table> tags to create a table, <th> tags for ‘Numbers’ and ‘Results’. Wrap code PHP inside HTML code. For example: <html> <title>CHECK ODD or EVEN</title> <body> <table> <?php...
This code should be written in php. Write a program that allows the user to input a student's last name, along with that student's grades in English, History, Math, Science, and Geography. When the user submits this information, store it in an associative array, like this: Smith=61 67 75 80 72 where the key is the student's last name, and the grades are combined into a single space-delimited string. Return the user back to the data entry screen, to allow...
#1. Operators and values, expressions Please correct the following code to make it print the given expressions and their answers well (3pts). public class DataTypes { public static void main(String[] args) { System.out.println("5*8/6 = " + 5*8/6); System.out.println("(2*6)+(4*4)+10 = " + (2*6)+(4*4)+10); System.out.println("6 / 2 + 7 / 3 = " + 6 / 2 + 7 / 3); System.out.println("6 * 7 % 4 = " + 6 * 7 % 4 ); System.out.println("22 + 4 * 2 = "...
Q1. rewrite the exercise on April 4 by adding the
following operations:
1) In the php script, create an associative array named
$order that stores the following values:
- the number of cheese toppings;
- the number of pepperoni toppings;
- the number of
ham
toppings;
- the size of the ordered pizza;
- the number of the ordered pizza;
- the total cost of the order;
- the discount rate;
- the total cost after the discount.
2) the...
Unit 1 Programming Assignment Follow the directions for each of the following questions. Answer the question or provide the code in a space below the question. 1. Write the complete script tag set for a script whose line statement is document.write(“Hello, world.”); 2. Build a complete HTML document and include the answer to the previous question such that the page executes the script as the page loads. Open the document in your browser to test the results. 3. Add a...
To insure that file output is written to the disk you need to execute what method? a. commit() b. write() c. close() d. complete() The following is called the ________ of a function. def myFunction(x,y) : a. header b. footer c. sentinel d. index True or False: A commonly used program that uses regular expressions is grep. True or False: In Python exception handling provides a mechanism for passing control from the point of the error detection to a handler...