2

on my website after clicking button I want to display new form which will allow users to add opinion. Im not sure in which way I should add jquery script to my project. I tried to it on simple html site and it works, but I couldnt do the same with flask app. Here is my code:

% extends "bootstrap/base.html" %}

{% block content %}

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css">
<script src="http://ajax.googleapis.com ajax/libs/jquery/1/jquery.js"></script>

 <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.js"></script>
 <script type="text/javascript">

$(function () {
  $("#btnclick").click(function () {
    $("#divpopup").dialog({
      title:"Dodaj opinie",
      width: 430,
      height: 200,
      modal:true,
      buttons: {
        Close:
        function(){
          $(this).dialog('close');
        }
      }
    });

  });
})

<div id="divpopup" style="display: none">
Here will be form
</div>

 <button type="button" id="btnclick">Add opinion</button>

{% endblock %}

And after clicking on the button nothing happend. I use bootstrap and jinja2, so should I use other contents to add scripts?

davidism
  • 98,508
  • 22
  • 317
  • 288
Frendom
  • 314
  • 4
  • 16

1 Answers1

3

You might consider using a Bootstrap Modal for this as you said you're using Bootstrap in the project.

This is a very simple example copied from their website:

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        INSERT FORM HERE
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

If you're using Bootstrap 4 you should be able to copy this right in and change the IDs to match what you want. Link to documentation: https://getbootstrap.com/docs/4.0/components/modal/

Caleb Lawrence
  • 93
  • 1
  • 4
  • 10
  • Ok, it works. Now I have to look for soma AJAX magic to send information to database – Frendom Nov 19 '18 at 17:43
  • Awesome glad you got it working. If you setup a route in you're flask app that will insert information into the database it should be really easy. This link has information about doing an AJAX Post: https://stackoverflow.com/questions/1960240/jquery-ajax-submit-form – Caleb Lawrence Nov 19 '18 at 17:46
  • Also to implement the path in Flask check this out: https://stackoverflow.com/questions/11556958/sending-data-from-html-form-to-a-python-script-in-flask From there you would just have to use some sort of SQL library to insert it into the database. – Caleb Lawrence Nov 19 '18 at 17:47
  • Yeah, my app is in good status atm, so I have loging and adding some stuff to databse. Problem is that now I want to add opinion about books, so I need the book's id from nowhere and I dont want to refresh my site after submit opinion – Frendom Nov 19 '18 at 17:51
  • Not sure what you mean by "need the book's id from nowhere". The opinion form is on a page where the book id is right? So you can have the id somewhere in your POST most likely? Also you shouldn't need to refresh the page if you're using AJAX. You can do a POST on the opinion form and then close the Modal without refreshing. – Caleb Lawrence Nov 19 '18 at 17:55
  • Yeah, that;s what I wanna achieve ;) – Frendom Nov 19 '18 at 18:01