I'm trying to create a php page which takes data from my python code and show them as a table.
r = requests.post('http://localhost/index.php', data={"iv":a,"ven":b,"hem":c,"card":d,"ecg":e})
print(r.text)
In above code, I post data then print it to check if everything is okay. My r.text output
<table><tr><th>IV Pump</th><th>Ventilator</th> <th>Heart Monitor</th><th>Cardiac Machine</th><th>Echocardiogram</th></tr><tr><td>off</td><td>on</td><td>off</td><td>off</td><td>off</td></tr></table>
which seems fine because I can get a,b,c,d,e (on/off basically). However when I open my index.php I cannot see those "on"s and "off"s. I am newbie on server things, PHP, etc. My mistake is probably very dummy. Should I store post data in somewhere? My index.php:
<?php
$iv=$_REQUEST['iv'];
$ven=$_REQUEST['ven'];
$hem=$_REQUEST['hem'];
$card=$_REQUEST['card'];
$ecg=$_REQUEST['ecg'];
echo '<table>';
echo '<tr>';
echo '<th>IV Pump</th>';
echo '<th>Ventilator</th> ';
echo '<th>Heart Monitor</th>';
echo '<th>Cardiac Machine</th>';
echo '<th>Echocardiogram</th>';
echo '</tr>';
echo '<tr>';
echo '<td>';
echo $iv;
echo '</td>';
echo '<td>';
echo $ven;
echo '</td>';
echo '<td>';
echo $hem;
echo '</td>';
echo '<td>';
echo $card;
echo '</td>';
echo '<td>';
echo $ecg;
echo '</td>';
echo '</tr>';
echo '</table>';
?>
My r.text is ok, but in web page I cannot see request data, the table cells are empty. What is the difference? As I know r.text returns the page content, so index.php must be wrong, I guess it is about storing the data.
When you make a HTTP request to a server, that server will return the rendered webpage to that user. This means requests
will get back the correct response.
However when you open a web page in a browser, you are making a new request to that server which will render the page again. Since you aren't passing the $_REQUEST['iv']
etc. values this time, the table will appear blank.
You have a few options depending on what you want to do:
You can store that information in a database. Some databases for example are SQLite3 or MySQL . I've omitted the exact database insertion/reading implementation since it differs between which database you pick.
A simple method might be:
<?php
$iv=$_REQUEST['iv'];
// insert other variables etc.
// Check if this is a POST request
if ($_SERVER['REQUEST_METHOD'] === "POST") {
// INSERT DATA INTO YOUR DATABASE AS APPROPRIATE
// You can also echo back the table if you want
// Else it might be a GET request
} elseif ($_SERVER['REQUEST_METHOD'] === "GET") {
// READ DATA FROM DATABASE
// echo table with the data from the database
}
?>
Alternatively you can use URL parameters to encode your data like so:
# In your Python code
# Notice that requests.post changed to requests.get and data changed to params
r = requests.get('http://localhost/index.php', params={"iv":a,"ven":b,"hem":c,"card":d,"ecg":e})
# You can now access this specific URL to see the populated table
# The URL will look something like http://localhost/index.php?iv=a&ven=b/
print(r.url)
Note that this requires you to visit that specific URL and doesn't work if you visit the base URL (ie https://localhost/index.php
) without the parameters.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.