-1

I'm trying to run a function when I click the submit button on my form without the php submitting it.

I tried using onclick="return func_name();" & onClick="return func_name();" but that hasnt worked for me. Also tried changing the input type of the button from submit to button but even that hasnt worked. The php else inside the if condition runs regardless of the function having a return false inside the function.

php for the submit-

if(isset($_POST['submit']))
{
    if($username!='')
            {
                $sql_add="INSERT into work_transaction(work_date,from_time,to_time,work_total_hours,project_name,work_module,task,wprk_description,emp_name,add_date,assigned_by,ticket_no,title, is_billable) values ('$regdate','$fromtime','$totime','$tothr','$OTHP','$module','$_POST[title]','$_POST[worksdetail]','$username','$adddate','$_POST[assignText]','$_POST[ticket_no]','$_POST[title]', '$_POST[is_billable]')"; 
                //echo $sql_add;exit();
                $query_add=mysqli_query($GLOBALS["___mysqli_ston"], $sql_add) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
                if(!$query_add)
                {           
                    ?>
                    <script type="text/javascript">
                        alert("Your Task Details have not been added");
                    </script>
                    <?php
                }
                else{
                    ?>
                    <script type="text/javascript">
                        alert("Your Task Details has been added successfully");
                        var r = "Your Competitor Details has been added successfully";
                        window.location="add_working_details.php";
                    </script>
                <?php
                }
            }
    }
else{}

the function -

function submit(){
    alert("dont submit");
    return false;
}

The submit button -

<input type="submit" name="submit" value="submit" id="submit" onclick="return submit();">

the expected output should be the alert show the message but its not, not sure what im doing wrong. thanks for the help in advance!

Rohan Korde
  • 65
  • 1
  • 7

1 Answers1

1

use preventDefault like below code. preventDefault will prevent all default events then you can run on your code

document.getElementById("mybutton").addEventListener("click", function(event){
  event.preventDefault();
  console.log('preventing')
});
<button id="mybutton">Submit</button>
ANIK ISLAM SHOJIB
  • 3,002
  • 1
  • 27
  • 36