0
<script>
    function myFunc(a){
        document.getElementById("col2").innerHTML=a; 
        var str=a;
        if(str.charAt(0)=="c"){
            document.getElementById("col2").innerHTML="credit_card";
            var x=document.getElementById("col2").innerHTML;
            document.getElementById("col2").innerHTML= "<?php echo cc_print_info("<script> document.write(x) </script>") ;?>  ";
        }   
        
    }
</script>

I need to pass the value of cc_print_info() into document.getElementById("col2").innerHTML

I tried in the way mentioned in the code but it isn't working

Let me know how to do this

  • Please can you explain more.. What exactly you wanna do?? and what is `cc_print_info()` – Vinayak Sep 30 '20 at 19:52
  • Do not mix client-side and server-side web scripting. If your PHP function cc_print_info is scripted as the PHP function, then you need to call it (and that functions returns the calculated result back). See https://stackoverflow.com/a/2379251/2103539 for more explanation. You would want to script the AJAX request to get the PHP function results AFTER the page was loaded in a web browser. – Matt Sergej Rinc Sep 30 '20 at 20:41
  • @Vinayak cc_print_info() actually is a PHP function which does some MySQL queries and returns the value to the Javascript myFunc() – Sadikul Alim Toki Oct 01 '20 at 05:15

1 Answers1

0

You should not use PHP inside JS script - it will not work but you can use PHP that generates JS script for example just variable assignment:

example code:

<!DOCTYPE html>

<html>

    <head>
    </head>

    <body>
        <?php

            function cc_print_info($a, $b) { return $a + $b; };

            echo '<script>' . PHP_EOL;
            echo "var phpReturn = " . cc_print_info(2, 3) . ';' . PHP_EOL;
            echo '</script>' . PHP_EOL;

        ?>

        <script>
            function myFunc(a, phpReturn){

                document.getElementById("col2").innerHTML=a; 
                var str=a;
                if(str.charAt(0)=="c"){
                    document.getElementById("col2").innerHTML="credit_card";
                    var x=document.getElementById("col2").innerHTML;
                    document.getElementById("col2").innerHTML= phpReturn;
                }   
                
            }
        </script>

    </body>

</html>

note that PHP must be before the script that uses returned value of cc_print_info.

Code above generates this HTML:

<!DOCTYPE html>

<html>

    <head>
    </head>

    <body>
        <script>
var phpReturn = 5;
</script>

        <script>
            function myFunc(a, phpReturn){

                document.getElementById("col2").innerHTML=a; 
                var str=a;
                if(str.charAt(0)=="c"){
                    document.getElementById("col2").innerHTML="credit_card";
                    var x=document.getElementById("col2").innerHTML;
                    document.getElementById("col2").innerHTML= phpReturn;
                }   
                
            }
        </script>

    </body>

</html>

so for a browser looks like no problem since you have

var phpReturn = 5;

you pass to your function:

function myFunc(a, phpReturn){

and use it:

document.getElementById("col2").innerHTML= phpReturn;
Jimmix
  • 5,644
  • 6
  • 44
  • 71
  • But the thing is I need to pass the value of 'a' in **myFunc()** into **cc_print_info()** as well. In that case, what should I do? @Jimmix – Sadikul Alim Toki Oct 01 '20 at 05:11
  • @SadikulAlimToki for that you need to make HTTP request to the PHP page like for eg you do with [form](https://www.w3schools.com/php/php_forms.asp) or make from JS [XHR Request](https://javascript.info/xmlhttprequest). Then no matter the method you have chosen PHP returns value of the cc_print_info() as a normal web text that JS is able to read as a content. PHP can read your `a` from JS by reading that JS request data from GET or POST [see tutorial](https://www.tutorialspoint.com/php/php_get_post.htm) – Jimmix Oct 01 '20 at 09:47
  • I know this isn't at all good to say but can you please build the code for me if you can as I am still unable to execute. I really need this at the moment. – Sadikul Alim Toki Oct 01 '20 at 16:41
  • @SadikulAlimToki I afraid I would not do that but I would suggest creating a new question that shows what you did with the information I provided and additionally a description of the problems you had when you tried to get it working. – Jimmix Oct 01 '20 at 17:56