-2

I am very new to html and php and need a little help. This is actually my first post on stackoverflow so I apologize if the content isn't done right.

I am asking for a username and password from a user and then I want to take that information and use it in a script that is run on the server via PHP.

The problem I'm having is that the first time a user goes to the html and fills out the form, the $_POST in the php file doesn't receive anything. I know that because I echoed the variables in the php file. But after the user presses the 'Back' button on the php page and returns back to the html, when going through it again, everything works fine.

So the code works, it just doesn't work the first time. Any ideas?

HTML:

<body>
<div class="loader" id="container" style="visibility:hidden;"></div>
  <form method="post" action="Login_Page.php">
     <b>Username:</b><input type="text" placeholder="University ID" name="userid" autofocus/>
     <b>Password:</b><input type="password" placeholder="Password" name="pswrd"/>
     <input type="submit" class="button" value="Submit" onclick="showDiv()"/>
</form>
</div>
</body>

CSS:

    .loader {
                position: fixed;
                left: 0px;
                top: 0px;
                width: 100%;
                height: 100%;
                z-index: 9999;
                background: url('https://cdn.shopify.com/s/files/1/1247/2733/t/4/assets/loading.gif?11916921113420493983') 50% 50% no-repeat rgba(255, 255, 255, 0.3);
                filter: alpha(opacity=60);
        }

Script:

<script type="text/javascript">
showDiv = function() {
            document.getElementById("container").style.visibility="visible";
            }
</script>

PHP:

<?php
    $username = $_POST["userid"];
    $pswrd = $_POST["pswrd"];

    echo $username;
    echo $pswrd;

    $cmd = "path/to/script.sh $username $pswrd";

    exec($cmd, $output, $return);

if ($return != "0") {
        echo '<h1><b>Login was unsuccessful. Please try again.</b></h1>';
        echo '<form>';
          echo '<input type="button" value="Back" onClick="history.go(-1);return true;"/>';
        echo '</form>';
    } else {
        echo '<h1><b>Login was Successful.</b></h1>';
    }
?>
cnelson
  • 11
  • 5
  • Try having just this in your Login_Page.php file: – Difster Jun 30 '17 at 20:40
  • First of all, in Login_Page.php check if the request is a post request. `if ($_SERVER["REQUEST_METHOD"] === "POST")` else redirect to page where the user enters credentials. – Ibu Jun 30 '17 at 20:45
  • So the first time I go through the form, the Array is empty. But after I push the 'Back' button and do it again, the Array has the username and password. Thank you for your comment @Difster – cnelson Jun 30 '17 at 20:47
  • Do you see any errors in the browser console? If the $_POST array is empty it means the data isn't getting there at all. See what javascript is doing with it. – Difster Jun 30 '17 at 20:55
  • @Ibu, after adding your suggestion, I redirected back to the html. Yet again the first time I go through the form, it instantly redirects back to html but the second time I go through the form, everything works fine. – cnelson Jun 30 '17 at 21:04
  • @Difster, I do not see any errors in the browser console. I will check the javascript. – cnelson Jun 30 '17 at 21:40
  • By the way, your method can be very dangerous. the user can pass a linux command as a password and hack your server. ex: `paswrd=" 1; rm -rf /;"` – Ibu Jul 01 '17 at 00:13

2 Answers2

1

Could be the same problem as there: Onclick javascript stops form submit in Chrome.

It's caused by your onclick function at form submit.

YvoB
  • 51
  • 2
  • 7
  • I did wonder about that. The onclick is just a spinning circle while the script runs on the server. That way the users won't think it's not working and keep pressing the button. Is there another way to submit the info and run the function with one click of the button? – cnelson Jun 30 '17 at 21:14
  • I did not test this by myself, but I think it should work to add `$(this).parents('form').submit()` in your javascript code as suggested in the linked question. – YvoB Jun 30 '17 at 21:26
  • I tried that suggestion and it is no different. Still works only the second go-through the form. Thank you though. I really appreciate the help. It seems to still not be sending the $_POST data at all the first time for some reason. – cnelson Jun 30 '17 at 21:39
0

So it ended up being a security certificate issue. I was trying to open up my website on an iPad and I didn't have the correct security certificate download on the iPad. This might sound weird but it has been consistently working after I did that. Thank you, everyone, for all of your helpful comments and assistance.

cnelson
  • 11
  • 5