katia Posted June 14, 2006 Share Posted June 14, 2006 How can I change the src value when I press a button? Is there any way I can do that in a function? Link to comment Share on other sites More sharing options...
aspnetguy Posted June 14, 2006 Share Posted June 14, 2006 of an image or an iframe??? Also do you want the page to refresh or do you want to do this without page refreshing??? Link to comment Share on other sites More sharing options...
katia Posted June 14, 2006 Author Share Posted June 14, 2006 I want to change just the image, without refreshing the whole page.I keep my src values of the pictures in an array and when I press a button I want to change the src value of my img tag with another value from the array. Link to comment Share on other sites More sharing options...
Little Goat Posted June 14, 2006 Share Posted June 14, 2006 this can't be done with php. php does stuff when the page loads. to change it without refreshing the whole page, you need Javascript. Link to comment Share on other sites More sharing options...
aspnetguy Posted June 14, 2006 Share Posted June 14, 2006 You will need to use AJAX to achieve this.Read the AJAX tutorials on www.w3schools.com Link to comment Share on other sites More sharing options...
katia Posted June 14, 2006 Author Share Posted June 14, 2006 Ok . Can i call a javascript function in a php function? Link to comment Share on other sites More sharing options...
aspnetguy Posted June 14, 2006 Share Posted June 14, 2006 you can echo some javascript code from a php function but php function only get executed on page refresh.AJAX allows you to execute server-side (PHP) functions without page refresh. Link to comment Share on other sites More sharing options...
katia Posted June 14, 2006 Author Share Posted June 14, 2006 well, i don't want to use ajax too. If I will submit the form, and reload the page, how can I sumbit the array? Link to comment Share on other sites More sharing options...
justsomeguy Posted June 14, 2006 Share Posted June 14, 2006 You will need to use PHP to write the array as a javascript array, and then use javascript to manage the data. Link to comment Share on other sites More sharing options...
katia Posted June 14, 2006 Author Share Posted June 14, 2006 how can i do that? Link to comment Share on other sites More sharing options...
justsomeguy Posted June 14, 2006 Share Posted June 14, 2006 You need to write out the javascript code with PHP. So you would set up the array first:echo "var info = new Array();\n";and then loop through your array and write out the java script:for ($i = 0; $i < count($array); $i++) echo "info[{$i}] = {$array[$i]};\n";Then you can put whatever javascript code you need to use the array. Link to comment Share on other sites More sharing options...
katia Posted June 14, 2006 Author Share Posted June 14, 2006 You need to write out the javascript code with PHP. So you would set up the array first:echo "var info = new Array();\n";and then loop through your array and write out the java script:for ($i = 0; $i < count($array); $i++) echo "info[{$i}] = {$array[$i]};\n";Then you can put whatever javascript code you need to use the array.<{POST_SNAPBACK}> So, I got the array "info' , and "nr" that represents a position in the array . When I submit the form I will increment nr .But when the page reloades nr 's value is the right value but array's value is Array....... Link to comment Share on other sites More sharing options...
justsomeguy Posted June 14, 2006 Share Posted June 14, 2006 If you are reloading the page, you don't even need to use javascript at all. What are you trying to do? Link to comment Share on other sites More sharing options...
katia Posted June 14, 2006 Author Share Posted June 14, 2006 I'm just trying to read the next value (wich is a picture url )from an array and to dispplay it in an image tag , without having to read the whole picture folder over and over again Link to comment Share on other sites More sharing options...
scott100 Posted June 14, 2006 Share Posted June 14, 2006 I'm just trying to read the next value (wich is a picture url )from an array and to dispplay it in an image tag , without having to read the whole picture folder over and over again<{POST_SNAPBACK}> So you have a folder on your server with images in it, you use php to load these images into an array, then you want to create a slideshow that cycles through all these images.Is that correct? Link to comment Share on other sites More sharing options...
katia Posted June 14, 2006 Author Share Posted June 14, 2006 So you have a folder on your server with images in it, you use php to load these images into an array, then you want to create a slideshow that cycles through all these images.Is that correct?<{POST_SNAPBACK}> That's right. Link to comment Share on other sites More sharing options...
scott100 Posted June 14, 2006 Share Posted June 14, 2006 That's right.<{POST_SNAPBACK}> ok so you are going to have to print the contents of the php array into a javascript array, see post #11 by justsomeguy.After you have done that you will need to code a javascript to cycle through it's array display each pic. Link to comment Share on other sites More sharing options...
katia Posted June 14, 2006 Author Share Posted June 14, 2006 ok so you are going to have to print the contents of the php array into a javascript array, see post #11 by justsomeguy.After you have done that you will need to code a javascript to cycle through it's array display each pic.<{POST_SNAPBACK}> I do that and I get an error. Why? <? echo " <input type=\"button\" name=\"Submit4\" value=\"next\" onClick=\"parseArray($picnr $picarray )\"> "; ?> and the java script function parseArray(picnr,picarray){ picnr++;document.getElementById('picture').src=picarray[picnr];} Link to comment Share on other sites More sharing options...
justsomeguy Posted June 14, 2006 Share Posted June 14, 2006 One reason might be because you left the comma out from the first part. Link to comment Share on other sites More sharing options...
katia Posted June 14, 2006 Author Share Posted June 14, 2006 One reason might be because you left the comma out from the first part.<{POST_SNAPBACK}> But if I change it to:<? echo " <input type=\"button\" name=\"Submit4\" value=\"next\" onClick=\"parseArray( $picarray )\"> "; ?> and in the Java script I have just an alert function parseArray(picarray){alert(picarray[0]);} or function parseArray(picarray)ar=new Array();<?phpfor ($i = 0; $i < count($picarray); $i++)echo "$ar[$i] = $picarray[$i]";?>alert(ar[0]);} I only get errors Link to comment Share on other sites More sharing options...
justsomeguy Posted June 14, 2006 Share Posted June 14, 2006 Change this line:echo "$ar[$i] = $picarray[$i]";echo "ar[{$i}] = \"{$picarray[$i]}\";\n"; Link to comment Share on other sites More sharing options...
katia Posted June 14, 2006 Author Share Posted June 14, 2006 Change this line:echo "$ar[$i] = $picarray[$i]";echo "ar[{$i}] = \"{$picarray[$i]}\";\n";<{POST_SNAPBACK}> After changing this line, I did't get error messages anymore , but when I try to check the result , the alert (ar[0]) returns undefined Link to comment Share on other sites More sharing options...
justsomeguy Posted June 14, 2006 Share Posted June 14, 2006 Create the page, and then copy and paste the javascript code that gets produced so I can see it. Link to comment Share on other sites More sharing options...
scott100 Posted June 14, 2006 Share Posted June 14, 2006 After changing this line, I did't get error messages anymore , but when I try to check the result , the alert (ar[0]) returns undefined <{POST_SNAPBACK}> Here's a quick example of how a javascript array should look, are you remembering to enclose it within script tags?<html><body><script type="text/javascript">var ar = new Array()ar[0] = "Saab"ar[1] = "Volvo"ar[2] = "BMW"alert(ar[0])</script></body></html> If you are still having problems try simply printing out the contents of the php array to make sure that values exist in it, then we know that's not the problem. Link to comment Share on other sites More sharing options...
katia Posted June 14, 2006 Author Share Posted June 14, 2006 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Slide show</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><script type="text/javascript"> <!--function parseArray(sirpics){sir = new Array();<?phpfor ($i = 0; $i < count($sirpics); $i++)echo "sir[{$i}] = \"{$sirpics[$i]}\";\n";?>alert(sir[0]);}--></script><body><form name="form1" method="post" action="slideshow.php"> <table width="980" height="1045" border="1" cellpadding="0" cellspacing="0"> <tr> <td width="81" height="128"> </td> <td width="800"><table width="797" height="74" border="1" cellpadding="0" cellspacing="0"> <tr> <td width="259"> </td> <td width="277"><input type="button" name="Button" value="Start"></td> <td width="253"> </td> </tr> <tr> <td><input type="button" name="Submit3" value="back" onClick=""></td> <td><input type="button" name="Submit2" value="Stop"></td> <td><div align="justify"> <? $nrpoza=$nrpoza; $numepoza=$numepoza; $album=$album; $opendir = opendir($album); $dim=0; $sirpics=array(); while($fname=readdir($opendir)) { $poza=$album."\\".$fname; $a=split("\\\\\\\\",$album); $nr=count($a)-1; $pozanou="..\\Img\\".$a[$nr]."\\".$fname; if (is_file($poza)) { $sirpics[$dim]=$pozanou; $dim++; } } echo " <input type=\"button\" name=\"Submit4\" value=\"next\" onClick=\"parseArray($sirpics)\"> "; ?> </div></td> </tr> </table></td> <td width="91"> </td> </tr> <tr> <td height="330"> </td> <? $nrpoza=$nrpoza-1; $poza=$sirpic[$nrpoza]; $proba=str_replace("\\\\","\\",$poza); $t_ht=600; $t_wd=seteazaDimensiuni($proba); echo "<td><img name=\"locpoza\" id=\"locpoza\" src=$sirpic[$nrpoza] height=$t_ht width=$t_wd alt=\"\"></td>"; closedir($opendir); ?> <td> </td> </tr> <tr> <td rowspan="2"> </td> <td><? echo $nrpoza." din ".$dim; ?> </td> <td rowspan="2"> </td> </tr> <tr> <td><a href="#">back to albume</a></td> </tr> </table> </form></body></html><?function seteazaDimensiuni($imagine){$imageinfo = getimagesize("$imagine");$t_ht=600;$o_wd=$imageinfo[0];$o_ht=$imageinfo[1];$t_wd=round($o_wd*$t_ht/$o_ht);return $t_wd; } ?> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now