在表单提交上显示/隐藏元素

在表单提交上显示/隐藏元素

问题描述:

hello I am a php beginner and I have the following problem with my code, I need to add 2 numbers and echo the result as a 3rd number (sum) The sum number must not be shown before clicking the submit button, only the input boxes for the 2 numbers, and when the sum is displayed the 2 input boxes must be hidden and only the sum shown

<html>  
<body>  
    <form method="post" id="form1">  
        First Number:  
        <input type="text" name="number1" /><br>  
        Second Number:  
        <input type="text" name="number2" /><br>  
        <input  type="submit" name="submit" value="Add" >  
    </form>  


    <?php 
    $number1 = $_POST['number1'];  
    $number2 = $_POST['number2'];  
    $sum =  $number1+$number2;     
    echo "Sum = ".$sum;
    ?>

    </script>  
</body>  
</html>

You need to check with isset function to understand if the form is submitted or not. Like following,

<body>  
   <?php if( !isset($_POST['submit'])){ ?> 
   <form method="post" id="form1">  
        First Number:  
        <input type="text" name="number1" /><br>  
        Second Number:  
        <input type="text" name="number2" /><br>  
        <input  type="submit" name="submit" value="Add" >  
    </form>  
    <?php } ?>

    <?php 
    if(isset($_POST['submit'])){
       $number1 = $_POST['number1'];  
       $number2 = $_POST['number2'];  
       $sum =  $number1+$number2;     
       echo "Sum = ".$sum;
    }
    ?>

    </script>  
</body>  
</html>