Reputation: 1
I am trying to make my form submit the data without reloading the page itself but currently I am struggling to do so.
Nothing is happening when I click my submit button in the current setup I have. If I remove the onsubmit="return validateForm()"
then the data will save but the page reloads.
Form HTML
<form id="predictionform-1" action="" method="post" onsubmit="return validateForm()"><p style="text-align: center;"><input type="hidden" id="_footballpool_wpnonce" name="_footballpool_wpnonce" value="f12119edf4"><input type="hidden" name="_wp_http_referer" value="/play/"><input type="hidden" name="_fp_form_id" value="1"></p><table id="matchinfo-1" class="matchinfo input"><tbody><tr><td class="matchtype" colspan="11">All</td></tr><tr><td class="matchdate" colspan="11">Dec 13, 2016</td></tr><tr id="match-5-1" class="match open" title="match 5">
</tr>
<tr><td class="score" style="width: 48%; text-align: center;"><input type="text" maxlength="3" name="_home_5" value="3" class="prediction"></td>
<td style="width: 4%; text-align: center;"></td>
<td class="score" style="width: 48%; text-align: center;"><input type="text" maxlength="3" name="_away_5" value="1" class="prediction"></td>
<td title="score" class="numeric"><span class="no-score"></span></td>
</tr></tbody></table><div class="buttonblock button-matches form-1"><input type="submit" name="_submit" value="Save"></div><input type="hidden" id="_action_1" name="_fp_action" value="update"></form>
JavaScript
<script type="text/javascript">
function validateForm()
{
return false;
}
Any help will be much appreciated.
Thanks
Upvotes: 0
Views: 125
Reputation: 1290
Don´t use the HTML attribute onsubmit, it is not a good practice.
But the problem is that you must catch the event and prevent his default behavior, something like this:
var form = document.getElementById("predictionform-1");
function update(){
console.log("You submited the form!");
}
form.addEventListener("submit", function(event){
event.preventDefault();
update();
});
<form id="predictionform-1" action="" method="post"><p style="text-align: center;"><input type="hidden" id="_footballpool_wpnonce" name="_footballpool_wpnonce" value="f12119edf4"><input type="hidden" name="_wp_http_referer" value="/play/"><input type="hidden" name="_fp_form_id" value="1"></p><table id="matchinfo-1" class="matchinfo input"><tbody><tr><td class="matchtype" colspan="11">All</td></tr><tr><td class="matchdate" colspan="11">Dec 13, 2016</td></tr><tr id="match-5-1" class="match open" title="match 5">
</tr>
<tr><td class="score" style="width: 48%; text-align: center;"><input type="text" maxlength="3" name="_home_5" value="3" class="prediction"></td>
<td style="width: 4%; text-align: center;"></td>
<td class="score" style="width: 48%; text-align: center;"><input type="text" maxlength="3" name="_away_5" value="1" class="prediction"></td>
<td title="score" class="numeric"><span class="no-score"></span></td>
</tr></tbody></table><div class="buttonblock button-matches form-1"><input type="submit" name="_submit" value="Save"></div><input type="hidden" id="_action_1" name="_fp_action" value="update"></form>
The form above won´t submit because sandbox mode don´t allow form submission.
Upvotes: 1