![]() ![]() edit_class is the class associated with the element for which events are monitored. $("#d3").load("load-text.php",function()) #display_body is the element to where part of the page is loaded after DOM is created. Then same data is retuned with a welcome message. We have used load-text2.php file to receive data from POST method. We will use them to post data to backend, you can read more on how to read data entered in a text box. We will create object by reading two text boxes as first name and last name. In the above code it has used GET method to post data to backend script, to use POST method we have to use object to post data. Our backend file load-text1.php code is here Var msg=encodeURIComponent("Hello World") Its general form is: url : is the only mandatory parameter. When we have blank space, / ? : & = + $ # as part of the string then we can use encodeURIComponent() to encode the data before sending in query string. Sends an asynchronous http POST request to load data from the server. Simply would put this functionality into the function- or class-based view.$("#d1").load("load-text1.php?msg=Hello_world") It can be editing existing data or deleting data you Retrieve data after performing a POST function.Īgain, with a POST request, you can do anything. Than an alert function, or you'll have a get() function, which will then You may put either your own custom function displaying something prettier In production, you may not have this alert() function. Going back to the jQuery code, we then simply have anĪlert() function, which prints out, 'Data Successfully Posted'. So we have a very simple function-based view which posts theĭata that the user has entered into the form to the UserInfo database table. Return render(request,'accounts/userinfoform.html') The user's first name and last name into the database table is shown below. So the Python function-based view that I created to put View to get the values that the user and has entered and then put them in These are important, because we will use these in our We put their values into the variables, firstnamevalueĪnd lastnamevalue. Being that the fields have ids, we use this attribute to Last name field that the user has entered into the form. We must retrieve the first name field and the ![]() The next thing we must do is we must get the data that the Has entered (the first and last name) into the database table. It is in this view that we post the data that the user Has a view that executes code that you want run, either a class-based orĪ function-based view. We specify the URL that we want to operate from. To post the data to our desired database table.įor the AJAX method, we specify, 'POST', because this we are looking When the form is submitted, then we run the AJAX function $('#userinfoform').on('submit', function(e)/updatedocumentviews/',Ĭsrfmiddlewaretoken:$('input').val() The following jQuery code below allows us to do this. How can we now incorporate AJAX into it so that we can take this data from this form and post it to our database without a page refresh? The form has 2 fields, first name and last.Įach field has an id, respectively, firstname and lastname. Let's say we have the following HTML representing a form. With AJAX, we will then store this data into a database table asynchronously- no page refresh needed. In this article, we're going to focus on how to perform a POST request.Īs an example, we will have a basic form in which a user enters his/her first and last name. Perform any type of function needed in AJAX. ![]() Posting data to a database (or storing data to a database), editing existing data in a database, and deleting data from a database. In this article, we show how to perform an AJAX POST request in Django with jQuery.Ī POST request is important in doing things in AJAX, because with a POST request, you can perform a dynamic number of functions, including ![]() How to Perform an AJAX POST Request in Django with jQuery ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |