Click here to Skip to main content
15,905,144 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I have a form with input type button:

HTML
<form method="post" action="index.php" id="addForm" name="addForm" enctype="multipart/form-data">
      ...
      <input type="button" class="btns" value="Add" id="submitAdd" name="submitAdd"/>
</form>


This is my jQuery code that I use for form validation and submitting form:

JavaScript
$('#submitAdd').click(function (){
	// validating input and if everything is right submitting the form	
	$('#addForm').submit();
});


Now if I want to check if button submitAdd is empty nothing happens:
PHP
if(!empty($_POST('submitAdd'))) //this is always false; if I put true script is normally done 


What I have tried:

I can't put input type submit instead button because form is then automatically submitted. How can I avoid this situation? Please help.
Posted
Updated 5-Jul-16 6:12am
Comments
PeMaCN 5-Jul-16 10:58am    
Any help?

1 solution

First, the form:
HTML
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<form method="post" action="index.php" id="addForm" name="addForm" enctype="multipart/form-data">
      <!-- other input types -->
      <input type="submit" class="btns" value="Add" id="submitAdd" name="submitAdd">
</form>
<script>
$('#addForm').submit(function (){
	// validating input and if everything is right submitting the form	
	//$('#addForm').submit();
    valid = false; // true or false based on the validation outcome
	if(valid) {
		return true; // proceed to submit
	
	} else {
		return false; // abort submission
	}

});
</script>
</body>
</html>

Next, the PHP side, it should be [] not (), i.e.
$_POST["submitAdd"]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900