1

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.

2
  • The way I see it those on/offs are coming from your python script Commented Apr 22, 2019 at 1:46
  • @Niels so you say they are not actually requesting from php? r.text gets the basic table data from web page, and a,b,c,d,e are just python variable am I right? Commented Apr 22, 2019 at 2:03

1 Answer 1

1

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:

Store the information in a database

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 } ?> 

Use URL parameters

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 (i.e. https://localhost/index.php) without the parameters.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for the information, I didn't tried the code. However at least I understand I have to store data in somewhere. I never tried mysql or similar, maybe I can write to text file in python then I will read it in php.
@SelimTurkoglu If you want to store to a file you can try SQLite which is a file database. You can definitely write to a plain text file if you prefer though.
I wrote the output to text file in python, now I'm displaying it in php via $myfile = fopen("text.txt","r"); while(!feof($myfile)) { $word = fgets($myfile); I guess it is easier than a database for just now
@SelimTurkoglu Yep, if that works for your use case that's great. If you think my answer has completely covered your question's concerns, feel free accept it or you can wait if you think someone else can satisfy your concerns better.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.