2

I want to get a javascript function inside an html() The codes looks like

 var test = getParameterByName('page');
 $(".pageContainer").html("<?php echo check("+ test")['content']; ?>  ");

I have tried a lot of different combinations, none works. I am new with javascript.

Thanks in advance

Ahsan Khurshid
  • 9,383
  • 1
  • 33
  • 51

5 Answers5

0

You can't mix javascript and php like this. Javascript works in clint side while php works in server side.

You could use ajax to ask the server for the response.

Javascript side:

var test = getParameterByName('page');
$(".pageContainer").load('foo.php?key=' + encodeURIComponent(test));

PHP side:

<?php
// foo.php

// do something ....

echo check($_GET['key'])['content'];
xdazz
  • 158,678
  • 38
  • 247
  • 274
0

Supposing the <?php is not processed by the server before JavaScript can touch it, that should work: JavaScript will get the parameter and change the HTML of all elements of class pageContainer to

<?php echo check(X)['content']; ?>

where X is the result of getParameterByName. However, I can't think of a legitimate reason to put that in some HTML. More likely, you want to execute a PHP function, not a JavaScript function. If that's the case, it's not that simple; you'll have to create a new PHP page for the sole purpose of executing that function and use AJAX to retrieve it.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
0

Try this,

$(".pageContainer").html(check(test)['content']);

If you need javascript function inside an html() then your js check() should return an array like,

function check(test){
   var arr=[];
   // your code
   arr['content']='some value';
   return arr;
}

Live Demo

If check() is a php function then you need to use ajax() like,

$(function(){
    var test='your value';
    $.ajax({
       url:'function.php',
       dataType:'json',
       data:{action:'check',test:test},
       type:'POST',
       success:function(data){
          $(".pageContainer").html(data.content);
       }
    });
});

Function.php page

<?php
    $action=isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
    if($_REQUEST['action'] == 'check'){
       echo json_encode(array('content'=>$_REQUEST['test']));
       return;
    }
?>
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
0

The .html() function within jQuery will accept a function as an argument. See https://api.jquery.com/html/#html2 So, using your example:

$(".pageContainer").html( function() {
    //Get the html content you are looking for
    //Perhaps $.get() [https://api.jquery.com/jQuery.get/] is what you 
    //are looking for instead of using php inline here
});

Also, .getParameterByName() is not a built in function to javascript or jQuery. To replicate it, see this answer: How can I get query string values in JavaScript?

Community
  • 1
  • 1
NCharby
  • 1
  • 1
0

if you want get value from php function then try following

you can pass js variables as argument in php function code edited

$( document ).ready(function() {
var param=' value from js print in php';    
$(".pageContainer").html(" <?php echo te('"+param+"'); ?>");    

}); <?php
function te($param){
    echo $param;
}
?>

or if you want to get value from js itself then try this

$( document ).ready(function() {    
 $(".pageContainer").html( te());   

});
function te(){
    return "value from js";
}
Sarath
  • 608
  • 4
  • 12