Jump to content

How can i call javascript function from php code?? (Kindly Help)


radiations3

Recommended Posts

What actually i am trying to do is to check whether any body logged in or not and if someone is logged in then in it shows the divs with id = 2 else it shows the divs with id = 4

 

i am using showing and hiding of divs coding via javascripting but the functions are not being called from php dont know why and how :( kindly help...

Link to comment
Share on other sites

Show us.

 

 

 

<script language="javascript">

 

function jsfunction ()

 

{

 

$("2").show();

 

$("1").hide();

 

alert("I am an alert box!");

 

}

 

 

 

function jsfunc()

 

{

 

$("2").hide();

 

$("1").show();

 

alert("I am an alert box!");

 

}

 

 

 

hijsfunction();</script>

 

 

 

 

 

 

 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head>

 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

 

<title>Untitled Document</title>

 

</head>

 

 

 

<body>

 

<p> </p>

 

<div id="2" > hello </div>

 

<div id="1" > Fellow </div>

 

</body>

 

</html>

 

Link to comment
Share on other sites

My complete code the old code was copied from view source section of chrome

(this code is also checked by calling javascripting function via echo "<SCRIPT LANGUAGE='javascript'>jsfunction();</SCRIPT>";)

 

<script language="javascript">

function jsfunction ()
{
$("2").show();
$("1").hide(); 
alert("I am an alert box!");
}

function jsfunc()
{
$("2").hide();
$("1").show(); 
alert("I am an alert box!");
}
</script>
<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

if (isset($_SESSION['MM_Username']))
{
echo "hi";
print("jsfunction();");
}

else
{

print("jsfunc();");

   echo "cool";
}

?>






<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<p> </p>
<div id="2" > hello </div>
<div id="1" > Fellow </div>
</body>
</html>

Link to comment
Share on other sites

That is not the html source, or if it is, it has php in it.

 

Looking at that code I can see that your not calling the javascript functions within <script></script> tags.

 

You do understand that PHP is processed server side long before your markup is displayed (and javascript executed) client side.

 

Why don't you just wrap your divs in an if statement and not produce them if you don't need them? Your approach really makes little sense.

Link to comment
Share on other sites

This code worked

<script type='text/javascript'> 
function mySuperAwesomeFunction(){ 
alert('Yay, this works!'); 
} 

<?php 
print("mySuperAwesomeFunction();"); 
?> 
</script> 

 

but when i use same code in mine nothing happend all i want to do is the following

 

<script language="javascript">

function jsfunction ()
{
$("2").show();
$("1").hide(); 
alert("I am an alert box!");
}

function jsfunc()
{
$("2").hide();
$("1").show(); 
alert("I am an alert box!");
}

<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

if (isset($_SESSION['MM_Username']))
{
echo "hi";
//call javascripting function here
}

else
{

print("jsfunc();"); //call javascripting function here

   echo "cool";
}

?>

</script>

Link to comment
Share on other sites

You really need to read my previous reply again. Your entire logic is floored. Why are you using JavaScript to hide the divs when you can simply not send them to the client using php. I meen really, this logic is terrible.

Link to comment
Share on other sites

hmmmmm well your approach really worked but kindly let me know of the code if i can use my php code to call any function from javascript

 

like

if ($x=1)

echo "<SCRIPT LANGUAGE='javascript'>divclose();</SCRIPT>";

i just want to know that's all cause i have wasted my time alot for this .....

Link to comment
Share on other sites

You have already posted an example that you said did work.

 

true but it doesnot when i put it in my code like (every  single bit is same as the code i said was working but dont know why this one doesnot work)

 

 

<script type='text/javascript'>

function mySuperAwesomeFunction(){ 
alert('Yay, this works!'); 
} 



<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

if (isset($_SESSION['MM_Username']))
{
echo "hi";
print('mySuperAwesomeFunction();');
}
else
{


echo "<SCRIPT LANGUAGE='text/javascript'>jsfunc();</SCRIPT>";
   echo "cool";
}

?>

</script> 

Link to comment
Share on other sites

So, a few posts ago I asked you to show us the source. You still haven't.

 

If you ask for help you should read the replies.

 

Also, not that you cannot call session_start after sending out. Turn error_reporting on and you will see your php code produces errors.

Link to comment
Share on other sites

So, a few posts ago I asked you to show us the source. You still haven't.

 

If you ask for help you should read the replies.

 

Also, not that you cannot call session_start after sending out. Turn error_reporting on and you will see your php code produces errors.

 

Well sir i read all of your replies and respond accordingly i may missunderstood something so sorry for that

you have really helped me alot and gave me an alternative

i have tried error_reporting(); but it didnot report any

i'll try to google to get the solution of how to do javascripting in echo...

 

Thanx alot!!

 

Link to comment
Share on other sites

There are a couple of problems with the last bit of code you posted.

 

First, you really should close your <script> tags after the actual javascript and not the entire page.

 

Second, when calling the javascript function from a PHP print command, use the full javascript syntax:

 

echo "<SCRIPT type='text/javascript'>jsfunc();</SCRIPT>";

 

Third, notice that I changed your code from SCRIPT LANGUAGE='text/javascript' to SCRIPT type='text/javascript'.

 

Fourth, you weren't calling the same function as the one you defined. You were still using mysuperawesomefunction but the code in the script tags were calling jsfunc(). Here is your code with the modifications made, and it works on my system:

 

<script type='text/javascript'>

function jsfunc(){ 
alert('Yay, this works!'); 
} 
</script> 


<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

if (isset($_SESSION['MM_Username']))
{
echo "hi";
echo "<SCRIPT type='text/javascript'>jsfunc();</SCRIPT>";
}
else
{


echo "<SCRIPT type='text/javascript'>jsfunc();</SCRIPT>";
   echo "cool";
}

?>

Link to comment
Share on other sites

ok i get it know one last question

kindly let me know what is wrong with the following code : (why isn't it hiding the div with id=2)

 

<script type='text/javascript'>

function toggle(id) {

document.ElementById("id").style.visibility = "hidden";  

} 

</script> 

<body>
<SCRIPT type='text/javascript'>toggle(2);</SCRIPT>
<p> </p>
<div id="2" > hello </div>
<div id="abc" > Fellow </div>
</body>

Link to comment
Share on other sites

The most important problem you have there is that the javascript function is called getElementById, not ElementyById, though this doesn't solve everything. In troubleshooting javascript, you need to mess around with things like code placement (where in the script your code is), single quotes vs. double quotes vs. no quotes at all, etc. After playing around with this stuff, I got this code to work. Note the placement of the script tags and quotes:

 

<script type='text/javascript'>

function toggle(id) {

document.getElementById(id).style.visibility = "hidden";

} 

</script> 

<body>

<p> </p>
<div id="2" > hello </div>
<div id="abc" > Fellow </div>
<SCRIPT type='text/javascript'>toggle(2);</SCRIPT>
</body>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.