0

I have my HTML web page, and a mobile application.

In my mobile application I have three buttons, start, pause and stop, I have these same three buttons on my website (On my website these three buttons already have actions).

What I want to do is if I click the button on my mobile phone, it clicks the same designated button on my web page.

Mi initial plan was, from my mobile application I send via post to a php file inside my website files. Then from that php file send a simple variable to a JavaScript file, where that JavaScript file would read what variable it is, and depending on that variable it would perform the click on my corresponding button in my webpage.

The script I'm using on my website is inside my HTML code.

Website:

<!DOCTYPE html> 
<html>
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="css/style.css">
        <script type="text/javascript" src="js/test"></script>
        <title> Spelling Bee </title>
    </head>
    <body>
        <header>
            <h1> Spelling Bee </h1>
        </header>

        <section>
            <div id="textbox">
                <h2> "Word" </h2>
            </div>

            <div>
                <p> Choose the difficulty: </p>
                <select id="difficulty">
                    <option value="0"></option>
                    <option value = "1"> Easy </option>
                    <option value = "2"> Medium </option>
                    <option value = "3"> Hard </option>   
                </select>

                <div id="timer"></div>

                <button id="start"> Start! </button>
                <button id="next"> Next </button>
                <button id="stop"> Stop </button>

                <script>

                    //1000 = 1 sec
                    var start= document.getElementById("start");
                    var difficulty = document.getElementById("difficulty")
                    var next = document.getElementById("next");
                    var stop = document.getElementById("stop");
                    var timer;

                    document.getElementById("timer").innerHTML = "-";
                    document.getElementById("start").disabled = true;
                    document.getElementById("next").disabled = true;
                    document.getElementById("stop").disabled = true;

                    start.addEventListener("click", functionStart);
                    var seconds, stop, counterStarted = false, counter;
                    var select_difficulty= document.getElementById('difficulty');

                    select_difficulty.onchange = function select_stop_time() {
                        var selectedValue = select_difficulty.options[select_difficulty.selectedIndex].value;

                        if (selectedValue == 1 ) {
                        document.getElementById("start").disabled = false;
                        stop = 10;//10
                        }
                        else if (selectedValue == 2 ) {
                        document.getElementById("start").disabled = false;
                        stop = 15;//15
                        }
                        else if (selectedValue == 3 ){
                        document.getElementById("start").disabled = false;
                        stop = 20;//20
                        }
                        else {
                        document.getElementById("start").disabled = true;
                        document.getElementById("next").disabled = true;
                        document.getElementById("stop").disabled = true;
                        stop = 0;//0
                        }
                    }

                    function functionStart() {
                        seconds = 1;
                        document.getElementById('difficulty').disabled = true;
                        document.getElementById("start").disabled = true;
                        document.getElementById("next").disabled = false;
                        document.getElementById("stop").disabled = false;

                        if(counterStarted === false){
                            counterStarted = true;
                            counter = setInterval(function(){
                                if(seconds <= stop){
                                    document.getElementById("timer").innerHTML = seconds;
                                    seconds++;
                                }
                                else{
                                    seconds = 1;
                                }
                            },1000)
                        }
                    }

                    stop.addEventListener("click", functionStop);
                    function functionStop(){
                        document.getElementById("timer").setAttribute("disabled", "disabled");
                        clearInterval(counter);
                        counterStarted = false;
                        document.getElementById('difficulty').disabled = false;
                        document.getElementById("timer").innerHTML = "-";
                        document.getElementById("start").disabled = false;
                        document.getElementById("next").disabled = true;
                        document.getElementById("stop").disabled = true;
                        frame();
                    }

                    next.addEventListener("click", functionNext);
                    function functionNext(){
                        seconds = stop;
                    }
                </script>
            </div>
        </section>
        <footer>
        </footer>
    </body>
</html>

Mobile Application: -HTML

