3

I was trying to embed some PHP into a javscript function that gets called from an onChange in a drop down list. the javascript function is in a .js file, I can get the DDL to call the function, however i cannot get the function to work with the php... eventually i am going to use the value selected in the DDL to access a DB and populate other fields from there, right now i am trying to get it to work with the php:

    function controllerType(){

       alert('outside php');
       <?php
       $message = "inside php";
       echo "alert('$message');";
       ?>

     }

The function prints the first alert but not the alert called in the php.

1
  • When you examine the JavaScript, do you see the PHP "as is" ? Commented May 9, 2011 at 3:57

6 Answers 6

4

Your webserver probably isn't looking for PHP code in .js files by default. You'll have to tell it to either look for PHP in those files or change the file extension to .php.

If you you want to try the former and you're running an Apache web server, try adding the following line to your .htaccess file:

AddHandler application/x-httpd-php .js

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

2 Comments

is there a way to make an onChange call a PHP function in a php file?
@user744480 That's a complicated topic and should be posted in a separate question.
1

Try the following

function controllerType() {
    alert('outside php');
    alert('<?php echo $message; ?>');
}

4 Comments

There shouldn't be a difference from JavaScript's point of view.
I wouldn't think so either :-\
Now you're create PHP code with javascript, I think OP's idea was to create JS code with PHP :)
What are you talking about? You can't create PHP code with javascript, at least definietly not this way -- one would need an asycn/syncrhonous request to do that.... This is simply putting a PHP variable into his javascript code... So yes, I am creating JS code with PHP.. i dont know where you see it otherwise..
0

You can't call an alert() in PHP.

PHP can just echo text the JavaScript interpreter can interpret as JavaScript.

As for the code you posted, that should all be ran by JavaScript fine. CodePad.

Comments

0

I think the problem might be that your javascript is not being evaluated by your PHP parser. You might have to create a separate document for that function which ends in .php or using a .htaccess (assuming you're using apache) to tell your php handler to parse .js files

Comments

0

From your following sentence:

eventually i am going to use the value selected in the DDL to access a DB and populate other fields from there

It really sounds like you are trying to have the PHP code execute at runtime within the browser. PHP is a server side language and you cannot simply use it in a javascript file as you are trying to.

You will need to have the javascript function post the value from the drop down list to a PHP page through a HTML form or through AJAX.

Forgive me if I misunderstood you.

2 Comments

It sounds like you understood what i was trying to get at, i basically need the page to reload displaying information retrieved from the database based on the value selected... so you are suggesting i create a javascript that sends the selected value to a new php page?
That is correct. You could do it simply by using window.location.href = "page.php?item=" + ddl_value; That would redirect the user to php page. The other way to do it without reloading the page is to use AJAX, have a look at the JQuery javascript library, it makes dealing with AJAX a lot easier.
0
<?php
   $message = "inside php";
?>
<script type="text/javascript">
function controllerType(){
       alert('outside php');
       alert(<?=$message;?>);
     }
</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.