Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm making a survey for a school assignment and I can't figure out how to reload the page of my survey, in order to clear the data previously entered, after an alert message has been displayed from the click of a submission button.

What I have tried:

<!--My script looks like this-->
<script>
        function message(){
            let name = document.getElementById("name").value
            let email = document.getElementById("email").value
            if(name != '' && name == ' ' && email == '' && email == ' '){ 
            alert("Thanks for your submission " + name + "!")
        } else if(name == '' && email == '' || name == ' ' && email == ' '
            || name == ' ' && email == '' || name == '' && email == ' '){
            alert("Please eneter a name and email!")
        } else if(name == '' || name == ' '){
            alert("Please eneter a name!")
        } else if (email == '' || email == ' '){
            alert("Please eneter an email!")
        }
  
    }
    function reload(){
        window.location.reload()
    }
    </script>

<!--and my button looks like this-->
<button type="button" onclick="message(); reload()">Submit Survey</button>
Posted
Updated 9-Jan-23 21:48pm
Comments
Member 14024838 19-Sep-23 3:04am    
like

1 solution

You haven't explained what the problem is. Assuming you only want to reload the page if one of the conditions in your message function is met, then you'll need to return a value from that function indicating whether a condition was met, and use an if block to conditionally execute the reload function.

You should also look at the trim[^] function to simplify your tests, rather than testing for multiple variations of empty strings and single spaces.
JavaScript
function message(){
    const name = document.getElementById("name").value.trim();
    const email = document.getElementById("email").value.trim();
    
    if (name === '' && email === ''){
        alert("Please enter your name and email address.");
        return false; // Don't reload the page
    }
    
    if (name === ''){
        alert("Please enter your name.");
        return false; // Don't reload the page
    }
    
    if (email === ''){
        alert("Please enter your email address.");
        return false; // Don't reload the page
    }
    
    alert("Thanks for your submission " + name + "!");
    return true; // Reload the page
}
HTML
<button type="button" onclick="if (message()) { reload(); }">Submit Survey</button>
 
Share this answer
 
Comments
King Esidore 10-Jan-23 10:57am    
Thanks Richard, i also realized i made a typo in the part were i was making sure the texboxes where filled, and the first part wasn't even working, but this made the web reload work, thanks for the help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900