0

I'm working on a profile page that auto-populates email field, would like to get text value and then use it as variable on PHP.

To get the text value, using jquery:

jQuery(document).ready(function(){
   var str = jQuery(".user_email").text();
});

Is there a way to echo variable "str"? Thanks in advance.

2
  • 1
    Possible duplicate of how to put javascript variable in php echo Commented Feb 24, 2016 at 16:47
  • 2
    If you want to use it on the same page, it's not possible. PHP loads on the server before it goes to the user, jQuery/javascript renders after in the browser. If you want to do something with it, you'll need to submit it to a PHP page. Commented Feb 24, 2016 at 16:48

2 Answers 2

0

You can't do that on the same page since php is executed on the server while javascript runs from the browser. However, where is the auto populate value coming from ?
If it is from a DB or an external API resouce (e.g in json or xml) then you can directly assign that to a php variable without passing it through javascript

Sign up to request clarification or add additional context in comments.

Comments

-1

log it to the browser javascript console

console.log('str', str);

how to display the console varys amongst browsers

EDIT ok... apparently this wasn't what the OP wanted at all.. probably wants to jQuery.ajax('/url', {str:str}); to send the value to the server.

My question: if these values are on the page.. where did they come from... did they not originate server-side/php ?

5 Comments

That is not what OP is asking.
@noob: he asked "how to echo str"
Definitely no on console. OP mentioned that he wishes to use it as variable on PHP, for that purpose he wants to test it out by using echo. On a side note: I didn't down voted your answer.
OK.. is it kosher to remove an answer?
Yes, it is if your answer is not contributing anything to the solution. Besides when a question is marked as duplicate a solution is probably present so there is no point in giving out same 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.