Verification: a143cc29221c9be0

Pass var to function php

Use GET and POST Through HTML Form

HTML form is one of the most powerful features of PHP. Any form element will be automatically available to the action destination of the form.

POST Request

Fetching data to nextPage.php

$username = isset($_POST['username']) ? $_POST['username'] : "";
$email       = isset($_POST['email']) ? $_POST['email'] : "";
echo "Username: ".$username;
echo "Email: ".$email;

The sample output of the script may be:

Username: johndoe
Email: johndoe@gmail.com

The example above shows how to pass a variable using POST through an HTML form. The form element needs to have the action and method attribute. The action contains the next page, in this case, it’s nextPage.php. The method can be a POST or GET. You can then access the elements in the nextPage.php using a $_POSTor $_GET.

GET Request

Bring me to nextPage

This example will create a GET variable and can be accessed on nextPage.php.

Example:

echo $phpVariable = $_GET['phpVariable'];
//output: Dog

The GET can be accessed using $_GET

Another way is to add a hidden element in an HTML form that submits to the next page.

Example:

nextPage.php

//Using POST
$phpVariable = $_POST['phpVariable'];
//Using GET
$phpVariable = $_GET['phpVariable'];
//Using GET, POST or COOKIE;
$phpVariable = $_REQUEST['phpVariable'];

You can change the method from POST to GET to use the GET request. POST and GET are bot insecure, but GET is easier to hack since it’s available through the front-end.

The $_REQUEST can both accept GET, POST or COOKIE. It’s good to use $_REQUEST on self-referential forms for validations.

Use session and cookie

The session and cookie are easier to use, but the session is far more secure than the cookies, but not completely secure.

session

//page 1
$phpVariable = "Dog";
$_SESSION['animal'] = $phpVariable;

//page 2
$value = $_SESSION['animal'];

Note

When using the session, always remember to add session_start() on both pages before accessing the $_SESSION array.

//page 1
$phpVariable = "Dog";
$_COOKIE['animal'] = $phpVariable;

//page 2
$value = $_COOKIE['animal'];

The most obvious difference between cookies and session is that that session will be stored on the server-side while the cookies will have the client side as the storage.

Contribute

DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Use AJAX to Pass the PHP Variable to JavaScript

We can use AJAX to get the data and variables from the PHP server to JavaScript. This method has separate server-side and client-side scripts. It makes the code cleaner and enhances the code readability. AJAX stands for Asynchronous JavaScript and XML. It uses the XMLHttpRequest object to request the data from the server. It displays the data using JavaScript and HTML DOM. This process occurs asynchronously, which means the changes happen in the webpage without the page being reloaded. However, there is latency when AJAX is used over networks because it sends the HTTP request. In this method, we write JavaScript code in the PHP file.

For example, create a PHP file named index.php. Write the JavaScript code inside the tag. Create a vairable req using the var keyword. Create a request object of the XMLHttpRequest() function and assign it to the req variable. Call the onload() function with the req object using the function expression. Inside the function, use the console.log() function to log the this.responseText property. Do not forget to end the function expression with a semicolon. Use the req object to call the open() function. Supply get, get-data.php and true simultaneously as the parameters in the function. Call the send() function with the req object. Create another PHP file named get-data.php. Assign a value of Mountain to $var variable. Echo the json_encode() function with the $var as the parameter.

In the example below, we created a request object with the XMLHttpRequest() function. The onload() function handles the operation to be performed during the response. The open() function specifies the HTTP method, the PHP file that sends the JSON response, and a boolean value that controls the rest of the execution of the script. The json_encode() function in the get-data.php file sends the response in JSON format. Here, we printed the variable $var in the console.

Example Code:

# php 7.*
# php 7.*

Output:

"Mountain"

Use JavaScript to Escape the PHP Scripts to Pass the PHP Variable to JavaScript

We can use JavaScript after the PHP tags to escape the PHP scripts. We can create a PHP variable in the PH P script. Then, we can write the JavaScript to escape the above-written PHP and pass the variable inside the JavaScript code. Inside the JavaScript, we can use the PHP tag to echo the PHP variable assigning it to a JavaScript variable.

For example, assign a value Metallica to a variable $var inside the PHP tags. Write a script tag and inside it, write a PHP tag. Inside the PHP tag, echo a JavaScript variable jsvar. Assign the PHP variable $var to jsvar. Do not forget a quote wrapped around the $var and a semicolon after it. Close the PHP tag and log the value of jsvar using the console.log() function. Close the script tag. In the example below, we have logged the PHP variable $var in the console using JavaScript. We can see the output in the console of the webpage.

Example Code:

#php 7.x

Output:

Metallica

Пример использования

HTML

Результат: