1

I want to concatenate a value inside function in jquery. See code below. I want to put value data 1 into a function. Do you have any way to achieve what I want? Appreciate.

     var data='1';
 var user='<?php echo bp_core_get_user_domain('+ data +');?>';
    
         $("#find_members").html(user);
conan
  • 1,327
  • 1
  • 12
  • 27
  • PHP isn't available on a page once it has loaded. It all process on the server and then the generated code is sent to the client. You'll need to send out an ajax request. – chris85 Feb 23 '15 at 04:46
  • 1
    Ok. And what if I need aJax request with php code for eval()... )) – winston86 Feb 23 '15 at 04:48
  • What is `eval()... ))`? Also, is Winston == conan? The ajax sends data from the client to a server side language then awaits the response that the server sends with the data requested. So you'd have this `` on a PHP page that this page would send the request to. – chris85 Feb 23 '15 at 04:55
  • he is not me. I believe Winston has the same problem like me. – conan Feb 23 '15 at 04:59
  • Okay, here's another posting about how this can be done. http://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php – chris85 Feb 23 '15 at 05:02
  • chris85, what do you suggest is that I should put in my search.php. I confuse. – conan Feb 23 '15 at 05:03
  • you could achieve this using a hidden field mate and is this `data` variable always set to 1..?? – Nibin Feb 23 '15 at 05:07
  • I was wondering can I use ajax to call the php function bp_core_get_user_domain( ). Is that possible? – conan Feb 23 '15 at 05:09

1 Answers1

0

Make an empty php page.

file.php

Inside put the PHP you want to interact with the JS value.

<?php 
function bp_core_get_user_domain($something) {
     //do Stuff here
      return $something;
}
echo bp_core_get_user_domain($_POST['data_var']);
?>

Then on your page make an ajax request to this script and the data will come back...

var data='1';
$.post( "file.php", { data_var: data})
  .done(function( data ) {
    $("#find_members").html(data);
  });

This is a rough version (untested), there are MANY examples out there for this.

chris85
  • 23,846
  • 7
  • 34
  • 51
  • Thank you for your time. The big issue here bp_core_get_user_domain() cannot be called in ajax. I don't know why. maybe it is buddypress function. firebug said NetworkError: 500 Internal Server Error. The ajax has no problem if I put plaintext. – conan Feb 23 '15 at 05:30
  • 1
    `bp_core_get_user_domain` is obviously a custom function which you will need to `include` inside the php code. – EternalHour Feb 23 '15 at 05:40
  • Can you guide me how to include it? it is a buddypress function, so I have to find the file inside buddypress and include it in my php, or do I have other way to include it? – conan Feb 23 '15 at 05:49
  • Oh yeah, @EternalHour good point missed that one. @conan somewhere you have `function bp_core_get_user_domain($something) {...` you need to bring that over to the `file.php`. If that is defined in a config file you could just do `include 'config.php';`. – chris85 Feb 23 '15 at 13:16
  • I have create a function to test. It doesn't work when I include any file in file.php. Do I have to do something? I think that is why bp_core_get_user_domain cannot be call in file.php, but can call in other php file. – conan Feb 23 '15 at 14:37
  • Can you post your whole file or atleast the function and I can show you how to put it into 'file.php'? – chris85 Feb 23 '15 at 23:53
  • I've made a sample function you can fill it in with what actually goes there. – chris85 Feb 24 '15 at 04:05