1

I have a question about using javaScript value in PHP:

var i; <br>
for(i=0;i<4;i++){ <br>
    alert("echo $MarkA[i]");<br>
}

$MarkA is a PHP array
I want to know that how can I using the javascript value 'i' in PHP code.
Thanks.

4
  • alert("echo".$MarkA[i]);<br>` Commented Dec 23, 2016 at 4:19
  • can you please paste your above and below code, need to see how you want to use this... Commented Dec 23, 2016 at 4:56
  • can ajax solve your problem? you can pass javascript value into php using ajax. Commented Dec 23, 2016 at 5:21
  • var i; for(i=0;i<<?echo count($IDA)?>;i++){ alert(<?php echo $MarkA[i]?>); } Commented Dec 23, 2016 at 5:43

2 Answers 2

4

That's not how PHP works. You can't mix client and server-side scripts that way. One option is creating a JavaScript variable:

<script>
   var marks = <?php echo json_encode($MarkA);?>;
   for(i=0;i<4;i++) alert(marks[i]);
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

You cannot use client side script variable into server side script. But you can parse the PHP array using the below method. No need javascript increment variable.

<?php $MarkA = array(1,2,3,4);?>

<script>
var json = JSON.parse("<?php echo json_encode($MarkA);?>");

$.each(json,function (i,item) {
    console.log(item);
});
</script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.