Er. SA Sujon
Er. SA Sujon

Reputation: 11

Click event on two buttons for if else condition is not working

Advance thanks for your help. I am not understanding why it is not working. I am a beginner and hence I am not finding the problem. Please anyone help me...

echo "<form method='POST'><br>
    <p>Would you Like to create your Account with Details Information<b>?
</b>
                <br><br> 
                <input type='button' id='yes' name='yes' value='Yes'/> 
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
                <input type='button' id='no' name='no' value='No'/></p>
</form>";
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        if (isset($_POST['yes'])) {
          echo "Yes Clicked";
         } else if (isset($_POST['no'])) {
         echo "<br><br><p> You have successfully Registered!!!</p>";
             }
        }

Upvotes: 1

Views: 408

Answers (3)

Yassine CHABLI
Yassine CHABLI

Reputation: 3734

try this code :

<?php

echo "<form method='POST' action='index.php'><br>
    <p>Would you Like to create your Account with Details Information<b>?
</b>
                <br><br> 
                <input type='submit' id='yes' name='yes' value='yes'/></p>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
                <input type='submit' id='no' name='no' value='No'/></p>
</form>";

        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        if (isset($_POST['yes'])) {
         echo "<br><br><p> Yes clicked!!!</p>";
         } else if (isset($_POST['no'])) {
         echo "<br><br><p> You have successfully Registered!!!</p>";
             }
        }

?>

form are trigged with input type submit , so if you want to get value of two input , they must be a submit input instead of button . in this cas , you have two input type submit , theey should be type submit with different name .that's all

Upvotes: 1

Sarah Gro&#223;
Sarah Gro&#223;

Reputation: 10879

You have to change your button's type attributes to submit instead of button, or else they won't actually submit the form.

<input type='submit' id='yes' name='yes' value='Yes'/> 

Upvotes: 0

newman
newman

Reputation: 424

Replace button with submit

<input type='submit' id='yes' name='yes' value='Yes'/>  
<input type='submit' id='no' name='no' value='No'/></p>

Upvotes: 1

Related Questions