user3211972
user3211972

Reputation: 11

Getting button input

Why does this not work. I have look at many sites and this works I look long time. Fine no no.

Edited code to this it echo pressed when I havent even pressed the button

<form method='POST' name="form1" action="index.php">

        Username: <input name="username"><br>
        Password: <input name="password"><br>


        <input type = 'submit' name = 'submit' value = 'click me'>



    </form> 

        <?php

        if (isset($_POST['submit'])){

            echo "Pressed button";



}



        ?>

Upvotes: 0

Views: 56

Answers (4)

Balaji Kandasamy
Balaji Kandasamy

Reputation: 4506

Your code is running even without closing form tag. But below is the formal way to use form.

<form method='post' action="">
    Username: <input name="username"><br>
    Password: <input name="password"><br>
    <input type = 'submit' name = 'submit' value = 'click me'>
</form>

<?php
if (isset($_POST['submit'])) {

    echo "Pressed button";
}
?>

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 59997

You need summat like

<form method="post" action="myscript.php">

etc

Upvotes: 0

M I
M I

Reputation: 3682

you need to add form tag and in action tag of form give your php page name. Here is index.php but you need to specify your own script name.

     <form name="form1" method="post" action="index.php">
    Username: <input name="username"><br>
    Password: <input name="password"><br>


    <input type = 'submit' name = 'submit' value = 'click me'>
    </form>


    <?php

    if (isset($_POST['submit'])){

        echo "Pressed button";


    }

Upvotes: 1

R00We
R00We

Reputation: 1981

You missed the tag <form></form>. Therefore it is not working

Upvotes: 2

Related Questions