python101
Posts: 50
Joined: Sun Jun 22, 2014 11:20 am

HTML & PHP

Thu Jul 02, 2015 12:38 pm

So I have a robot and I want to control it from a website using PHP. I am currently testing writing to files but the following code won't work

Code: Select all

<!DOCTYPE html>
<html>
<body>
<?php
$myfile=fopen("test.txt", "w");
?>
<button onclick="echo <?php fwrite($myfile, 'Forward');fclose($myfile);?>">Forward</button>
<button onclick="echo <?php fwrite($myfile, 'Stop');fclose($myfile);?>">Stop</button>
</body>
</html>
I have already used chmod 777 so I know its writable. It is writing to the file but writing the wrong values. What is wrong with it?

Code: Select all

if human contains caffeine:
work.hard()
else:
state.sleep()

User avatar
rpdom
Posts: 17415
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: HTML & PHP

Thu Jul 02, 2015 12:45 pm

PHP is server-side only. The PHP code runs before the page is sent and is never seen by the browser.

Try "view page source" in your browser to see what is left of the page once it has loaded.

To react to events on the page you need to use something like Javascript and perhaps Ajax. Or for simple things you could just use a form and make the buttons submit the page. Then your PHP could check the POST or GET data and control the robot accordingly.

python101
Posts: 50
Joined: Sun Jun 22, 2014 11:20 am

Re: HTML & PHP

Thu Jul 02, 2015 12:49 pm

I'm only new to PHP and i've tried POST and GET before but I couldn't get it to work

Code: Select all

if human contains caffeine:
work.hard()
else:
state.sleep()

python101
Posts: 50
Joined: Sun Jun 22, 2014 11:20 am

Re: HTML & PHP

Thu Jul 02, 2015 12:53 pm

Can you write to a file using javascript?

Code: Select all

if human contains caffeine:
work.hard()
else:
state.sleep()

User avatar
DougieLawson
Posts: 39551
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: HTML & PHP

Thu Jul 02, 2015 1:30 pm

python101 wrote:Can you write to a file using javascript?
Where does the file live?

You can run server side Javascript to write to a file, but you can't run browser side Javascript to write to a remote file.

Look at writing a CGI program (in perl, php or python). It will need a <FORM>...</FORM> to process the data that comes in from the remote browser. If you get very advanced you could run that with XMLHTTPREQUEST to run from the button press event.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: HTML & PHP

Thu Jul 02, 2015 1:32 pm

PHP is server side and will be able to write to your server file
js (*) is client side and will not be able to write to your server

They work well together if you code them to work well together.
PHP has to manage the "server stuffs" (in your example, the file writing)
JS can help (but it's not mandatory) to get the client stuffs nicer or easier to use.

One thing is what the client view when it visits a php page (the HTML code is created by the php. the code you reported is NOT what a client sees)
Another thing is what the server has (what you posted is a php file)

You have to take some step backward :)

btw, "post and get" are the easier way to start with these sort of things.
Then you'll get to "but my page refreshes at any command" and we well able to talk about js again, but it will be in the future :)

(*) node js and blah blah bla.. i know, but it's not the point

Heater
Posts: 16296
Joined: Tue Jul 17, 2012 3:02 pm

Re: HTML & PHP

Thu Jul 02, 2015 2:56 pm

Ha! Yes, I suggest node.js and blah blah bla.. :)
Memory in C++ is a leaky abstraction .

jackccwong
Posts: 6
Joined: Thu Jun 25, 2015 10:03 am

Re: HTML & PHP

Fri Jul 03, 2015 3:37 am

@python101,

We suggest an approach to use webpage (JavaScript & HTML) to control hardware
http://zetakey.github.io/zkgpio.js/
It is for GPIO interface on raspberrypi.

If we have hardware control plugin to your robot, you can control the robot in webpage.
Jack Wong

Heater
Posts: 16296
Joined: Tue Jul 17, 2012 3:02 pm

Re: HTML & PHP

Fri Jul 03, 2015 4:28 am

Except zetakey does not give you a web page. It is rendered by a browser but it is not on the web. That is to say you can access your robot from any browser that happens to be connected over network.

That's OK if local control is all you want.
Memory in C++ is a leaky abstraction .

myoung008
Posts: 55
Joined: Mon Mar 18, 2013 9:56 pm

Re: HTML & PHP

Fri Jul 03, 2015 6:14 am

This is what you were wanting your original code to do.

Note that the code gets called multiple times. The first time the action is not set so the php part is ignored and it puts out the form. Clicking a button submits the form with action set. The PHP writes to the file and then sends the form out again for the next change.

Code: Select all

<?php
if (isset($_GET['action'])) {
  $myfile=fopen("test.txt", "w");
  switch (strtolower($_GET['action'])) {
    case 'forward':
      fwrite($myfile, 'Forward');
      break;    
    case 'stop':
      fwrite($myfile, 'Stop');
      break;
  }
  fclose($myfile);
} 
?><!DOCTYPE html>
<html>
<body>
<form method="GET">
<input type="submit" name="action" value="Forward">
<input type="submit" name="action" value="Stop">
</form>
</body>
</html>
Hope this helps.

ethanmagestore
Posts: 9
Joined: Thu Jun 04, 2015 7:13 am

Re: HTML & PHP

Tue Jul 14, 2015 7:21 am

I'm learning PHP code, which website has tutorials for beginners like me?

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: HTML & PHP

Tue Jul 14, 2015 7:54 am

ethanmagestore wrote:I'm learning PHP code, which website has tutorials for beginners like me?
www.php.net is the bible of php

BillStephenson
Posts: 256
Joined: Thu May 17, 2012 1:25 am
Location: SW Missouri Ozarks
Contact: Website

Re: HTML & PHP

Mon Jul 20, 2015 5:17 am

python101 wrote:Can you write to a file using javascript?
Yes, sort of. You can store the contents of a file in a web browser's built-in database. Check out PouchDB ( http://pouchdb.com ), it does this and some other very handy stuff, like sync the contents of that file to a remote server side CouchDB database (check out http://couchdb.apache.org & http://cloudant.com ). You can find install instruction for the latest version of CouchDB on your Pi here.

Return to “Other programming languages”