1

exactly as title says, I need to put php inside of the javascript that is echoed by php

ex:

<?
echo ('<script language="javascript" type="text/javascript">
if (something) then (some php)')
?>

thats not real code just might help get the idea and please note the best way to do what im trying is this way, if its possible.

4
  • 1
    I you say what you want to do, then people will tell how to do that unlike how you are suggesting . Commented Mar 16, 2012 at 10:07
  • I just updated my answer with an example. Commented Mar 16, 2012 at 10:17
  • Whoa. Codeception. Though, that is acceptable syntax, it just might not work the way you want it to. It will generate the PHP first and run the js after. So, you might want something like if(something == $php_variable) then ($php_variable_that_tells_js_to_do_something). The javascript will NOT run the php code, all php code will run before the JS. Commented Mar 16, 2012 at 10:20
  • I have no idea what this is trying to accomplish. Commented Jul 28, 2024 at 13:31

5 Answers 5

5

You can't do that, PHP is a server-side language, that means it renders when the page loads and not after that.

The solution can be to call a PHP via AJAX, that PHP can have the case conditions and then it will render what you want.

Example:

The javascript (using jQuery):

$(".yourbutton").click(function(event){
        event.preventDefault();

        $.post("yourPHP.php", {var: somethingdynamicpassedviajavascript},
            function(data){
                //get ther result
                 $("#yourdiv").html(data);
            }, "html");
    });

What this does is place a click event into something with a class named "yourbutton", and when you click that, it will call an external PHP via an AJAX post, sending a var (in this example), you can send something dynamic, change the "somethingdiynamicpassedviajavascript" with some var.

PHP (yourPHP.php):

$myvar = $_REQUEST['var'];

//do your cases here:

switch ($myvar) {
    case "1":
        echo "this is for the case 0";
        break;
    case 1:
        echo "this is for the case 1";
        break;
}

Here you get that var, and depending on the case, send a different output. Notice that this may need to add a test for POST and other anti-vandalism methods...

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

3 Comments

I don't know if I get the question right, but it would be possible to construct a script based on computations done in PHP, right?
well the issue is that i have server code running and in the middle i would like to get some user input based on how things go and then i want to continue with the ode the server was working on based on that the user put in.
try googling the words {ajax jquery and PHP}, might be useful for you
0

yes you can do that.. your php scirpt generates/echoe the javascript code in your html page. You just need to play with single and double quotes and escape them properly In large scripts this is quite messy - better to put your js code in a seperate js file

Comments

0

if you're trying to dynamically create a javascript based on some conditions you're looking for something link this:

<script language="javascript" type="text/javascript">
<?
    if ($something == $somethingelse)
    {
        echo 'var something = 10;';
    }
    else
    {
        echo 'var somethingelse = 25;';
    }
?>
</script>

if you're to execute php-code via javascript ... that can't really be done, at best you can use PHPjs to emulate php-functions.

Comments

-1

Output the <script> tag like this:

<?php
function run_javascript($script_code){
    print "
    <script language='javascript'>
    <!--
      $script_code
    // -->
    </script>
    ";
}

run_javascript(" alert('I´m javascript here!'); ");
?>

1 Comment

To learn more about this community and how we can help you, please start with the StackOverflow Tour and read: How To: Ask and its linked resources. Please explain your answer and also format your code.
-3

u may try this

<?php
echo ('<script language="javascript" type="text/javascript">
if (something) then (some php)')
echo ('</script>');
?>

Comments

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.