Jump to: navigation, search

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


  1. HTML POST
  2. HTML GET
  3. HTML Textarea
  1. HTML Hidden Field
  2. HiddenField Multiple Pages
  3. HTML Password
  1. HTML Textbox
  2. HTML Checkbox
  3. HTML Radio
  1. Drop down list
  2. Pass value uses URL
  3. Pass value uses URL 2.
  4. Multiple Submit Buttons
Navigation
Web
SQL
MISC
References