0

I wanna send the value from controller to ejs file without reloading page when I summit input in ejs. This is code in ejs file.

<html>
    <p class="topicCard3">History Of PM2.5 Values ​​In <%= country %></p>
        <form method="POST" action="/historyPM2.5">
            <input class="inputCountry" name='country' placeholder="enter country name">
            <button class="submitButton">SUBMIT</button>
        </form>
    <p>
<html>

and this is code in controller.js

exports.historyPm = function(req,res) {
    country = req.body.country
    res.render('home',{
        'country': country
    })
}

I need to process something in controller then send result to ejs for showing. Now, I send the value input from ejs to controller and then click summit button the page will reload. Thank for helping.

1
  • 1
    you need to use ajax for making a request to an endpoint and get data... Commented May 20, 2020 at 14:36

2 Answers 2

1

You need just to prevent the form from reloading the page when clicking on submit button. We can achieve that by multiple techniques and here below a jQuery

example:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    $('form').submit(function(e){
        e.preventDefault(); // prevents page reloading
       // api request
       return false;
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

0
  <script>
    $(document)
    .on('click', 'form button[type=submit]', function(e) {

        //write your ajax code here

        e.preventDefault(); //stop submitting the form to avoid reload
    });
  <script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.