bonaca bonaca - 3 months ago 11
PHP Question

How to avoid URL $_GET variables after submitting a form with ajax?

<form id='gbookform'>
<input id='inputname' type='text' name='name' autocomplete='off' placeholder='Your name'>
<input id='inputcon' type='text' name='contact' autocomplete='off' placeholder='Your contact or website'>
<div id='forminfo'></div>
<textarea id='inputstory' name='story' rows='9' placeholder='Your story'></textarea>
<div class="g-recaptcha" data-sitekey="6LfobygTAAAAACxBzCl1FxPe4lALE-669wt6NC7G"></div>
<input id='btnsubmit' type='submit' value='SEND'>
</form>


JS

$("#gbookform").submit(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "gbookajax.php",
data: $(this).serialize(),
success: function(data) {
$('#dataload').load('gbookdata.php');
$('#forminfo').text(data).show();
}
})
})


After submitting the form I see my variables written inside url:

http://localhost/gbook.php?name=q&contact=q&story=q&g-recaptcha-response=


How can I avoid this ?

Answer

Just specify the form method :

<form id='gbookform' method='post'>...</form>