Jump to content


Photo

about php functions


  • Please log in to reply
7 replies to this topic

#1 isaac_cm

isaac_cm
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 04 June 2006 - 12:46 AM

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

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 04 June 2006 - 01:06 AM

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?
Legend has it that reading the manual never killed anyone.
My site

#3 isaac_cm

isaac_cm
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 04 June 2006 - 10:54 AM

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 ?

#4 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 04 June 2006 - 01:21 PM

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 clicks on a pic. So you can place a link to the pic, something like this (pay attention to bold part):
<a href="copy.php?img=img1&ext=gif"><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):
<?php 
$img=$_GET['img'];
$img.=".";
$img.=$_GET['ext'];
if (!copy($img, "images/sample.jpg")){
copy("product_img/noimage_large.jpg", "images/sample.jpg");
}
?>

Then you can have link or a redirection to previous or other page.

Orio.
Think you're smarty?

(Gone until 20 to November)

#5 isaac_cm

isaac_cm
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 04 June 2006 - 01:36 PM

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 ?


#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 04 June 2006 - 01:41 PM

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

Ken

#7 isaac_cm

isaac_cm
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 04 June 2006 - 02:16 PM

I do not want to go so far is there a simple method I can use

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 04 June 2006 - 02:28 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users