Jump to content

change src value when a button is pressed


katia

Recommended Posts

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

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

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

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.

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

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

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

That's right.

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

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.

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

One reason might be because you left the comma out from the first part.

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

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   :)

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

<!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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...