In the code below, how do you concatenate the variable c to the string name so that the PHP function foo() will actually read foo('name1') since the value of c in this case is 1?
$(function(){
var c = 1;
$('#foo').click(function() {
$('#bar').append('<div style="color:red">\n\
Name:<?=foo('name')?>\n\
</div>');
c++;
});
});
Edit:
All foo() does is return the string (e.g, name1 or name2) depending on c‘s value.
What I actually need is just something like this <?=foo('name' + c)?> however that doesn’t work.
PHP happens on the server side and javascript happens on the client side so you can’t actually do this. If you want to use the results of a php method called with a value updated in javascript, you have to use ajax. Something like this:
Here,
something.phpwill accept a single request parameter “c”, and should output only: