0

I'm new to programming and PHP. I'd like to use onclick from HTML to log in to PHP.

This is my code:

<script type="text/javascript">
    function Log(){
    <!-- This is Log in script -->
        <?php
            $Logged = 0
            $pass = $_GET["pass"];
            $uid = $_GET["uid"];
            $click = $_GET["con"]
            if($pass=123 && $uid=xxx| && $click=1){
                $Logged=1
                break
            } else {
                $Logged=0
            }
        ?>
    }
</script>

And there HTML button:

<input type="button" onclick="$Logged++" value="Log in"
    style="color: #000000; background-color: #7200E3;
      width: 80px; height: 40px; border-style: solid;
      border-color: #00E372; border-width: 0.1cm"
/>

Please help me :)

3
  • 2
    PHP is processed before your HTML is output. If you want to increment something onClick you'll need to use Javascript or make a request through AJAX to your PHP. Commented Aug 18, 2011 at 22:02
  • 2
    Not sure where that PHP has come from, but it is absolutely not valid Commented Aug 18, 2011 at 22:04
  • What if the user presses the button more than once? $Logged would be incremented every time the button is clicked, if the script was valid. Better strategy is to send the data to a PHP, do the login process, return the data (either via json or xml or any other means) and process it on the client-side. Try searching for "login flow in php ajax", anything like that, and you'll get a better idea. Commented Aug 18, 2011 at 22:12

1 Answer 1

4

You are throwing two different things in one jar here.

  • PHP runs on the Server and does not interact with your Browser.
  • JavaScript runs "in your Browser" and can interact with it.

Since this seams like you want to create a simple Log-In Script, why don't you use a normal Hyperlink to your PHP-site which offers a form to log in. There are hundreds of tutorials out there which show you the way.

Also, what you "created" is not save to use, since you're using PHP to write a JavaScript function. The JavaScript-code and your password (as you simply add it to the JS-function in clear text) can be read by simply displaying the sites source-code.

Also, using something like onclick="$Logged++" to increase a PHP-Variable does not work. Again, PHP runs on the server and not in your Browser.

If you seriously want to start with PHP (and create secure and save pages) you should conceder reading a whole book for the startup. A great collection can be found here.

Sign up to request clarification or add additional context in comments.

Comments

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.