tags:

views:

1056

answers:

5

Hi friends, 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...

+8  A: 

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.

PintSizedCat
+2  A: 

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.

Chris Lutz
A: 

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.

Gary Willoughby
if i type like that it will not shown , thats y i type n that way to display
Sakthivel
@sakthiopr - you need to type it in as code, i.e. either with `backticks` or indent the code 4 spaces so that it displays as code.
Chris Lutz
+2  A: 

Hi,

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

Tudor Olariu
i typed the same as the above , But it displayed the error 'objected expected'...
Sakthivel
You should have a look at what customerpage.php does, then, as it may be outputting something that can't go into a Javascript function...
Ben
Good point, does that PHP script output text?
Gary Willoughby
I believe the PHP script does output javascript, since it's nested inside a javascript function. I think the problem is not with the php itself. Check the source code of your rendered page, and you will see the syntax error.
Tudor Olariu
A: 

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.

Bryson