Monday 5 June 2017

Querying MongoDB on button click with $_POST method

How can I call a query and display the result in the HTML page on a button click using javascript?

I have a search box where users can search for others based on username. Then, when they click a button, it sends the value from the search box and should query my Users collection in MongoDB and return the results. However, all the resources that I find to do this are in PHP with MySQL.

Client-side:

<!-- ADD FRIENDS -->
  <label>Add Friends</label>
  <div id="addAnotherUser" class="input-group">
    <span class="input-group-addon" id="basic-addon1">@</span>
    <input type="text" class="form-control" placeholder="Who do you want to add?" id="search" aria-describedby="basic-addon1">

    <span class="input-group-btn">
      <button id="check" class="btn btn-primary" type="button">Check</button>
    </span>
  </div>

  <div id="results"></div>

  <script type="text/javascript">
    $("#check").click(function(){
      var searchterm = $("#search").val();

      $.post('public/js/search_process.js', { term: searchterm }, function(data){
        $('#results').html(data);
      }); 
    });
  </script>
<!-- /ADD USERS -->

Server-side:

var $_POST = ;

var term = document.write($_POST['term']);

return term;

First off, is it even possible to accept a $_POST method in javascipt? (Yes, I know it's PHP) And how can I do this with MongoDB and javascript? Thanks!



via jblew

No comments:

Post a Comment