Reputation: 167
I added some Javascript validation to a form on my website. The site connects to the DB just fine, however when you click on the submit button, no action is taken.
This is the form located in the footer of the page:
<div id="add_restaurant" class="full-shadow">
<h4>Add a Restaurant</h4>
<form name="submitForm" action="<?php echo $pageName ?>" method="POST">
<ul class="radio_list" id="category">
<li>Category:</li>
<li><label class="radiobtn"><input name="category" type="radio" value="healthy" id="is_healthy" checked="checked"/>Healthy</label></li>
<li><label class="radiobtn"><input name="category" type="radio" value="unhealthy" id="is_unhealthy"/>Unhealthy</label></li>
</ul>
<br />
<label>Name:</label> <input name="rest_name" id="rest_name" type="text" autocomplete="off"/>
<input id="submit" type="submit" name="submit" value="Submit">
</form>
<p id="add_thanks"></p>
</div>
I imagine the problem is something simple I'm over-looking. Can anyone help?
EDIT: I've found that the issue is here in my JS validation:
$('#submit').off('click').on('click', function(){
if($('#rest_name').val() === ""){
$('#add_thanks').html("Please type in a restaurant name.");
}
return false;
});
If I disable this code, my form works. What is causing the problem in this JavaScript code?
Upvotes: 0
Views: 13825
Reputation: 207511
You JavaScript validation is cancelling EVERY click, not just when there is an error.
$('#submit').off('click').on('click', function(){
if($('#rest_name').val() === ""){
$('#add_thanks').html("Please type in a restaurant name.");
return false; //<-- add this
}
//return false; <-- remove this
});
Upvotes: 1
Reputation: 63
It might looks silly. But try this:
Remove all your scripts. And just run the below line on your onclick event.
$("form").submit();
Your form won't be submitted.
Rename the "id" and "name" of your button:
<input id="btnSubmit" type="submit" name="btnSubmit" value="Submit">
Remove all your scripts. And just run the below line on your onclick event.
$("form").submit();
Your form will be submitted.
"You should not use 'submit' as your id and name"
Upvotes: 0
Reputation: 1521
This is what you wrote
<form name="submitForm" action="<?php $pageName ?>" method="POST">
You need to print that $pageName
variable.
<form name="submitForm" action="<?php print($pageName); ?>" method="POST">
Upvotes: 0
Reputation: 129
two places, the first is to print the $pageName in the action section of the form and the second is to define the $submit in the action page.
Upvotes: 0
Reputation: 341
You are not printing $pageName:
<form name="submitForm" action="<?php echo $pageName ?>" method="POST">
Upvotes: 2