Is this right way to call php file using jquery? - javascript

I am trying to call the php file onclick of button ,
i tried using ajax but many of them said not to use for downloading the file.
here is my code.
jQuery(document).ready(function($){
console.log("plugin script loaded3");
$('#csv').click(function()
{
alert("csv");
document.location.href = "/wp-content/plugins/est_collaboration/php/export_csv.php";
});
});
Even tried using $get but its not getting downloaded.
when i click the button it has to call php file which download csv format.

You can simply try this
Download

Change your jquery startup to simply:
$(function() {
$('#csv').click(function() {
alert("csv");
});
});
Alternatively, to download a "file", you can add it as the href to an anchor link, eg:
<a id='downloadlink' href='/wp-content/plugins/est_collaboration/php/export_csv.p‌​hp'>csv</a>
if you need to add parameters, you can change the href via jquery, eg:
$("#downloadlink")
.prop("href", "/wp-content/plugins/est_collaboration/php/export_csv.p‌​hp?param1=" + param1value);
Alternatively (again), it could be that you are adding the "#csv" button dynamically, after jquery startup has run, in which case you need event delegation. See this question for more info: Event binding on dynamically created elements?

Related

onClick is not working in codeigniter

When I try to use onClick in the php page like:
Delete
and in js page I used like:
function deleteCourse(course_id){
alert('trying to delete course');
},
It will shows error like
Uncaught ReferenceError: deleteCourse is not defined
at HTMLAnchorElement.onclick but when I use onClick in the same page its working properly.
courselist.php
<?php if($deleteRole == 1){?>
Delete
<?php }?>
user.js
function deleteCourse(course_id){
alert('trying to delete course');
}
That user.js is included properly, I have seen it in page source.
Delete
The intention of a tag is to redirect. If you are not redirecting, why you want to use a tag, instead i suggest you to button or li tags (if used in menu)
I am changing the attributes. The reason we go for jquery is to seperate HTML and JS codes. Dont use javascript codes inside HTML if you are using jquery.(A suggestion, which you might omit)
Delete
In user.js file (loaded after jquery file)
$(document).on("click", ".delete", function(event){
event.preventDefault();
event.stopPropagation();
courseId = $(this).attr("data-courseId");
alert('trying to delete course');
//your logic for deleting the course.
});
Delete
You dont need to use quotes while passing integer to JS function

Jquery click event function is can't download csv file?

At the first , I export csv file by html onclick element !It was working.When I change it to jquery click event for some reason ! It is can't export csv file ? Why, I think the same function is called !
Export.Export() is for my csv file downloading function.
This code can download csv file
Export
This is not working
Export
<script>
$('.export').click(function(e){
e.preventDefault();
alert("click");//click
Export.Export.apply($(this), [$('table.listtbl'), 'Table.csv']);
});
</script>
Maybe the click event is bound before all the scripts are loaded..
try :
$( document ).ready(function() {
$('.export').click(function(e){
Export.Export.apply($(this), [$('table.listtbl'), 'Table.csv']);
});
});
I'd never use an anchor-tag together with $(element).click(function(){});. How about using
<button onclick="exportcsv(this);">Export</button>
<script>
function exportcsv(e){
var $e = $(e);
Export.Export.apply($e, [$('table.listtbl'), 'Table.csv']);
}
</script>
Try using Export, Hope it should work. Make sure you script is loaded after the tag is loaded.
If href is blank the current page will be reloaded before calling the click event, So the page will be refreshed before downloading the file.

Setcontent of tinymce from external file

How can I load a specific html template inside my TinyMCE editor?
I saw this code but it doesnt work in me;
$.get("hello.html", tinyMCE.activeEditor.setContent);
and then I tried this code so that it will automatically load;
$(window).load(function(){
$.get("../../../../app/views/tools/prooftemplate.phtml", tinyMCE.activeEditor.setContent);
});
but it doesnt work.
Try this:-
You need to pass along the data to a callback function then use that data in the function as I've done. Data from the file is then passed to "d" so we update tinyMCE with the newly loaded data.
$(window).load(function(){
$.get("../../../../app/views/tools/prooftemplate.phtml", function(d){
tinyMCE.activeEditor.setContent(d);
});
});

jQuery iframe onload

I have an iframe that represents the content of my main page and it loads a different src depending on the menu option chosen. I want to perform a series of actions as soon as the iFrame is loaded but I cant seem to get it to work. The code looks more or less like the following:
$(document).ready(function () {
$('#navigation').load(function () {
alert('frame loaded!')
});
});
.load for event binding was deprecated in jQuery 1.8, use .on("load" instead.
I asume this function is in your iFrame and will be executed after the document loading. If not - ther is some conceptual error. Anyway - the load function in jQuery will try to load a html document or piece of it in the object you specify - http://api.jquery.com/load/

jquery script to extract src from iframe

I want to extract the src of the iframe which are present in a html page and save those in a file. I wanted to use jquery for doing this. Is it possible to write a jquery . Which one of php /javascript /jquery should I use for this.
Thanks,
Vinay
Try this
var ifrms = [];
$("iframe").each(function() {
ifmrs.push(this.src);
});
$.ajax(function(){
url: "urlOfThePage",
data: { sources: ifmrs }
});
First retrieve the source of each iframe element on the page:
$(document).ready(function() {
$("iframe").each(function() {
//You'll want to persist this value somewhere instead of alerting it.
alert($(this).attr("src"));
});
});
Once you have the iframe source(s), it's up to you how to submit them to the server. You could persist the value(s) in a hidden element in the form or use the .ajax() method of jQuery to post the form via AJAX.

Categories

Resources