How to grab a form input in HTML using bash? - php

I am making a little project that grabs the HTML input from a local hosted website and then parses it through bash to open certain amount tabs using Bash.
Pseudo code:
<Input>50<Input> --> Bash grab HTML<Input>50<Input> --> loop 50: chrome.exe google.com
Code of my HTML input field:
<label class="tabAmount">Tab Amount: <input class="tabCount" type="number" name="tabSize" size="4" min="1" max="9999"></label>
So, my main problem is that I need a Bash command to search for the HTML input and then parse it through the script to open the tabs.
Thank you for any help!

This is my html code:
<form action="/cgi-bin/grab_html_input.sh">
Enter Number:<br>
<input type="number" name="number"><br>
<input type="submit" value="Submit">
</form>
This is my bash script:
saveIFS=$IFS
IFS='=&'
parm=($QUERY_STRING)
IFS=$saveIFS
number=${parm[1]}
while (( number > 0 ))
do
<command line command goes here>
done
I don't know the command to open new tab but this is the foundation that I've used to pass HTML input into a bash script.

Related

How to take real time input output from a python script on PHP

I want to take input from user through a html form, process it through a python script and print the output on html page. The problem is the whole python script is executed each time, while I want the script to give real time output for each input. How can I manage to do this ?
Here is what I am doing so far.
<?php
$vout='';
if(isset($_POST['submit']))
{
$vin=$_POST['input_text'];
$vout=exec('python bot.py '.$vin);
}
?>
<form method="post">
<label>
BotIn: <input type="text" name="input_text">
</label>
<input type="submit" name="submit">
</form>
</br></br>
<label>
<p>Bot: <?=$vout?></p>
</label>
The best way to solve your problem will be running simple python app using, let's say, Flask and hit to specific endpoint you'll make in it. Everything then could take place through localhost, if you'll set it on the same machine.

How can I get a string (or other data) from php and pass it to a python script?

I'm trying to do this (seemingly) simple little project on my raspberry pi. I have a 16x2 LCD display, and I want to be able to input some text from a webpage and have it show up on the LCD.
I have this simple html webpage where I'm trying to get the input from a user:
<!DOCTYPE html>
<head>
<title>Text Input</title>
</head>
<body>
<form action="index.php">
Line One:<br>
<input type="text" name="line1"><br>
Line Two:<br>
<input type="text" name="line2"><br>
<input type="submit" value="Submit">
</form>
<?php
// runs script and inputs 2 lines
?>
</body>
And the python is fairly simple, where line1 and line2 would be replaced by whatever is input.
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import os
line1 = "Line One"
line2 = "Line Two"
os.chdir('/home/pi/lcd')
import lcd
lcd.lcd_init()
lcd.lcd_byte(lcd.LCD_LINE_1, lcd.LCD_CMD)
lcd.lcd_string(line1,2)
lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
lcd.lcd_string(line2,2)
lcd.GPIO.cleanup()
I just don't know how to get the information from the webpage to the python script. I know that from php, you can use exec() to do commands, but even then, I'm not sure how you would get the data from the command line into the python script.
I would suggest you look into how post and get values are retrieved in Python. I am not sure why you would even use PHP with this since the HTML form could post to your Python script and have it handle parsing the get/post parameters.
You can see how the get/post values are retrieved at this SO question: How are POST and GET variables handled in Python?
Whether you are using get or post is determined by the method in your form tag.
<form method="post" action="url/to/python/script.py">
Line One:<br/>
<input type="text" name="line1"><br>
Line Two:<br/>
<input type="text" name="line2"><br>
<input type="submit" value="Submit">

Running bash script using PHP

I am working on little project. Task is get ip or address from input field and then run ping command for that ip and finally print result in the webpage.
I have written code:
<FORM ACTION="pingIp.php">
Enter Host: <INPUT name="host">
<INPUT TYPE=SUBMIT VALUE="Submit">
</FORM>
<div class="results"></div>
I don't know how to run ping command and retrieve results on webpage. Can you please give little php code as an example?

PHP POST not working correctly

When I submit my form, I get sent to the correct file as specified in the action attribute of my form, but the PHP in the file isn't printing the variables at all... I've combed through posts of other people having the same issue, but none of their solutions fix my problem. I've stripped my code down to a simple, textbox, button, and a php file that's supposed to print the textbox value.
If it matters, I'm running this locally in chrome, not using any servers or websites yet, I'd like to get my code working locally before I upload to my server.
HTML
<html>
<form action="Submit.php" method="post">
<input type="text" placeholder="First Name" name="firstName" id="firstName" required>
</br></br>
<input type="submit" name="submitted" value="Submit">
</form>
</html>
PHP
<html>
<body>
Name <?php echo $_POST["firstName"]; ?><br>
</body>
</html>
All I get when I click the button is a white page with "Name" printed.
Thanks!
Running scripts in response to HTTP requests is something that's done by the webserver. If you just use local files, the script will simply be loaded into the browser as a text or HTML file, it won't be executed. You can't do form processing like this.
You need to run a local server, then access the form as http://localhost/form.html

Running a PHP in the background while displaying another PHP

Currently I have 2 PHP files. 1 is the user interface and another fetches data from the backend and inserts into the database.
Currently if I use the following in the UI php:
<html>
<body>
<form action = "test.php" name="form" method="post">
<input type="text" name="text_box" size="50"/>
<input type="submit" id="search-submit" value="submit"/>
</form>
</body>
</html>
Upon clicking submit it goes to the test.php. If it possible to execute test.php in the background while remaining on the UI php?
Some of the previous posts talk about using ajax etc which I am not sure how to implement. Possible to do this in php?
In test.php you can use the exec function and call whatever php file you want using php by command line:
exec("php test.php > /dev/null 2>/dev/null &");
Just notice that the session that it will have is not the same as what you have on the browser, and it can be tricky to send parameters to the command line instance that is being initiated, take a look here.

Categories