Reputation: 1941
in my project I have the situation like this.
<script language="JavaScript">
function fonsubmit()
{
alert('Out side PHP');
//Here the php code starts
include("customerpage.php");
$cc="us";
$clang="en";
$cpath="us";
some coding goes here......
Php ends here
}
</script>
But it shows Object expected error. What is wrong in this? But I give only PHP tag it won't generate any error. Please help me to find out this issue, thanks in advance...
Upvotes: -1
Views: 9889
Reputation:
An alternative would be to display the Javascript within PHP. Instead of putting the PHP in the Javascript, you'd put the Javascript in the PHP. For example, you could do this:
<?php
echo '<script language="JavaScript">
function fonsubmit()
{
alert("outside php");
';
// here is where you insert your PHP code
include("customerpage.php");
$cc="us";
$clang="en";
$cpath="us";
echo '
}
</script>';
?>
Granted, echoing the Javascript logic doesn't read well. What you could also do is specify the start of the Javascript by echoing it,
<?php
echo '<script language="JavaScript">';
?>
and then inserting your desired Javascript normally. Whenever you want to insert PHP code, surround it with the <?php ?> tags.
Upvotes: 1
Reputation: 8047
You're trying to embed a server based language in a client-side based language. This will not work.
If you could explain a bit more what it is you're trying to achieve there might be a better solution, like AJAXie type thing.
Let me explain a bit more as others have given answers that could be put here.
You can have php
embedded inside your *.php files which will be interpretted on the server and parsed there. Using this you can set variables which javascript could pick up on later and use on the client-side. Like I said though the client side doesn't have a route for intpretting and parsing php
and so sending php
across to the user is not going to work.
If you are however trying to get things working so that your php sets some javascript values then I would, like Gary Willough and Tudor Olariu both say, add the php tags
<?php
... PHP CODE...
?>
However, you have to realise this will be interpretted on the server and not on the client. Therefore the following code would alert with "outside php" and then "inside php".
<script language="JavaScript">
function fonsubmit()
{
alert('outside php');
<?php
$message = "inside php";
echo "alert('$message');";
?>
}
</script>
The html sent to the user would then be
<script language="JavaScript">
function fonsubmit()
{
alert('outside php');
alert('inside php');
}
</script>
Hope this helps.
Upvotes: 12
Reputation: 1328
You are missing the php start and tags:
function fonsubmit()
{
alert('Out side PHP');
//Here the php code starts
<?php
include("customerpage.php");
$cc="us";
$clang="en";
$cpath="us";
//Php ends here
?>
}
Please be aware that your php code will be executed on the server, before your javascript kicks in
Upvotes: 2
Reputation: 52498
PHP should be enclosed in the proper php tags:
<?php include("customerpage.php"); $cc="us"; $clang="en"; $cpath="us"; ?>
Make sure your page has the *.php extension and run on a server which has PHP installed.
Upvotes: 0
Reputation: 75389
I don't know JavaScript, but JS is a client-side language, meaning it is evaluated in the web-browser as the user of your webpage views it. PHP is a server-side language, meaning that it is evaluated at the computer that hosts your webpage. Your browser can't evaluate PHP code. All PHP must be done at the server. You have to evaluate the PHP unconditionally, otherwise the PHP can't be evaluated at all. Then use JavaScript to pick what part of the PHP output you want to display - this will achieve the effect I think you want.
Upvotes: 2