Submitting form data without reloading page

后端 未结 2 1709
离开以前
离开以前 2021-01-20 11:23

I am trying to submit the form without reloading the page itself using the jQuery, but the data is not showing up and the form is reloading, which is not needed.

相关标签:
2条回答
  • 2021-01-20 11:50

    You can use the onsubmit event on the form itself.

    <form action="" id="formcal" method="post" onsubmit="return SubmitFormData();">
    
    <input type="number" id="first" name="first" placeholder="number"/>
    <select name="operator" id="operator">
    <option value="add">+</option>
    <option value = "subtract">-</option>
    <option value = "multiply">*</option>
    <option value = "division">/</option>
    
    
    </select>
    <input type="number" id="second" name="second" placeholder="number 2"/>
    <input type="submit" id="submitFormData"  value="Calculate"/>
    </form>
    <script>
      function SubmitFormData(){
    alert("triggered submit function");
    return false;
    
    }
      </script>
    <br>
    <?php //if(!empty($_POST['first']) && !empty($_POST['second'])){
            $number = $_POST['first'];
            $number2 = $_POST['second'];
    
            echo "Answer:  ";
            if($_POST['operator'] == 'add'){
                $complete = $number + $number2;
                echo " $number + $number2 = $complete";
            }
            if($_POST['operator'] == 'subtract'){
                $complete = $number - $number2;
                echo "$number - $number2 = $complete";
            }
            if($_POST['operator'] == 'multiply'){
                $complete = $number * $number2;
                echo "$number X  $number2 = $complete";
            }
            if($_POST['operator'] == 'division'){
                $complete = $number / $number2;
                echo "$number / $number2 = $complete";
            }
        //} 

    0 讨论(0)
  • 2021-01-20 12:02

    Prevent the default action of the form submit.

    function submitFormData(event) {
       // prevent the default action
       event.preventDefault();
    
       // Other pieces of code
    
    0 讨论(0)
提交回复
热议问题