Hi friends I am going to discuss Access $_GET or $_POST or $_SESSION using javascript.
In other words access parameters send by form as POST or GET method or
Read the session variables in the server using javascript.
1.Access $_GET values using javascript
Code
json_encode() is the core function using here.In the javascript section a variable named $_GET
is declared.This variable is assigned with the $_GET array of php page.This is done by this statement :
var $_GET = <?php echo json_encode($_GET); ?>;
Dont forget the semicolon at the end.
All the parameters passed as GET to the page will stored in the variable.These values can be retrived
by their name.That is the name of the textbox in the html page.Here my textbox's name is 'n1'.So I use
$_GET["n1"] to access the value of n1.
save it as a php file(.php extension) and run.
2.Access $_POST values using javascript
Code Description
json_encode() is the core function using here.In the javascript section a variable named $_POST
is declared.This variable is assigned with the $_POST array of php page.This is done by this statement :
All the parameters passed as POST to the page will stored in the variable.These values can be retrived
by their name.That is the name of the textbox in the html page.Here my textbox's name is 'n1'.So I use
$_POST["n1"] to access the value of n1.
save it as a php file(.php extension) and run.
Code Description
json_encode() is the core function using here.
First of all in a php section we call the function session_start().This is for accessing SESSION variable in php.
In the javascript section a variable named $_SESSION is declared.This variable is assigned with the $_SESSION array of php page.
This is done by this statement :
var $_SESSION = <?php echo json_encode($_SESSION); ?>;
Dont forget the semicolon at the end.
All the SECTION variables will be stored in the variable $_SESSION.These values can be retrived
by their name.Here my section variable's name is 'ns'.So I use
$_POST["ns"] to access the value of 'ns'.
save it as a php file(.php extension) and run.
You can also read :-
In other words access parameters send by form as POST or GET method or
Read the session variables in the server using javascript.
1.Access $_GET values using javascript
Code
<html>
<head>
<script type="text/javascript">
var $_GET = <?php echo json_encode($_GET); ?>;
alert($_GET["n1"]);
</script>
</head>
<body>
<form method="GET">
<input type="text" name="n1">
<input type="submit">
</form>
</body>
</html>
<head>
<script type="text/javascript">
var $_GET = <?php echo json_encode($_GET); ?>;
alert($_GET["n1"]);
</script>
</head>
<body>
<form method="GET">
<input type="text" name="n1">
<input type="submit">
</form>
</body>
</html>
Code Description
json_encode() is the core function using here.In the javascript section a variable named $_GET
is declared.This variable is assigned with the $_GET array of php page.This is done by this statement :
var $_GET = <?php echo json_encode($_GET); ?>;
Dont forget the semicolon at the end.
All the parameters passed as GET to the page will stored in the variable.These values can be retrived
by their name.That is the name of the textbox in the html page.Here my textbox's name is 'n1'.So I use
$_GET["n1"] to access the value of n1.
save it as a php file(.php extension) and run.
2.Access $_POST values using javascript
Code
<html>
<head>
<script type="text/javascript">
var $_POST = <?php echo json_encode($_POST); ?>;
alert($_POST["n1"]);
</script>
</head>
<body>
<form method="POST">
<input type="text" name="n1">
<input type="submit">
</form>
</body>
</html>
<head>
<script type="text/javascript">
var $_POST = <?php echo json_encode($_POST); ?>;
alert($_POST["n1"]);
</script>
</head>
<body>
<form method="POST">
<input type="text" name="n1">
<input type="submit">
</form>
</body>
</html>
Code Description
json_encode() is the core function using here.In the javascript section a variable named $_POST
is declared.This variable is assigned with the $_POST array of php page.This is done by this statement :
var $_POST = <?php echo json_encode($_POST); ?>;
Dont forget the semicolon at the end.All the parameters passed as POST to the page will stored in the variable.These values can be retrived
by their name.That is the name of the textbox in the html page.Here my textbox's name is 'n1'.So I use
$_POST["n1"] to access the value of n1.
save it as a php file(.php extension) and run.
3.Access $_SESSION values using javascript
Code
<?php session_start(); ?>
<html>
<head>
<script type="text/javascript">
var $_SESSION = <?php echo json_encode($_SESSION); ?>;
alert($_SESSION["ns"]);
</script>
</head>
<body>
<?php
$_SESSION["ns"]="SESSION_VALUE";
?>
<form method="POST">
<input type="text" name="n1">
<input type="submit">
</form>
</body>
</html>
<html>
<head>
<script type="text/javascript">
var $_SESSION = <?php echo json_encode($_SESSION); ?>;
alert($_SESSION["ns"]);
</script>
</head>
<body>
<?php
$_SESSION["ns"]="SESSION_VALUE";
?>
<form method="POST">
<input type="text" name="n1">
<input type="submit">
</form>
</body>
</html>
Code Description
json_encode() is the core function using here.
First of all in a php section we call the function session_start().This is for accessing SESSION variable in php.
In the javascript section a variable named $_SESSION is declared.This variable is assigned with the $_SESSION array of php page.
This is done by this statement :
var $_SESSION = <?php echo json_encode($_SESSION); ?>;
Dont forget the semicolon at the end.
All the SECTION variables will be stored in the variable $_SESSION.These values can be retrived
by their name.Here my section variable's name is 'ns'.So I use
$_POST["ns"] to access the value of 'ns'.
save it as a php file(.php extension) and run.
You can also read :-
Thank u nice script
ReplyDelete