isaac_cm Posted June 4, 2006 Share Posted June 4, 2006 I have a small function in the top of my page and I want to call this function when an image is clicked but every time the page is loaded the function is executed how to prevent that and execute the function only when I want ?thanks Quote Link to comment https://forums.phpfreaks.com/topic/11129-about-php-functions/ Share on other sites More sharing options...
AndyB Posted June 4, 2006 Share Posted June 4, 2006 If your function works when someone clicks an image on your site, then your function isn't a server-side php function, it's a client-side javascript function.Care to explain what you're trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/11129-about-php-functions/#findComment-41606 Share on other sites More sharing options...
isaac_cm Posted June 4, 2006 Author Share Posted June 4, 2006 here is the PHP function I trying to make<?php function copy_image_file($source_db_file){ if (!copy($source_db_file, "images/sample.jpg")){ copy("product_img/noimage_large.jpg", "images/sample.jpg"); } }?>now can you tell me what is wrong so my function is executed every time the page is loaded ? Quote Link to comment https://forums.phpfreaks.com/topic/11129-about-php-functions/#findComment-41667 Share on other sites More sharing options...
Orio Posted June 4, 2006 Share Posted June 4, 2006 PHP is executed before the final HTML is being sent to the user's browser. It's being executed on the server-side- your host or your computer.Javascript is a client-side language, and that means it can be triggered by users click, when a user puts his mouse on something, when a user selects something etc'.I dont know (but I dont think) Javascript can copy images, but you try and do something with php:You said you want to copy an image to somewhere, when a user [b]clicks[/b] on a pic. So you can place a link to the pic, something like this (pay attention to bold part):[b]<a href="copy.php?img=img1&ext=gif">[/b]<img src="img1.gif" border=0></a>Now as you see, when the user clicks the image, he is directed to "copy.php" trasnfering data with the "GET" method (img=img1).Now in copy.php, you can do this (your code):[code]<?php $img=$_GET['img'];$img.=".";$img.=$_GET['ext'];if (!copy($img, "images/sample.jpg")){copy("product_img/noimage_large.jpg", "images/sample.jpg");}?>[/code]Then you can have link or a redirection to previous or other page.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/11129-about-php-functions/#findComment-41692 Share on other sites More sharing options...
isaac_cm Posted June 4, 2006 Author Share Posted June 4, 2006 so there is no way I can embed php code in the page and executed when I want ?how about a javascript call to the php function ? Quote Link to comment https://forums.phpfreaks.com/topic/11129-about-php-functions/#findComment-41697 Share on other sites More sharing options...
kenrbnsn Posted June 4, 2006 Share Posted June 4, 2006 For that you need to use AJAX. See the forum [a href=\"http://www.ajaxfreaks.com/\" target=\"_blank\"]AJAXfreaks[/a].Ken Quote Link to comment https://forums.phpfreaks.com/topic/11129-about-php-functions/#findComment-41699 Share on other sites More sharing options...
isaac_cm Posted June 4, 2006 Author Share Posted June 4, 2006 I do not want to go so far is there a simple method I can use Quote Link to comment https://forums.phpfreaks.com/topic/11129-about-php-functions/#findComment-41711 Share on other sites More sharing options...
kenrbnsn Posted June 4, 2006 Share Posted June 4, 2006 That is the only way Javascript can "call" a PHP script in line and continue to execute. It's really not that hard to do. Look at the Yahoo User Interface Library, specifically the [a href=\"http://developer.yahoo.com/yui/connection/\" target=\"_blank\"]Connection Manager[/a]. Ken Quote Link to comment https://forums.phpfreaks.com/topic/11129-about-php-functions/#findComment-41715 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.