<!DOCTYPE html> 
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/>
        <title> Spelling Bee </title>
        <!-- grupo CSS -->
        <link rel="stylesheet" href="css/jquery.mobile.icons-1.4.5.min.css"/>
        <link rel="stylesheet" href="css/jquery.mobile.structure-1.4.5.min.css"/>
        <link rel="stylesheet" href="css/theme_sb.min.css"/>
        <!--grupo JavaScript-->
        <script src="js/jquery-1.11.2.min.js"></script>
        <script src="js/jquery.mobile-1.4.5.min.js"></script>
        <script src="js/acciones.js"></script>
        <script src="phonegap.js" type="text/javascript"></script>
    </head>
    <body>
        <div data-role="page" id="principal" style="background-image: url(imagenes/honeycomb.png); text-align:center;">

            <div data-role="header" data-position="fixed" data-tap-toggle="false">
                <h1> Spelling Bee </h1>
            </div>

            <div data-role="main" class="ui-content" style="width: 30%; margin-left: 32%; margin-top: 45%;">
                <div id="buttons">
                    <input type="button" class="ui-btn" id="button1" value="Start!">
                    <input type="button" class="ui-btn" id="button2" value="Next">
                    <input type="button" class="ui-btn" id="button3" value="Stop!">
                </div>
            </div>

            <div data-role="footer" align="center" data-theme="a" data-position="fixed">
                <h4> Spelling Bee </h4>
                <img src="imagenes/bee.png" width="60">
            </div>

        </div>
    </body>
</html>

-JavaScript

// JavaScript Document
$(document).ready(function(e) {
    $('#button1').on('click',function(){
        alert("1");
    });//1
    $('#button2').on('click',function(){
        alert("2");
    });//2
    $('#button3').on('click',function(){
        alert("3");
    });//3
}); //ready

I put alerts to test it.

Andres
  • 11
  • 1

1 Answers1

-1

Assuming you want the action on your website to be the same as on your mobile app, you could just send the aforementioned POST request and then internally trigger the Javascript event, instead of having to emulate an actual mouse click on the website.

If you want to trigger some visual effect on your elements on the website, you can also add styles/classes to elements using Javascript.

To answer your original question of programmatically clicking on an HTML element, you can just do:

document.getElementById('elementID').click();

which is supported by nearly all browsers. (taken from this thread)

The only variable you would need to send via PHP is the 'elementID', which, in your JS files, you would use as the ID of the element you're trying to click.

Okie
  • 67
  • 1
  • 11
  • Thank you, would you mind further helping how can I do the first thing you mentioned, without emulating the mouse click? – Andres Nov 04 '19 at 01:33
  • You would need to have a backend server running with PHP, and then, from your mobile app, send the request to your server using any method you like, and then include a script tag in your HTML using PHP that contains the code for activating your JS function. You can read more about triggering JS with PHP from here https://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php – Okie Nov 04 '19 at 01:43
  • How can I create a php server? – Andres Nov 04 '19 at 01:52
  • There are many sources to learn about making a backend with PHP, and starting a PHP server on your local machine is made easy in PHP 5.4 and later with the built-in local server: `php -S localhost:8000`, or you could install and run XAMPP (http://www.apachefriends.org/en/xampp.html) – Okie Nov 04 '19 at 01:59
  • I have WAMP server installed, is it similar? – Andres Nov 04 '19 at 02:02
  • XAMPP is a Cross-platform Apache server using MariaDB as a database and PHP and Perl as backend languages, and WAMP is an equivalent of the LAMP stack, but for Windows. WAMP uses MySQL as a database and PHP as a backend language, so you shouldn't have any problem. – Okie Nov 04 '19 at 02:06
  • Thank you very much for your help, would you mind if you can help or provide me with a link toi use wamp as my backend php sever? – Andres Nov 04 '19 at 02:14
  • I haven't had any extensive experience with WAMP, but I recommend just using Google to find sources on setting up WAMP, as it has an active community and extensive documentation, not to mention providing more support for Windows – Okie Nov 04 '19 at 02:20
  • No problem, just remember to mark the question as solved – Okie Nov 04 '19 at 02:28