1

i have a problem when trying to include php with javascript

i have an:

if(document.getElementById(acheckbox).checked==true){
<?php $result=$result."bar-";?>
}
else{
<?php $result=$result."none";?>
}

but both are executed... so $result is>>>bar-none how can i solve this problem??? please help!

1
  • 2
    You're mixing your client-side javascript with server-side PHP. The PHP will be executed first, then the page content returned to the user, then the Javascript happens. You would probably need to do something like an AJAX request from your javascript that calls the PHP that then determines what data to return to your client. Commented Jan 26, 2011 at 9:55

2 Answers 2

3

That JavaScript code, while it's in a source file on your server, is just plain text to output.

It's not code that's executed by the PHP interpreter.

Both pieces of PHP code will always run.

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

7 Comments

but what do i have to do to prevent this?
Rethink your logic. It's not clear how you should fix it since there is no context for that code -- what it was supposed to do, what result was supposed to be used for.
i have 3checkboxes...restaurant,bar,hotel...when i check any one or uncheck..i run a function which checks which checkboxes have been checked..from there i want to fetch data from my database through php and mysql...
Then you have two options: First, you can use PHP to echo JavaScript variables containing the data you might need for all 3 checkboxes. Then you use JavaScript code to decide which variable to use/display. Your other option is to make an AJAX request to a PHP script, providing it with which checkbox was checked as part of the URL or POST body, to get only the data you need to your JavaScript code.
okay let's say i use the first option....but i have to convert javascript variable values to php...if for example the user clickd on hotel and bar..the javascript variable <result> will contain bar;hotel...then i have to fetch in my database..how do i convert the javascript values into php so that i can fetch it from my database?
|
-2

your else statement needs to be written in php not in javascript.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.