Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to refresh only a specific section of a page except using Ajax

I have a simple form to get user email address. But I also need to get the users name and age as well and display them in the page for only a one time. When the user submit his name and age, it will be displayed in the same page using HTML JavaScript DOM. Now what I need to do is that I want to prevent the whole page from refreshing once the user submit his email cause it will clear the users name and age which was given only for a temporary time and I also don’t need to store them in the database as well. Is there have any idea of how to do that except using Ajax? Any help will be greatly appreciated!

function submitForm(){
    if (document.getElementById("age").value > 10){
        document.getElementById("postNameText").innerHTML = document.getElementById("name").value;
        document.getElementById("form2").style.display = "block";
    } else {
        document.getElementById("showData").innerHTML = "You are not eligible to use this site.";
    }
    document.getElementById("showData").style.display = "block";
    document.getElementById("form1").style.display = "none";
}
<form id="form1">
    <input type="text" name="name" id="name" placeholder="Name">
    <input type="number" name="age" id="age" placeholder="Age">
    <button type="button" onclick="submitForm()">Send</button>
</form>

<h2 id="showData" style="display: none;">Your name is <span id="postNameText"></span> and you are eligible to use this site.</h2>

<form action="process.php" method="post" style="display: none;" id="form2">
    <input type="email" name="email" id="email" placeholder="Email">
    <input type="submit" value="Submit" id="submitData">
</form>

Thank you!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this!

function submitForm(){
    if (document.getElementById("age").value > 10){
        document.getElementById("postNameText").innerHTML = document.getElementById("name").value;
        document.getElementById("form2").style.display = "block";
    } else {
        document.getElementById("showData").innerHTML = "You are not eligible to use this site.";
    }
    document.getElementById("showData").style.display = "block";
    document.getElementById("form1").style.display = "none";
}
<form id="form1">
    <input type="text" name="name" id="name" placeholder="Name">
    <input type="number" name="age" id="age" placeholder="Age">
    <button type="button" onclick="submitForm()">Send</button>
</form>

<h2 id="showData" style="display: none;">Your name is <span id="postNameText"></span> and you are eligible to use this site.</h2>

<iframe src="email.html" frameborder="0" id="form2" style="display: none;"></iframe>

Include your second form inside a new HTML file called email.html and this will prevent the page from reloading.

Thanks and best regards!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading