0

Right now I am programming a Raspberry Pi robot with wheels for a personal project. I want to control it through an Apache website which is running on the Raspberry. I only want to connect to it locally.
Last night i have done the buttons to control the wheels from the robot. I tried to execute the files which are needed to start the motors through PHP but soon learned that the site always switches, which i dont want it to do, when i just use a submit button in a <form></form>. So i went on the internet and found something about ajax.
Since all buttons are not working, i will just give you an example of one button from my code. They all work the same, they just have different names, directorys and call a different function. All functions are in the same Javascript.

HTML:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="../javascript/pfeile/pfeile.js"></script>

    <div class="pfeilvorne" id="submitvorne" style="background-color: transparent;" onClick="vorne()"></div>

Javascript:

function vorne() {
      $.ajax({
        url:"../../php/Pfeile/vorne.php", //the page containing php script
        type: "POST", //request type
        url:"../../php/Pfeile/php.php", //the page containing php script
     });
 }

PHP:

<?php
exec("sudo killall vorne.bin");
exec("sudo killall hinten.bin");
exec("sudo killall links.bin");
exec("sudo killall rechts.bin");
exec("sudo killall kreis.bin");
exec("sudo /home/pi/Maxwheel/Scripte/Vorne/sketch_apr13a/vorne.bin");
?>

I tested the scripts using sudo ./[Scriptname] in an ssh console and they are working. The buttons also work when i solely use php (But only using php still changes the site....)

I hope you can help me, thank you!

3
  • 2
    You can't have two of the same key in a javascript object (url). Commented Apr 15, 2018 at 16:48
  • Just to be clear on terminology and methodology, JavaScript does not - cannot - execute PHP; it can merely invoke an HTTP request, e.g. via AJAX, to a file which the server executes via PHP. Commented Apr 15, 2018 at 16:50
  • I know that this might not be what you're after, but I recommend using node.js for doing projects such as these. Commented Apr 16, 2018 at 0:27

1 Answer 1

1

You have two identical keys (url) in one object. They will be overwritten. Try something like this

$(document).ready(function() {
  $('#submitvorne').click(function() {
      $.ajax({
        url:"../../php/Pfeile/vorne.php", //the page containing php script
        type: "POST", //request type
     });

     $.ajax({
        url:"../../php/Pfeile/php.php", //the page containing php script
        type: "POST", //request type
     });
  })
})
<div class="pfeilvorne" id="submitvorne">
Sign up to request clarification or add additional context in comments.

1 Comment

Solved the issue! Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.