I used the solution provided in this post: Check for existing email in WooCommerce checkout using Ajax
and it's working great.
The only thing that I need is that the ajax uses a spinner loading to indicates to the user that the system is loading an information.
I am taking an example of your provided reference link. If you use the same line of code then, you have to change the below line of code. Otherwise, please provide your line of code.
In below example first give loadingImage to image that you want to load while processing.
FROM
$.post( validateEmail.ajaxurl, { action:'validate_email', billing_email:input_value }, function(data) {
$('.message').html(data);
});
TO
$('#loadingImage').show(); // show the loading image.
var data = {
'action': 'validate_email',
'billing_email' : input_value
};
$.ajax({
url: validateEmail.ajaxurl,
type: "POST",
cache: false,
data: data,
success : function(html){
$('.message').html(data);
$('#loadingImage').hide(); // hide the loading image
}
});
Here I have added answer to how to set loading image in ajax call. If you want to set loader in outside the input then you have to make below changes
CSS
#loadingImage {
display:none;
}
HTML
Here I'm using some dummy GIF for an example. You can set that any gif and apply css you want.
<p class="form-row form-row form-row form-row-first validate-required woocommerce-validated" id="billing_email_field">
<label for="billing_email" class="">Email <abbr class="required" title="required">*</abbr></label>
<input type="text" class="input-text " name="billing_email" id="billing_email" placeholder="Email" value="abc#gmail.com">
<img src="https://lh6.ggpht.com/_S0f-AWxKVdM/Sc4G-1MAUtI/AAAAAAAAHg8/SoQCgQ6-aSY/1%5B3%5D.gif?imgmax=800" id="loadingImage">
</p>
<span class="message"></span>
Script
$('#loadingImage').show(); // show the loading image.
var data = {
'action': 'validate_email',
'billing_email' : input_value
};
$.ajax({
url: validateEmail.ajaxurl,
type: "POST",
cache: false,
data: data,
success : function(html){
$('.message').html(data);
$('#loadingImage').hide(); // hide the loading image
}
});
Related
I have a requirement where I want to upload an excel file to the controller, read the file, process its data and send it back to the same view as the JSON object.
I want to achieve this using AJAX call as I want to capture its success callback and manipulate DOM as per received response. I have tried a few things but I am not able to hit the controller. Any help on this is appreciated.
Below shown is my JS, HTML and C# code :
function SubmitInfo() {
var formData = new FormData();
formData.append('file', $('#fileInput')[0].files[0]); // myFile is the input type="file" control
var _url = '#Url.Action("Upload", "CrossValidation")';
$.ajax({
url: _url,
type: 'POST',
data: formData,
processData: true, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
success: function (result) {
//manipulate DOM and bind response with Kendo grid
alert("result");
},
error: function (jqXHR) {
},
complete: function (jqXHR, status) {
}
});
}
<div class="col-md-4">
<input id="fileInput" type="file">
</div>
<div class="col-md-4">
<input type="submit" value="Upload file" onclick="SubmitInfo()"/>
</div>
public JsonResult Upload(IFormFile formData)
{
//Do something here....
return Json("");
}
There are two obvious problems in the code you've posted:
processData: true. By setting this to true (the default), you're asking jQuery to attempt to turn the data you're providing into a query string, which is not what you want. Instead, you want to set this to false, which will ask jQuery to leave the data alone and pass it on to the underlying XMLHttpRequest as is, where the data will be formatted correctly as multipart/form-data.
The name argument passed to formData.append must match the name of your IFormFile parameter in the ASP.NET Core MVC Controller. You're using file in the the former and formData in the latter, so you'll need to change one of them so that they're the same.
I am trying to do some very basic ajax. I just want an onchange event on a select that will call an ajax function that will get the options for another select and fill it in. However I am not sure how to do this in a simple way in Typo3 Flow. My php code for the action just looks like this:
public function getProductsByCategoryAction( $category='' ) {
$postArguments = $this->request->getArguments();
echo __LINE__;
TYPO3\Flow\var_dump($postArguments); die;
}
and my ajax call looks like this:
jQuery('#get_category').change(function(event) {
event.preventDefault();
alert('get products');
var category = jQuery('#get_category').val();
alert(category);
jQuery.ajax({
url: "/admin/orders/getproductsbycategory.html",
data: {
'category': category
},
async: true,
dataType: 'html',
success: function(data) {
alert('hi mom');
...
}
});
});
when I try this url in the browser mysite-dot-com/admin/orders/getproductsbycategory.html?category=17ca6f3e-a9af-da7d-75cd-20f8d6a05ed0
on the page the var_dump just gives me array(empty). Why doesn't the request->getArguments() call work and give the category argument?
The getproductsbycategory.html is created in Neos and has the right plugin for the action call. So I know the right action gets run but it does not get any args. At this point the argument is just a string and not an _identity even though I should eventually do it that way, I'm trying to keep things simple for now for the sake of expediency.
Thanks
Update: as a temp workaround shameless hack I just did this to get the variable:
$categoryID = $_GET['category'];
which works but I'd like to know the proper way especially if it does not involve writing my own view helpers.
First define variable in your html file
<script>
var ajaxUrl = '<f:uri.action action="actionName" controller="(controllername)Ajax"/>';
</script>
Your Ajax code will look like :->
$.ajax({
data: '&lookup='+{somevalue},
type: 'POST',
url: ajaxUrl,
success: function(data) {
$('.lookup-id').append(data);
},
});
Your conroller action will look like :->
public function getLookupIdAction(){
// Get company detail for set logo of company
$companyDetail = $this->getUserCompany();
// Template Name
$templateName = 'GetLookupID.html';
$viewVariables = $this->lookupIdentifierRepository->findByCompany($companyDetail);
$templatepath = 'resource://WIND.Alertregistration/Private/Templates/LookupIdentifier/' . $templateName;
$this->standaloneView->setLayoutRootPath('resource://WIND.Alertregistration/Private/Layouts');
$this->standaloneView->setPartialRootPath('resource://WIND.Alertregistration/Private/Partials');
$this->standaloneView->setFormat('html');
$this->standaloneView->setTemplatePathAndFilename($templatepath);
$this->standaloneView->assignMultiple(array("lookUpValue" => $viewVariables));
$this->standaloneView->setControllerContext($this->getControllerContext());
return $this->standaloneView->render();
}
Your view file look like :->
<f:layout name="Lookup" />
<f:section name="Content">
<label for="lookupId" style="box-sizing: border-box;">Identifier</label>
<div class="select-box" style="box-sizing: border-box;">
// Your Code
</div>
</f:section>
I have a View which renders an action. For example:
<h1>Test View</h1>
#Html.Action("Intro", "Account", new { id = "5" })
However, this Action takes 10-20 seconds to load.
How can i wrap it in an ajax with a loader?
Thanks!
Just create a DIV to serve as a placeholder, then load the content on document load event into the DIV. You can also add a GIF image to show progress and hide it when the content is loaded, Here's an example of how to do it:
<h1>Test View</h1>
<div id="accountInfo"></div>
<script>
$(document).ready(function () {
$.ajax({
type: "GET",
url: '#Html.Url("Intro", "Account")',
dataType: "html",
data: { id : 5 },
success: function (content) {
$("#accountInfo").html(content);
},
error: function (e) { }
});
});
</script>
Please note I didn't test that and just wrote it from the top of my head so there might be some minor syntax errors but this gives you the main idea.
Hope it helps!
hi i am using tinymce and trying to load dynamic text to active editor by ajax but i have problem
here is my code
$.ajax({
type: "post",
cache:false,
url: "/sistem/plugins/etiketlink/form/index.php",
data: data,
success: function(data) {
alert(data);
// tinymce.editors[0].setContent(data);
tinyMCE.activeEditor.setContent(data);
},
error: function() {
alert("error!");
}
when i alert data my link are like <a href="/didim.html" alt="didim" />
but when tinyMCE.activeEditor.setContent(data); works links are becoming like
<a href="../didim.html" so how can i delete this (..)
Try using
tinymce.init({
...
convert_urls: false
});
Hope this helps!!
I am developing a mobile site using asp.net and jquery. no plugin. just simple jquery.I am using the
<input type="file"/>
of HTML5.
So few questions to get the big picture:
1.Can i load files without jquery plugin, but only simple jquery? Just picking the file, send it using ajax and catch it on server side?
2. I have noticed the Request.Files attribute of the Request object. Will it get filled only with post of the whole page or can i get my files there using Ajax?
3.In case the answer in 2 is "No!", how do i exclude the files data on the server side?
Thanks
This is the solution i have found:
JS:
<script type="text/javascript">
$(document).ready(function () {
$('#inputFile').on('change', function () {
var file = this.files[0];
var name = file.name;
var size = file.size;
var type = file.type;
var formData = new FormData();
formData.append(file.name, file)
$.ajax({
url: 'AjaxPage.aspx',
dataType: 'script',
cache: false,
contentType: false,
processData: false,
data: formData,
type: 'post',
success: function (response) {
alert(response);
},
error: function (e) {
alert(e);
}
});
});
});
</script>
CS: (On ajax page to catch the files and manipulate them as you will)
var files = Request.Files;
HTML:
<body>
<div>
<input type="file" id="inputFile" />
</div>
</body>