Can a submit button have a function, action and submit?

The below code is meant to allow the user to fill in the fields and then save their details to the database table, although before adding to the database the form must be validated so blank or incorrect information can not be submitted. there is php code to match, and the validation does work if either the ‘action’ or ‘onsubmit’ is deleted but all three can’t work at once. how to fix this?

<ol class="breadcrumb">	
<form method="post" action="sendmail.php" onsubmit="saveUser()">  
 First Name: &nbsp; *<br>
 <input type="text" name="firstName" id="firstName">
 <span class="error"><?php echo $firstNameErr;?></span>
 <br><br>
 Second Name: &nbsp; *<br>
<input type="text" name="secondName" id="secondName">
<span class="error"><?php echo $secondNameErr;?></span>
<br><br>
E-mail: &nbsp; * <br>
<input type="text" name="email" id="email">
<span class="error"><?php echo $emailErr;?></span>
<br><br>
<p><span class="error">* required field.</span></p>
<br><br>
<input type="submit" class="btn btn-lg btn-default btn-block" name="submit" value="submit"><br>

We can’t see the code associated with the action or the onsubmit, so it is impossible for us to know.
Why not use one script to do everything?

i am new to php and completing a simple website for a project, how do i do all as one script?
i have changed the form so users can insert their own username and password, i have taken away the action=“sendmail.php” the onsubmit=“saveUser()” is as follows:

function saveUser()
{
	var xmlhttp;
	xmlhttp= new XMLHttpRequest();
	xmlhttp.open("GET", "insert.php?&firstName="+document.getElementById("firstName").value
				+"&secondName="+document.getElementById("secondName").value
				+"&username="+document.getElementById("username").value
				+"&password="+document.getElementById("password").value
				+"&email="+document.getElementById("email").value, false);
	xmlhttp.send(null);
	alert("Record insert successfully!"); 
}

wheni hit the submit button now, the records are added to the database but once the form is submitted and records added then the validation kicks in, how can i make this happen before?

This is javascript, not php.
But it seems to be calling another php script: insert.php

Note: you should never use GET for data insertion. It’s too easy to have these requests cached and repeatedly issued, even without your intend.

this insert.php then inserts all data to the database. i have changed topic to javascript.

I don’t see any validation, but if you want to handle the form submission on client side you need to prevent the default action:

var form = document.querySelector('form')

form.addEventListener('submit', function validate (event) {
  event.preventDefault()
  
  // Do some basic client side validation
  // Note: this can't replace backend validation!
  
  // If everything is okay, ssubmit the form
  // using AJAX
  saveUser()
})

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.