3

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.

3 Answers 3

2

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:

var c = 1;

$('#foo').click(function() {
    $.ajax({
        url: 'something.php?c=' + c,
        success: function(name) {
            $('#bar').append('<div style="color:red">\n\
                Name:' + name + '\n\
            </div>');   
        }
    });

    c++;
});

Here, something.php will accept a single request parameter "c", and should output only:

<?= foo('name' . $_GET['c']) ?>
Sign up to request clarification or add additional context in comments.

2 Comments

This works although it seems like a lot of trouble to achieve a simple task. Is this really the only way?
@IMB, yes it really is. It does seem complicated for such a simple task, but it's the nature of the web. No way around it (unless you can somehow implement that foo() function using just javascript).
0

This is not possible, because javascript is executed on the client and can therefore not be mixed with php. What you could do is use ajax to achieve a server <-> client communication.

$.ajax({
  type: 'POST',
  url: "your_php_file.php", 
  data: {"counter": c},
  success: function (val) { 
      $('#bar').append('<div style="color:red">Name:' + val + '</div>');   
  }
});

So this way, you would call the function on the server side like foo($_POST['counter']).

Hope this helps.

Comments

0

Use concatenation opeartor +, and instead of

Name:<?=foo('name')?>\n\

write:

Name:<?=foo('name' + i)?>\n\

But still, this will not work as you want. as PHP is on server side and you need to make request to the server from your JavaScript. And since it's rather PHP who dengerates JS code then you need to rething your application logic.

EDIT

Instead of

$('#bar').append('<div style="color:red">\n\
                    Name:<?=foo('name')?>\n\
                </div>');

try

$('#bar').append('<div style="color:red">\n\
                    Name:<?=foo(\'name\'' + i + ')?>\n\
                </div>');

3 Comments

But it will not work that way. JavaScript cannot call PHP functions.
JS doesn't actually need to call PHP. All foo() does is return a string like name1 or name2 depends on the value of JS's c variable. Still not doable?
@IMB: "All foo() does is return a string like name1 or name2 depends on the value of JS's c variable" -- PHP happens on the server side and emits javascript before any javascript is actually run. By the time javascript is running, PHP is finished. There is no way to call php directly based on a javascript variable. The only way to do this is by talking to the server again from javascript (e.g. using AJAX). See my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.