0

I have a webpage provided by the server. I have about 20 different clients accessing this webpage. The webpage is a simple form with a textbox displaying to each client. Client is responsible for providing the input usually it would be through input device like a keyboard, microphone, barcode scanner etc. I have a different device which cannot directly input its value to the textbox but only through a variable value. Server is using WAMP and each client has LAMP installed on it.

I created a request for that input from the server using ajax call but that is not ideal and I do not want to put too much pressure onto the server. I just want the variable to be sent automatically to the webpage as it is being loaded by the client. I heard it can be done using cURL, but my question is what would be the best way to achieve this and how to do it? Would it be easier to send a variable from a shell to a web browser on the load? I’m looking for a secure but easy to implement method. This is what my attempt was: (although I got confused on how to approach this)

Server: (WAMP Server / server.php)

<?php

if(isset($POST)){
    $_POST['var'];
}

?>

<html>
<body>
<form action="" method="post">                                                                                              
    <input type="text" name='var' value="<?php echo "$var"; ?>"/>
</form>
</body>
</html>

Client: (LAMP Server / inputProv.php)

<?php

$var = "Hello";
echo $var;

?>

Client: (LAMP Server / send.php)

<script>
    $(document).ready(function(){
        $.get("http://localhost/inputProv.php", function(response) {
            $("input[name=var]").val(response):
        });
    });
</script>

1 Answers1

0

inputProv.php should just echo the variable.

<?php
$var = "Hello";
echo $var;

In your Javascript you use AJAX to fill in the input from this script.

$(document).ready(function() {
    $.get("http://localhost/inputProv.php", function(response) {
        $("input[name=var]").val(response);
    });
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • this is not bad but there will be 20 different IP addresses do I need to get the content of each one? what if I do not know the IP of the client only client knows what is the address of the server? –  May 10 '17 at 22:35
  • If only the client knows, then it needs to make an AJAX request, you can't do it on the server when creating the page. – Barmar May 10 '17 at 22:36
  • Can the client make an AJAX request directly to the LAMP server, so it doesn't have to load the WAMP server? – Barmar May 10 '17 at 22:37
  • @Barman the main server is WAMP and each client should provide input to that web page on that server. If LAMP is sending the data to WAMP at a later stage then yes. –  May 10 '17 at 22:53
  • The clients should use an AJAX request to the LAMP server to fill in the input, then it gets sent to the WAMP server when they submit the form. – Barmar May 10 '17 at 22:57
  • So the LAMP server need to host the form page? –  May 11 '17 at 05:59
  • No. LAMP hosts the AJAX scripts, WAMP hosts the form page. Clients use AJAX to fill in the field in the form, then it gets submitted to WAMP. – Barmar May 11 '17 at 06:10
  • 1
    I've changed the answer to show how to get it using AJAX. – Barmar May 11 '17 at 06:46
  • Thank you could you tell me how the WAMP server will receive it? –  May 11 '17 at 07:21
  • It will be in `$_POST['var']`. It's just an ordinary form. – Barmar May 11 '17 at 07:22
  • I'm not sure if I follow you correctly I updated the script in my question to your recommendation. When I load the form page on client machine the input is not there and it says there is undefined variable in the textbox. I still do not understand how client is sending data without specifying the server address, how does it know where to send it? –  May 11 '17 at 08:28
  • I put `lampserver.yourdomain.com` in as a placeholder. Replace that with the actual name of your LAMP server. – Barmar May 11 '17 at 08:31
  • I do not have a name on the LAMP server, each client has LAMP installed as a localhost and its IP address is the only thing they identify as. I can only put their IP address so it eventually be a localhost, as they the same. –  May 11 '17 at 10:47
  • See http://stackoverflow.com/questions/20194722/can-you-get-a-users-local-lan-ip-address-via-javascript for how Javascript can get the local IP address. – Barmar May 11 '17 at 10:51
  • In my question it is localhost, but it still does not fill the textbox when I load the server.php from client. –  May 11 '17 at 10:56