Jump to content
php.lv forumi
  • 0

ar JS norādīt PHP masīva elementu


Question

Posted

Respektīvi ir PHP masīvs , tas tiek padots js skriptam. Nepieciešams ar javascript mainīgo norādīt php masīva elementa indeksu.

 

Lai saprastu, ko vēlos izdarīt mazs piemērs:

<?php 
$a[] = "lacis";
$a[] = "kakis"
$a[] = "suns";
?>
....
<script language="JavaScript">
var i = 0;
var length = <?php echo count($a); ?>;

function on() {
  if ( i < length) {
      document.forms[0].elements[0].value = <?php echo "'".$a[!!!i!!!]."'"; ?>;
      i = i + 1;
  } else {
     clearInterval();
  }
}
</script>
....

<a href="#" OnClick="javascript:setInterval('on()',1500);">Tests!</a>
...

 

Respektīvi !!!i!!! vietā kā pareizi padot JS mainīgo i?

 

Paldies.

3 answers to this question

Recommended Posts

  • 0
Posted

Visvienkāršākais būtu šo PHP masīvu izvadīt kā JS masīvu. Citādi - sanāk ka jātaisa, piemēram, AJAX pieprasījums uz PHP skriptu, kas tad atkarībā no caur GET/POST padotās vērtības atgrieztu attiecīgā masīva elementa vērtību.

  • 0
Posted

Ja pareizi sapratu

<?php 
$a = array('lacis','kakis','suns');
?>

<script type="text/javascript">
var i = 0;
var mainigie = ['<?php echo implode("','",$a); ?>'];
var l = mainigie.length;
var intervals;

function on() {
  if ( i < l) {
      document.forms[0].elements[0].value = mainigie[i++];
  } else {
     clearInterval(intervals);
  }
}
</script>
....

<a href="#" onclick="javascript:intervals = setInterval(on,1500);">Tests!</a>

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...