Butt wikiristan jQuery.

submit / Golovna

Basic functionality

This article uses the code that I use to force ajax requests to the server via jQuery.

Their functions can be different, so you can use different functions to make writing code easier.

Submit html data to the additional Load function

This is the simplest ajax request via jQuery by extracting html data and inserting it into a dom element with id="result" (the element is replaced instead):

$("#result").load("");

More advanced version of vikoristan load:

$("#result").load("", (par1:val1, par2:val2, ...), function(response, status, xhr) ( if (status == "success") ( alert("Done ) "); ) else (alert("Arrear: " + xhr.status + " " + xhr.statusText); ) )));

For which application, parameters are also passed to the server, and after the connection is removed, it is checked that there was no cancellation (for example, the connection from the server was not rejected) and other actions are terminated.

Ajax-using GET and POST functions

These functions operate via ajax delivery using the http get and post methods.

I’ll point out a few butts of their vikoristan.

$.get("", // send addresses (par1:val1, par2:val2, ...), // transfer of some data function(data) ( // some actions with data retrieved from the server data ));

The transfer of data is obligatory, as well as the confirmation of any actions after disconnecting from the server, then.

In this case, rows 3 and 4-6 can be deleted if necessary and thus shorten the code.

The type of contents in the data server can be specified by adding dataType (div. below) - the values ​​are assigned automatically.

The wiki post is similar, but in the current application the wiki function is activated after disconnecting from the server.

$.getJSON("", (par1:val1, par2:val2, ...)).success(function(data) ( // let's work with the data, for example, loop through it and print it: for (var i= 0; i 10, "text" => "test row 1"); " => 30, "text" => "test row 3"); echo json_encode($arr);

So you can simply transfer stdClass objects from the server, converting them to a json row.

Simple ajax-powered via jQuery for additional AJAX functions

Now I’ll use a simple get function, use the ajax function, and extract html data.

$.ajax(( url: "", dataType: "html", success: function(data) ( // everything with the removed data data )));

The request to the server is made using the get method, because

parameter, similar to the request type, type behind the old GET.

More collapsible ajax powered via jQuery

The Windows application is equipped with the ajax function by transferring data using the post method and processing it.

Below I will describe the additional parameters that are most often used.

$.ajax(( url: "", type: "post", data: "", // can be in a row, or, for example, like this: $("input, input:checked, input:checked, select, textarea) " ) dataType: "json", beforeSend: function() ( $("#sendajax").button("loading"); ), complete: function() ( $("#sendajax").button("reset" ) ; ), success: function(json) ( // what's wrong with the data thrown), error: function(xhr, ajaxOptions, thrownError) ( alert(thrownError + "\r\n" + xhr.statusText + "\r \ n" + xhr.responseText); ) ));

Data override button:

Submit

In the hovered application, when you press the button, the button’s status immediately changes (the text on it changes to “Submitting...” and it becomes inactive), so you need to use an additional beforeSend parameter. Then the task is to transfer the required data.
After the connection from the server is disconnected, the button’s position is rotated to the front (the text changes to “Send” and becomes active). Please exit in the form of json data.
I will briefly describe the parameters for sending an ajax request, which can most often be useful: url
Addresses powered by ajax-asked type
Method of requesting GET or POST data
Data that is sent to the server. Asynchronously, I will ask the Wikonanny, then.
The program continues to quit without checking the server. If you enter false, the input will be executed synchronously, in which case the page will not respond to anything until the connection is removed from the server.
processData Conversion of data to be sent to URL format.
If necessary, so that the data is not recreated, set it to false. For example, when sending images or XML data to the server.
contentType The data type that is transmitted is defined as "application/x-www-form-urlencoded; charset=UTF-8".
If you enter false, the type is not passed into the header, which may be necessary, for example, when sending an image to the server. beforeSend
Function before sending an ajax request. complete

The function that ends after disconnecting the connection from the server (be it successful).

success

The function that is executed upon a successful confirmation.

error

The function is changed at the time of washing.

Below I will point out a number of applications for vikoristan ajax requests.

Powering up your form with ajax-powered data via jQuery

Zrazkovy html-form code:

Your name:

JavaScript code:

$("#myform").submit(function(e) ( e.preventDefault(); $.ajax(( type: $(this).attr("method")), url: "", data: $( this) ).serialize(), async: false, dataType: "html", success: function(result)( alert("Form submitted"); ) ));

To prevent the page from being re-enabled when you click on the “submit” button, use the standard browser settings and e.preventDefaults() first.

In the data parameter we pass all the form fields, vikorista $(this).serialize() - this function converts all inputs and selects into a row suitable for sending to the server.

The async: false parameter has also been changed here, so that until the form is sent to the server, nothing else can be entered or generated.

The essence of the idea is that a standard input for selecting a file is displayed on top of the button, but there is an opening on the surface and has the same dimensions as the button.

This is how you create a button, and if you point the cursor at it, you actually point at the input.

Apparently, when you press the button, you actually press the file selection input.

In order to avoid blurring the cursor after selecting a file, set the task font size to 0px.

Now the javascript code for sending the file to the server with progress shown:

$(function() ( $("#load_file").on("change", loadfile); ));

if (empty($_FILES["upload"]["name"]) || $_FILES["upload"] == "none") ( $message = "You did not select a file"; ) else if ($_FILES[ "upload"]["size"] == 0 || $_FILES["upload"]["size"] > 9437184) ( $message = "The file size does not meet the standards (maximum 9 MB)"; ($_FILES[ "upload"]["type"] != "image/jpeg") && ($_FILES["upload"]["type"] != "image/pjpeg") && ($_FILES["upload "][" type"] != "image/gif") && ($_FILES["upload"]["type"] != "image/png")) ( $message = "Only JPG, GIF and PNG images are allowed." ; ) else if (!is_uploaded_file($_FILES["upload"]["tmp_name"]))) ( $message = "Something went wrong. Try uploading the file again."; ) else ( $ ftype = $_FILES[ "upload"]["type"]; $fname = "newname_image.".($ftype == "image/gif" ? "gif" : ($ftype == "image/png" ? " png" : "jpg ")); if (move_uploaded_file($_FILES["upload"]["tmp_name"], $_SERVER["DOCUMENT_ROOT"].."/files/".$fname)) ( $message = "The image has been successfully uploaded."; ) else ( $message = "Something went wrong. Try to access the file again."; ) ) exit($message);

Information about uploading images is located in $_FILES["upload"], because

The script added the file like this: form.append("upload", files);

Apparently, everything you need in PHP programs is to check that the file matches the parameters you are searching for, move the file to the required folder (in the application, in the files folder) under the required names (in the application, newname_image) and turn to Browser confirms how it is in mine applications are simply displayed using the alert (message) command;

There are a lot of situations in which it is possible and necessary to use ajax requests, and all of them cannot be discussed here.

Prote, as well as propositions, which can be applied here, write in the comments.

AJAX is a group of technologies used by web developers for creating interactive applications.

AJAX allows you to transfer data from the server without reloading the page.

In this manner, even hostile results can be eliminated.

And the jQuery library significantly simplifies the implementation of AJAX by using additional methods.

To implement the technology, the $.ajax or jQuery.ajax method is used:

data (object/row) - data from the customer, which is transmitted to the requested page.

dataType (row) - possible values: xml, json, script or html.

Description of the type of data that is found in the server output.

type (row) – type of input.

Possible values: GET or POST.

Typically: GET.

url (row) – URL addresses for the request.

Butt 1

Easy transmission to text.

$.ajax(( url: "response.php?action=sample1", success: function(data) ( $(".results").html(data); ) ));

For a video, there is a div element.

Check out the feeds

The server simply rotates the row:

Echo "Butt 1 - transfer completed successfully";

Butt 2

We pass the data to the PHP script.

$.ajax(( type: "POST", url: "response.php?action=sample2", data: "name=Andrew&nickname=Aramis", success: function(data)( $(".results").html( ) data);

The server rotates the row of previously inserted data:

Echo "Butt 2 - transfer completed successfully. Parameters: name=".

$_POST["name"] .

", nickname=".

$_POST["nickname"];

Butt 3

Passing on to JavaScript Code

$.ajax(( dataType: "script", url: "response.php?action=sample3", ))

The server writes the code:

Site map