// Get the form element and add event listener to submit event
const form = document.getElementById('myForm');
form.addEventListener('submit', function(e) {
e.preventDefault(); // prevent default form submit action
const date = form.myDate.value; // get the date value from input field
const data = { date: date }; // create data object with date value
// Send data to server using Ajax
fetch('/path/to/server', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => {
// Handle response from server
})
.catch(error => {
// Handle error
});
});
In the above code, we first get the date value from the input field and create a data object with that value. Then we use the fetch() method to send this data to the server using the POST method. We also specify the content type as JSON, so the server knows how to handle the data.
Receiving date value using Ajax:
Let's assume the server sends the date value as a JSON response, and you want to display this value on the web page using Ajax.
JavaScript:
code
// Send Ajax request to server to get date value
fetch('/path/to/server')
.then(response => response.json())
.then(data => {
const date = new Date(data.date); // convert date string to Date object
// Use the date value as needed
})
.catch(error => {
// Handle error
});
In the above code, we send an Ajax request to the server using the fetch() method and get a JSON response. We then convert the date string to a Date object and use the date value as needed.
// Get the form element and add event listener to submit event
const form = $('#myForm');
form.submit(function(e) {
e.preventDefault(); // prevent default form submit action
const date = form.find('input[name="myDate"]').val(); // get the date value from input field
const data = { date: date }; // create data object with date value
// Send data to server using Ajax and jQuery
$.ajax({
url: '/path/to/server',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(data),
success: function(response) {
// Handle response from server
},
error: function(error) {
// Handle error
}
});
});
In the above code, we first get the date value from the input field and create a data object with that value. Then we use the $.ajax() method to send this data to the server using the POST method. We also specify the content type as JSON, so the server knows how to handle the data.
Receiving date value using Ajax:
Let's assume the server sends the date value as a JSON response, and you want to display this value on the web page using Ajax and jQuery.
JavaScript:
code
// Send Ajax request to server to get date value using jQuery
$.ajax({
url: '/path/to/server',
method: 'GET',
dataType: 'json',
success: function(data) {
const date = new Date(data.date); // convert date string to Date object
// Use the date value as needed
},
error: function(error) {
// Handle error
}
});
In the above code, we send an Ajax request to the server using the $.ajax() method and get a JSON response. We then convert the date string to a Date object and use the date value as needed. Note that we set the dataType to 'json' to tell jQuery to automatically parse the JSON response.
In the above code, we first get an array of selected options from the multiple select box. Then we create a data object with these selected options. Finally, we use the $.ajax() method to send this data to the server using the POST method with the content type set to JSON.
code
const myButton = $('#myButton');
myButton.click(function() {
const select1Value = $('#select1').val(); // get an array of selected options from select1
const select2Value = $('#select2').val(); // get an array of selected options from select2
const data = {
select1: select1Value,
select2: select2Value
}; // create data object with selected options from both select boxes
// Send data to server using Ajax and jQuery
$.ajax({
url: '/path/to/server',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(data),
success: function(response) {
// Handle response from server
},
error: function(error) {
// Handle error
}
});
});
In the above code, we first get an array of selected options from two different select boxes. Then we create a data object by combining both arrays of selected options. Finally, we use the $.ajax() method to send this data to the server using the POST method with the content type set to JSON.