-1

I want to output a value in PHP, something like the example below. How can I achieve this?

var number='1';
var $user_link = "<?php echo bp_core_get_user_domain('" + number + "') ?>";
Nisse Engström
4,77323 gold badges29 silver badges44 bronze badges
asked Feb 25, 2015 at 22:01
2
  • 3
    It is not possible in way you are trying to do, because the back-end part (php) renders before client side (javascript) Commented Feb 25, 2015 at 22:04
  • Also doesn't make sense that you can hard code a value for number but need to use it as a variable on next line. Maybe you need to use a php variable instead? If you already have it as php variable, use it within php function Commented Feb 25, 2015 at 22:13

1 Answer 1

1

There are a couple-three ways of going about this:

(1) Use an event driven Ajax call that passes the value of the variable number to a PHP script, returning JSON, which is then embedded into the page using JavaScript and the DOM.

(2)Submit the page to the server, passing the value of the variable number as a parameter in a GET query string or a hidden input in a POST.

(3)Take the functionality of bp_core_get_user_domain and put it into a JS function, and forget PHP altogether, unless you're using a PHP global variable to do something. In which case you can just put that data into a cookie on the original page request and let JavaScript read it without having to resubmit the page.

answered Feb 25, 2015 at 22:16
Sign up to request clarification or add additional context in comments.

6 Comments

and #4, if value is already known at server can run function by using it as php variable
lol..not always, look how the 2 language are getting mangled here
Yeah well, every rocket scientist had to learn simple addition at some point.
I wasn't trying to be condescending, the reality however is sometimes the obvious needs to be pointed out when people run into these sorts of issues
If you need PHP data, I generally go with option 1. It's the cleanest solution and keeps things nicely separated, so you're not blurring the lines between PHP and JS. Or do option 3, if you don't need PHP.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.