Home > Net >  How to prevent google app script reloading/redirecting my webpage after form data submission ..?
How to prevent google app script reloading/redirecting my webpage after form data submission ..?

Time:05-06

I am using google app script to submit form data to my email. It's a webpage contact form. But by default after hitting the submit button it takes to a different page and shows a "result:success" weird kinda message. I want to prevent that reload or redirect. How can I do that?

HTML

<form id="email-form"  action="https://script.google.com/macros/s/AKfyJu890G2t-RuhJxTNIA/exec" method="post">
        <div >
            
            <input id="name" name="name" type="text"  placeholder="write name" autocomplete="off">
        </div>
        <div >
            
            <input id="email" name="email" type="text"  placeholder="write email" autocomplete="off">
        </div>
        <div >
            
            <input id="subject" name="subject" type="text"  placeholder="write related sub" autocomplete="off">
        </div>
        <div >
            
            <textarea id="message" name="message"  placeholder="write message here"></textarea>
        </div>

        <button id="send-email" type="submit">SUBMIT EMAIL</button>

        
    </form>

I can add an alert using onclick event but still the reload happens. Please provide javascript code for preventing the default behavior for this form.

CodePudding user response:

I've been able to get around this before by moving the submit button out of the form.

<form id="email-form"  action="https://script.google.com/macros/s/AKfyJu890G2t-RuhJxTNIA/exec" method="post">
        <div >
            
            <input id="name" name="name" type="text"  placeholder="write name" autocomplete="off">
        </div>
        <div >
            
            <input id="email" name="email" type="text"  placeholder="write email" autocomplete="off">
        </div>
        <div >
            
            <input id="subject" name="subject" type="text"  placeholder="write related sub" autocomplete="off">
        </div>
        <div >
            
            <textarea id="message" name="message"  placeholder="write message here"></textarea>
        </div>
    </form>

        <button id="send-email" type="submit">SUBMIT EMAIL</button>

If that does not work you can try using target to a hidden iframe.

  • Related