javascript - Confirm a form & display message if form is valid with JQuery -
i'm developing django application , use javascript in order improve website.
i have form , display 2 things :
- confirm form before submit
- if form submitted, display message : 'form saved'
it's first time i'm using js , need make process.
this code :
<form class = "form" method='post' action=''> {% csrf_token %} <br></br> {{ form.as_p}} <br></br> <button type="submit">valider</button> </form> <script> $('#form').submit(function() { var c = confirm("click ok continue?"); return c; //you can return c because true or false }); </script>
and if form valid , saved :
<script type="text/javascript" > $(document).on('valider', 'form.form', function(form) { var $form = $(form); $.ajax({ url:"/path_to_my_html_file/bc_form2.html", type: "post", success: function(form) { alert("form saved"); } }); }); </script>
could me ?
thank you
you can try adopt purpose code:
$('#form').submit(function(e) { // prevents form submitted default post request page reloading e.preventdefault(); if (confirm("click ok continue?")) { // here can call ajax request callajax($('input[type=text]').val()) } }); function callajax(value) { // making ajax request endpoint // ipify example $.ajax({ url:"https://api.ipify.org?format=json", type: "get", success: function(form) { alert("form saved"); } }); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="form"> <input type="hidden" value="mock csrf token" /> <br></br> <input type="text" required /> <br></br> <button type="submit">valider</button> </form>
Comments
Post a Comment