Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you are using a <code>&lt;button&gt;</code> element to activate the serialize and ajax, and if that <code>&lt;button&gt;</code> element is within the <code>form</code> element, the <code>button</code> automatically acts as a form submission, no matter what other .click assignment you give it with jQuery.</p> <h2>type='submit'</h2> <p><code>&lt;button&gt;&lt;/button&gt;</code> and <code>&lt;button type='submit'&gt;&lt;/button&gt;</code> are the same thing. They will submit a form if placed within the <code>&lt;form&gt;</code> element.</p> <h2>type='button'</h2> <p><code>&lt;button type='button'&gt;&lt;/button&gt;</code> is different. It is just a normal button and will not submit the form (unless you purposely make it submit the form via JavaScript).</p> <p>And in the case where a <strong>form</strong> element has no action attribute specified, this submission simply sends the data back onto the same page. So you will end up seeing a page refresh, along with the serialized data appearing in the URL as if you used GET in your ajax.</p> <h2>Possible solutions</h2> <p>1 - Make the <code>&lt;button&gt;</code> type <code>button</code>. As explained above, this will prevent the button from submitting the form.</p> <p>Before:</p> <pre><code>&lt;form id='myForm'&gt; &lt;!--Some inputs, selects, textareas, etc here--&gt; &lt;button id='mySubmitButton'&gt;Submit&lt;/button&gt; &lt;/form&gt; </code></pre> <p>After:</p> <pre><code>&lt;form id='myForm'&gt; &lt;!--Some inputs, selects, textareas, etc here--&gt; &lt;button type='button' id='mySubmitButton'&gt;Submit&lt;/button&gt; &lt;/form&gt; </code></pre> <p>2 - Move the <code>&lt;button&gt;</code> element outside the <code>&lt;form&gt;</code> element. This will prevent the button from submitting the form.</p> <p>Before:</p> <pre><code>&lt;form id='myForm'&gt; &lt;!--Some inputs, selects, textareas, etc here--&gt; &lt;button id='mySubmitButton'&gt;Submit&lt;/button&gt; &lt;/form&gt; </code></pre> <p>After:</p> <pre><code>&lt;form id='myForm'&gt; &lt;!--Some inputs, selects, textareas, etc here--&gt; &lt;/form&gt; &lt;button id='mySubmitButton'&gt;Submit&lt;/button&gt; </code></pre> <p>3 - Add in the <code>preventDefault()</code> into the button click handler to prevent the form from being submitted (it's default action):</p> <pre><code>$("#addShowFormSubmit").click(function(event){ event.preventDefault(); var perfTimes = $("#addShowForm").serialize(); $.post("includes/add_show.php", {name: $("#showTitle").val(), results: perfTimes }, function(data) { $("#addShowSuccess").empty().slideDown("slow").append(data); }); }); </code></pre> <p>Obviously without seeing all your code, I have no idea if this is the case for your issue, but the only reason I have ever seen behavior you are describing is because the submit button was a <code>&lt;button&gt;</code> without a type specified.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload