How to use GET/POST method In Javascript
This example uses the form element and GET/POST method to pass JavaScript variables to PHP. The form of contents can be accessed through the GET and POST actions in PHP. When the form is submitted, the client sends the form data in the form of a URL such as:
https://example.com?name=value
This type of URL is only visible if we use the GET action, the POST action hides the information in the URL.
Client Side:
<!DOCTYPE html>
<html>
<head>
<title>
Passing JavaScript variables to PHP
</title>
</head>
<body>
<h1 style="color:green;">
w3wiki
</h1>
<form method="get" name="form" action="destination.php">
<input type="text" placeholder="Enter Data" name="data">
<input type="submit" value="Submit">
</form>
</body>
</html>
Server Side(PHP):
On the server side PHP page, we request for the data submitted by the form and display the result.
<?php
$result = $_GET['data'];
echo $result;
?>
Output:
How to pass JavaScript variables to PHP ?
JavaScript is the client side and PHP is the server-side script language. The way to pass a JavaScript variable to PHP is through a request.
Below are the methods to pass JavaScript variables to PHP:
Table of Content
- Using GET/POST method
- Using Cookies to store information
- Using AJAX