PHP URL and Pass values through URL
From w3cyberlearnings
Contents |
You need to pass value through URL
- To get the value through the URL you need to use the $_GET method
URL special character
- Use the urlencode() method to the string for the URL special character(see example 2 for detail).
Example 1
student.php page
- Pass the student name and class to the URL
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <a href="student_list.php?name=Paul&class=C">Paul Information</a> </body> </html>
student_list.php page
- Get the student name and class in the URL
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>List</title> </head> <body> <?php echo 'Student name: ' . $_GET['name'] . ' and student class: ' . $_GET['class']. '<br/>'; ?> </body> </html>
display result
Student name: Paul and student class: C
Example 2
user.php page
- You need to use the urlencode() method to encode the string to the URL special character.
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>All Users</title> </head> <body> <?php $user = array( 'Bob Kinita', 'Lily Chan', 'David Philip', 'Janny Joe' ); foreach ($user as $u) { echo '<a href="user_list.php?name=' . urlencode($u) . '">' . $u . 'Information</a><br/>'; } ?> </body> </html>
user_list.php page
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>List User</title> </head> <body> <?php echo 'Student name: ' . $_GET['name']. '<br/>'; ?> </body> </html>
Related Links
- Drop down list
- Pass value uses URL
- Pass value uses URL 2.
- Multiple Submit Buttons