I am uploading files using a hidden iframe as a target...from inside this iframe I want to run a function that's on my main page.
In my target iframe.php I have
<script>
upload_completed(<?php echo $numerolinha ?>);
</script>
but I get from firebug:
ReferenceError: loading_linha_21 is not defined
parent.upload_completed();</script>
If I run upload_completed(loading_linha_21); on my main page it runs ok so I'm guessing the command to run the function on the parent page isn't working... help!
I also tried :
window.top.upload_completed(<?php echo $numerolinha ?>);
window.upload_completed(<?php echo $numerolinha ?>);
my functions :
<script >
function upload_started(id){
$(id).css("display","block");
}
function upload_completed(id){
$(id).css("display","none");
}
</script>
The best guess i can give you is that the jQuery function requires the id to be #name_of_id, so when you call the function from the iframe like so:
parent.upload_completed('#<?php echo $numerolinha ?>');
or you can change your function to do this:
function upload_started(id){
$( '#' + id).css("display","block");
}
function upload_completed( id ) {
$( '#' + id ).css("display","none");
}
Put a ' inside function like below :
<script>
upload_completed('<?php echo $numerolinha ?>');
</script>
Related
Hello I want to execute bb() function on button click.
I tried following code but it did not work.
echo "<div class ='i2' id=".$x.">";
echo "<button type='button' style='display: none;' id='i' name='delete' onclick='document.body.removeChild(this.parentNode)'>";
echo"</button>";
echo "</div>";
<?php
function bb()
{
echo "hello";
}
if (isset($_GET['delete'])) {
bb();
}
?>
Your button is HTML and your function is PHP. They look like together because they are in the same file, but they are not together. PHP exists only on the server. HTML only works on the client (browser). When you see the button on your browser, the PHP is gone, you only have HTML.
To make a HTML button to call a PHP function, you will have to move your function to a PHP file, then make your button to call it with Ajax. Example:
bb1.html : contains button that uses Ajax to call PHP function.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">
function myAjax () {
$.ajax( { type : 'POST',
data : { },
url : 'bb2.php', // <=== CALL THE PHP FUNCTION HERE.
success: function ( data ) {
alert( data ); // <=== VALUE RETURNED FROM FUNCTION.
},
error: function ( xhr ) {
alert( "error" );
}
});
}
</script>
</head>
<body>
<button onclick="myAjax()">Click here</button> <!-- BUTTON CALL PHP FUNCTION -->
</body>
</html>
bb2.php : contains function that returns "hello".
<?php
function bb()
{
echo "hello"; // VALUE RETURNED.
}
bb();
?>
Create two text files with the given names, copy-paste this codes, open your browser and run "localhost/bb1.html".
This is how a button calls a PHP function : Ajax does all the magic.
I created a jquery function in a js file that is included in a php page.
Js file
$( function() {
function disab(){
$('input#idname').attr('disabled', 'disabled');
}
});
In my php file I tried to call the disab function in this way but with no success.
echo "<script>";
echo "$(function(){";
echo "disab();";
echo "});";
echo "</script>";
How can I do this? thanks
first of all put your funciton outside the document.ready function in your js file...so that it is global and can be accessed from anywhere.
function disab(){
$('input#idname').attr('disabled', 'disabled');
}
$( function() { .. }); //use if needed..
and call the function inside <script> tag in php file
<?php
//all you php related codes
...
?>
<script>
$(function(){
disab();
});
</script>
and most important, use prop() instead of attr() ... if you are using latest version of jquery (jquery 1.6+ )
$('input#idname').prop('disabled', true);
I have an ajax function that post to an PHP file.
Now since I'm using WordPress I can use the get_url function so I don't need to hard code the entire URL.
The WordPress function is an PHP so I'm trying to use PHP inside the ajax post. But it wont do the trick.
Any ideas ? and is it possible ?
This is what I have.
$(document).ready(function(){
$('#submit').click(function(){
$.post('<?php echo get_template_directory_uri(); ?>/send.php', $("#mycontactform").serialize(), function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
return false;
});
});
I have also tried the php echo inside quotes like this.
$.post(' "<?php echo get_template_directory_uri(); ?>" /send.php' ....
ether way I get this path
http://mysite.com/%27%3C?php%20echo%20get_template_directory_uri();%20?%3E%27/send.php&email=&message=&name=&sent=1
Javascript to PHP = nope you cant embed javascript to php, only php can embed html and javascripts.
the better way to do it is to create a .php file and insert the javascript there...
Example: js.php
<?php
function doSubmit() {
?>
<script>
$('#submit').click(function(){
$.post('<?php echo get_template_directory_uri(); ?>/send.php', $("#mycontactform").serialize(), function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
return false;
});
</script>
<?php
}
?>
call the js.php using "include(js.php);" and call the functions inside another php
Inside your index.php
<?php
include('js.php');
?>
<html>
<head><script><?php doSubmit();?></script></head>
<body>
</body>
</html>
I'm building a web application inside CodeIgniter and I've decided to have a fiddle with jQuery and Javascript - something I've never really used before.
I have included jQuery in my header using Google Libraries:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
And now I'm fiddling with the SIMPLEST of jQuery but it's not working.
<p><?php if ($post->love) { echo $post->love; } else { echo 0; } ?></p>
<script type="text/javascript">
$("#lovecounter").click(function() {
alert("Click");
});
</script>
I am clicking on the lovecounter link but there is no alert. If I add simple javascript to the actual anchor tag it works though.
<p><?php if ($post->love) { echo $post->love; } else { echo 0; } ?></p>
Any ideas? What am I doing wrong?
Wrap your code in ready handler:
$(document).ready(function(){
$("#lovecounter").click(function() {
alert("Click");
});
});
Working Example
This works perfectly fine for me:
<p>asdf</p>
<script type="text/javascript">
$(document).ready(function() {
$("#lovecounter").click(function() {
alert("Click");
});
});
</script>
You should include a $(document).ready(function() {}); just like Sarfraz said. Otherwise, it would not find anything with an id of lovecounter because nothing on the page has loaded yet. If you wait for it to load, then it will find the element. Here is some documentation: http://www.w3schools.com/jquery/event_ready.asp. Here is a full jQuery lesson: http://www.codecademy.com/tracks/jquery.
I am writing some jquery to call an ajax script every 2 seconds to get the result and update the page. I am mostly a backend programmer and could use some help on this.
This is the code I have now:
<script language="javascript">
function downloadProgress(id) {
$("#" + id + "").load("index.php?_controller=download&_action=getDownloadProgressAjax",
{
downloadId: id
}
);
setTimeout(downloadProgress(id), 2000);
}
</script>
<?php
foreach ($downloads as $dl) {
?>
<div id="<?php echo $dl["download_id"]; ?>">
<script language="javascript">
downloadProgress(<?php echo $dl["download_id"]; ?>);
</script>
</div>
<?php
}
?>
This does not work. What am I doing wrong or would you suggest another approach?
Thanks
I think that you are confusing your PHP script by giving it both query string variables (sent as GET) and data (which is probably getting sent as POST). Try this:
$("#" + id).load("index.php?_controller=download&_action=getDownloadProgressAjax&downloadId="+id }
since you are using jquery, you can use the $.ajax function when the page is ready.
$(function () {
function function downloadProgress(id) {
$.ajax({
url: "index.php?_controller=download&_action=getDownloadProgressAjax&downloadId="+id
})
setTimeout(function () {
if (downloadnotcomplete){ // this way your script stops at some pont.
downloadProgress(id);
}
},2000);
}
});
You will attach the downloadProgress(id) function to your download button or anything else, to trigger the function the first time.
The problem you are having is that you have to provide a parameterless function and not a function call to setTimeout. Also, I would do it a little bit different and use setInterval instead of setTimeout as it relays your intention better in the code. Here is how I would do it:
<script language="javascript">
$(function() {
setInterval(downloadHandler, 2000);
});
function downloadHandler() {
// I'm not sure where the id is coming from you will probably need to put a
// class on your div's so that you can select them.
$(".MyDivClass").each(function() {
var id = $(this).attr("id");
downloadProgress(id);
});
}
function downloadProgress(id) {
$("#" + id + "").load(
"index.php?_controller=download&_action=getDownloadProgressAjax",
{ downloadId: id }
);
</script>
and then on your div:
<?php
foreach ($downloads as $dl) {
?>
<div id="<?php echo $dl["download_id"]; ?>" class="MyDivClass"/>
<?php
}
?>
Hope this helps.