Backend Operations in PHP from 0 to Hero(Pt. 2 passing variables and user input)
Tags: php, devops, programming, webdev
Published: 2021-08-30 14:28:56

In the [last tutorial](https://dev.to/code_jedi/backend-operations-in-php-from-0-to-hero-pt-1-simple-operations-pop), we made a PHP script that performs simple backend operations.

**In part 2, we'll be making a PHP script that will perform customizable backend operations by getting user input and passing it to our backend script**

**************
### Let's get started!

First, let's create a form that will accept a `url` and a `tag` parameter:
```html
<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Simple operations</title>
  </head>
  <body>
  <form method="post" action="results.php">
  url: <input type="text" name="url"><br>
  tag: <input type="text" name="tag"><br>
  <button type="submit" >Submit</button>
  </form>
  </body>
</html>
```

*************
Once these parameters are accepted, they will passed-on to `results.php`, here's how it will look like:
```php
<?php
$url = $_POST["url"];
$tag = $_POST["tag"];
$output = exec("python3 op1.py $url $tag");
echo("<h1>$output</h1>");
?>
```
**What's going on here?**
* First, `results.php` defines the `$url` and `$tag` variables as the ones passed-on by the form we made earlier.
* It then executes `op1.py` using the [exec function](https://www.php.net/manual/en/function.exec.php) and passes the `$url` and `$tag` variables to `op1.py`.
* It finally echos the output of this exec function inside an `<h1>` tag.

***********
#### Here's where the magic happens

`results.php` passes the `$url` and `$tag` variables to `op1.py`, executes the python script and returns it's output.

One big question you might have is: 
_**"How is python able to work with PHP variables?"**_
![What?](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5nis82hah8kjn48ruzer.gif)

**Answer:** Using the `sys` library.
Let me show you:
```python
import sys
import requests
from bs4 import BeautifulSoup

url=sys.argv[1]
response = requests.get(url)

soup = BeautifulSoup(response.text, 'html.parser')
element = soup.find('body').find(sys.argv[2])
print(element)
```
This is a python script which scrapes webpages for certain HTML tags such as `h1` `div` and `p`, and outputs the text contents within the first occurrence of the specified HTML tag within the webpage.

As you might remember, `results.php` passed first the `$url` variable, then the `$tag` variable, therefor, when we define the `url` variable in our python script, we call `sys.argv[1]` to in turn get the first variable that was passed by the PHP script, which is the `$url` variable.

**********
Next, when defining the tag which we want to scrape for text contents within the specified URL's webpage, we call `sys.argv[2]` to get the second variable passed by `results.php`, which is the `$tag` variable.

#### The same can be done with Node.js like so:

**results.php**
```php
<?php
$url = $_POST["url"];
$tag = $_POST["tag"];
$output = exec("url=$url tag=$tag node op1.js");
echo("<h1>$output</h1>");
?>
```

**op1.js**
```javascript
console.log('URL: ' + process.env.url + ' | tag: ' + process.env.tag)
```
**Result**
![Node.js output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1s2jvnljb5dntxctch70.png)
*********
*********
Full code:

**index.php**
```php
<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Simple operations</title>
  </head>
  <body>
  <form method="post" action="results.php">
  url: <input type="text" name="url"><br>
  tag: <input type="text" name="tag"><br>
  <button type="submit" >Submit</button>
  </form>
  </body>
</html>
```

*********
**results.php**
```php
<?php
$url = $_POST["url"];
$tag = $_POST["tag"];
$output = exec("python3 op1.py $url $tag");
echo("<h1>$output</h1>");
?>
```

*********
**op1.py**
```php
import sys
import requests
from bs4 import BeautifulSoup

url=sys.argv[1]
response = requests.get(url)

soup = BeautifulSoup(response.text, 'html.parser')
element = soup.find('body').find(sys.argv[2])
print(element)
```

*********

**Now if you open a terminal in your project's directory, run `php -S localhost:8000` and open the respective url in your browser, here's how our code should work:**
![index.php](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azuydiuqysatn25xn7m3.png)
_These form parameters are passed on to `results.php` which the user is then redirected to
`results.php` then takes those parameters, passes them to `op1.py`, executes the respective python script and returns it's output inside an `<h1>` tag which in this case is "PHP tutorial"._
![results.php](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r7jzw48v76p692ol2zq9.png)

**********
If you were to change the tag parameter to `p`:
![p parameter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4p58r0oibfo7hw793rpm.png)
`op1.py` will then scrape the first `<p>` tag within the specified URL:
![p element](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/490hpaw60p0d1h0jx8kr.png)
**********

### You can now experiment!

You can now implement this method of performing user customizable backend operations into your projects and PHP scripts when trying to perform backend operations.

*************
**Stick around for part 3 to learn about more complex, memory and time intensive backend operations in PHP!**

*************

Byeeee👋