How to remove form after it's submitted in Laravel PHP?

- 1 answer

Ad

My site uses OctoberCMS and I have a contact form in which I want to display a success message in a partial and hide the form after it's submitted. Here is the code snippet for the form,I cleared unnecessary parts of it :

<form role="form" class="cntct" id="cntct" data-request="onSend" data-request-update="success: '#result'">
     <div class="form-group">
      <!-- Some input fields here !--> 
         <div class="row">
            <div class="col-sm-6">
               <button type="submit" class="btn btn-lg btn-info">Send message</button>
            </div>
          </div>
     </div>
 </form>
 <div id="result">
   {% partial 'success' %}
 </div>

Form is working, I can send emails and the success partial shows its content without any errors. I just want to remove the form,if it's possible by using PHP, and display only #result container.

Ad

Answer

Ad

If you put the form partial inside a div with an id="form" like this:

<div id="form">
    {% partial __SELF__~"::partials/form" %}
</div>

then you can hide the the form and show the result from the ajax handler by returning the new partial to be rendered in the div:

public function onSend()
{
    # save the data

    return ['#form' => $this-renderPartial('@partials/success')];
}

This assumes you have the form and success partial in your components partials folder.

Ad
source: stackoverflow.com
Ad