xwishmasterx Posted October 23, 2013 Share Posted October 23, 2013 Hello I hope someone here can help me out as this is starting to drive me crazy Below is a piece of code that I simply cannot figure out: $ip = $_POST["ip"]; $port = $_POST["port"]; $note = $_POST["note"]; $servers = array( 'Google Web Search' => array( 'ip' => $ip, 'port' => 80, The problem is the $ip variable. With the above setup the code won't work, although $ip value is correct. Adding the value directly in the script, instead of $_POST["ip"], it works perfectly. Anyone can answer me why, and maye point me in the right direction? Quote Link to comment Share on other sites More sharing options...
BrodaNoel Posted October 23, 2013 Share Posted October 23, 2013 But... What is the problem? What don't work? How is the fail result, and how is the expected result? Quote Link to comment Share on other sites More sharing options...
DaveyK Posted October 23, 2013 Share Posted October 23, 2013 Most likely your issue is either a faulty name in your input tag or the wrong method in your form Quote Link to comment Share on other sites More sharing options...
The Letter E Posted October 23, 2013 Share Posted October 23, 2013 please share the output of: var_dump($_POST); Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 23, 2013 Share Posted October 23, 2013 If I understand your problem as it is poorly worded. You are stating that if you use the variable $ip in defining the 'ip' element in the array it is not getting set as you expect. but, if you use $_POST["ip"] (which is what was used to define $ip) it is getting set as you expect. There is no reason that should not work the same. So, I have to assume there is more to the code than you are showing. Quote Link to comment Share on other sites More sharing options...
xwishmasterx Posted October 23, 2013 Author Share Posted October 23, 2013 You're absolutely right. Here's the entire code that won't work with the form inputs, but will work if variables are added into the code. As mentioned the form does work. <?php /* * * Use the examples below to add your own servers * */ $title = "EZ Pinger"; // website's title $kunde = $_POST["kunde"]; $ip = $_POST["ip"]; $port = $_POST["port"]; $note = $_POST["note"]; $servers = array( 'Example Down Host' => array( 'ip' => $ip, 'port' => $port, 'note' => $note ) ); if (isset($_GET['host'])) { $host = $_GET['host']; if (isset($servers[$host])) { header('Content-Type: application/json'); $return = array( 'status' => test($servers[$host]) ); echo json_encode($return); exit; } else { header("HTTP/1.1 404 Not Found"); } } $names = array(); foreach ($servers as $name => $info) { $names[$name] = md5($name); } ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title><?php echo $title; ?></title> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootswatch/2.3.2/cosmo/bootstrap.min.css"> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css"> <style type="text/css"> /* Custom Styles */ </style> </head> <body> <div class="container"> <h1><?php echo $title; ?></h1> <form method="post" action="<?php echo $PHP_SELF;?>"> Kundenummer:<input type="text" size="20" maxlength="20" name="kunde"><br /> IP:<input type="text" size="20" maxlength="20" name="ip"><br /> Port:<input type="text" size="20" maxlength="20" name="port"><br /> Note:<input type="text" size="20" maxlength="20" name="note"><br /> <input type="submit" value="submit" name="submit"> </form> <table class="table"> <thead> <tr> <th></th> <th>Kunde</th> <th>IP</th> <th>Port</th> <th>Note</th> </tr> </thead> <tbody> <?php foreach ($servers as $name => $server): ?> <tr id="<?php echo md5($name); ?>"> <td><i class="icon-spinner icon-spin icon-large"></i></td> <td class="name"><?php echo $kunde; ?></td> <td><?php echo $ip; ?></td> <td><?php echo $port; ?></td> <td><?php echo $note; ?></td> </tr> <?php endforeach; ?> </tbody> </table> </div> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <script type="text/javascript"> function test(host, hash) { // Fork it var request; // fire off the request to /form.php request = $.ajax({ url: "<?php echo basename(__FILE__); ?>", type: "get", data: { host: host }, beforeSend: function () { $('#' + hash).children().children().css({'visibility': 'visible'}); } }); // callback handler that will be called on success request.done(function (response, textStatus, jqXHR) { var status = response.status; var statusClass; if (status) { statusClass = 'success'; } else { statusClass = 'error'; } $('#' + hash).removeClass('success error').addClass(statusClass); }); // callback handler that will be called on failure request.fail(function (jqXHR, textStatus, errorThrown) { // log the error to the console console.error( "The following error occured: " + textStatus, errorThrown ); }); request.always(function () { $('#' + hash).children().children().css({'visibility': 'hidden'}); }) } $(document).ready(function () { var servers = <?php echo json_encode($names); ?>; var server, hash; for (var key in servers) { server = key; hash = servers[key]; test(server, hash); (function loop(server, hash) { setTimeout(function () { test(server, hash); loop(server, hash); }, 6000); })(server, hash); } }); </script> </body> </html> <?php /* Misc at the bottom */ function test($server) { $socket = @fsockopen($server['ip'], $server['port'], $errorNo, $errorStr, 3); if ($errorNo == 0) { return true; } else { return false; } } function in_array_r($needle, $haystack, $strict = false) { foreach ($haystack as $item) { if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { return true; } } return false; } ?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 23, 2013 Share Posted October 23, 2013 (edited) Your code should work when you first submit the form. The $servers array should be populated with the values from the form. However when you are doing the ajax request to check the status of the servers defined in the $servers array, it will be empty! The values from POST wont be remembered during the ajax request. Edited October 23, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
xwishmasterx Posted October 23, 2013 Author Share Posted October 23, 2013 (edited) Your code should work when you first submit the form. The $servers array should be populated with the values from the form. However when you are doing the ajax request to check the status of the servers defined in the $servers array, it will be empty! The values from POST wont be remembered during the ajax request. Is there a way to overcome this issue? What puzzels me if you're right, that if I run the code without the form it works perfectly (bar turning red when a ping times out). However if you're saying the ajax doesn't remember the values then why am I getting a positive result (ping is replied) if the values aren't there? Edited October 23, 2013 by xwishmasterx Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 23, 2013 Share Posted October 23, 2013 (edited) When the form is submitted you'll need to save the contents of the $servers array to some form of storage, such as a text file, or database.When your PHP script gets the ajax request you'll need to get the server data from the storage to repopulate the $servers array and then check the status of the servers. An easy way to write the contents of an array to a text file is to serialize it and then use file_put_contents to write the data to a file. To read the file you'll use file_get_contents and then unserialize it to repopulate the $servers array. Note make sure you only write the server data to the storage when a POST request has been made, otherwise the data will be overwritten on any request. Edited October 23, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
xwishmasterx Posted October 23, 2013 Author Share Posted October 23, 2013 thanks alot for the links..looks pretty difficult though I'll give it a try, but there's a decent chance I'm gonna end up at the freelancer forum Thanks for your replies Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted October 24, 2013 Solution Share Posted October 24, 2013 Just store the results in a session variable. When the code is run, check if the POST data was sent. If so, save it to the session variable. Then check if the GET request was sent. If so, check the session data for the matching value. Here is a "possible" solution - not tested so there may be some changes needed. <?php /* * * Use the examples below to add your own servers * */ session_start(); if(isset($_POST['kunde'])) { $kunde = trim($_POST["kunde"]); $ip = trim($_POST["ip"]); $port = trim($_POST["port"]); $note = trim($_POST["note"]); //Should add some validation of the data here //Add entry to session array $_SESSION['servers'][$kunde] = array( 'ip' => $ip, 'port' => $port, 'note' => $note ); } if (isset($_GET['host'])) { $host = $_GET['host']; if (isset($_SESSION['servers'][$host])) { header('Content-Type: application/json'); $return = array( 'status' => test($_SESSION['servers'][$host]) ); echo json_encode($return); exit; } else { header("HTTP/1.1 404 Not Found"); } } $names = array(); foreach ($_SESSION['servers'] as $name => $info) { $names[$name] = md5($name); } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.