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.

Jose Salazar
  • 341
  • 1
  • 4
  • 12
  • 1
    Possible duplicate of [how to put javascript variable in php echo](http://stackoverflow.com/questions/10853630/how-to-put-javascript-variable-in-php-echo) – Abdulla Nilam Feb 24 '16 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. – aynber Feb 24 '16 at 16:48

2 Answers2

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

Kudehinbu Oluwaponle
  • 1,045
  • 11
  • 11
-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 ?

Brad Kent
  • 4,982
  • 3
  • 22
  • 26
  • That is not what OP is asking. –  Feb 24 '16 at 16:46
  • @noob: he asked "how to echo str" – Brad Kent Feb 24 '16 at 16:50
  • 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.** –  Feb 24 '16 at 16:54
  • OK.. is it kosher to remove an answer? – Brad Kent Feb 24 '16 at 16:55
  • 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. –  Feb 25 '16 at 01:55