Jump to content

Archived

This topic is now archived and is closed to further replies.

isaac_cm

about php functions

Recommended Posts

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
For that you need to use AJAX. See the forum [a href=\"http://www.ajaxfreaks.com/\" target=\"_blank\"]AJAXfreaks[/a].

Ken

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.