Solved

calling a PHP function throu JS's - 'onClick' ?

Posted on 2004-08-17
11
3,704 Views
Last Modified: 2013-11-19
hello.
would like to ask if it is posibale to call a PHP function throu JS's 'onClick'.
I've tryied soemthing like that:
onClick="<? roll(); ?>;"     which i don't think is working..
would be happy to hear comments.
thanx. ori.
0
Comment
Question by:orik7
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 13

Expert Comment

by:StormyWaters
ID: 11823159
PHP is serverside; everything is parsed before the user ever sees the document. Javascript, on the other hand, is interpretted by the browser. The two languages are unrelated, so it's not possible to call a PHP function through a clientside feature. Some PHP functions can be implemented by javascript. What is it that you intend to accomplish?
0
 

Author Comment

by:orik7
ID: 11823369
something like that:
--------------
<html>
<title></title>
<?php
function roll(){
$link1="</td></tr><tr><td>sub1</td></tr><tr><td>sub2";
}
?>
</head>
<body>
<table width="130" border="1">
<tr>
<td onClick="<? roll(); ?>;">
link1
<? echo $link1; ?>
</td>
</tr>
</table>
</body>
</html>
------------------------------------
thanx.
0
 
LVL 8

Expert Comment

by:GEM100
ID: 11823407
No, this is not the best approach. You can use <div></div> and use Javascript to add data to DIV layer on click, by using  divid.InnerHTML
0
 

Author Comment

by:orik7
ID: 11823495
i am right now using divs on www.oris.ws over the menu at the left side. i heard it not working on some browsers so thought to an alternate..
0
 
LVL 1

Accepted Solution

by:
C7Swill earned 125 total points
ID: 11824517
Hey Orik,

The best way that I can think of would be to use hidden rows.

<table>
 <tr id="Section1" style="display:none;">
   <td>
     Content
    </td>
  </tr>
  <tr id="Section2" style="display:none;">
    <td>
      Content
    </td>
   </tr>
</table>

Then for you buttons in the onclick call a javascript function that shows or hides rows.

Use this function:

function showHideRow(el, showhide) {
  if (document.all){
        var whichRow = document.all[el];
        if(showhide == "show" ) {
            whichRow.style.display = "";
            } else {
            whichRow.style.display = "none";
              }
  } else if (document.getElementById){
        var whichRow = document.getElementById(el);
        if(showhide == "show" ) {
            whichRow.style.display = "table-row";
        } else {
            whichRow.style.display = "none";
        }
  }
}

So:

onClick="showHideRow('Section1', 'show'); return false;"

will show hidden section1.

onClick="showHideRow('Section1', ''); return false;"

will hide section1.

Hope this makes things easier.

--Sean
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:orik7
ID: 11824755
Sean, thanx alot for the efforts. but will this code work on browsers like 'mozila' 'NS' and others?
ori.
0
 

Author Comment

by:orik7
ID: 11824780
Sean, i'm asking because as i said i have something that is working but there's the browsers issus that are not solved- so if i'm replacing it it's better to have a good start (to have a code that will be more fitbale to other browser than IE).
thanx. ori.
0
 

Author Comment

by:orik7
ID: 11824789
and increasing points a bit..
0
 
LVL 1

Expert Comment

by:C7Swill
ID: 11825028
Hey Orik, this will work on mac on ie, safari, mozilla firefox...
and pc: ie, mozilla firefox, netscape all latest versions.

That's all I currently have on my system but the only thing limiting cross-browser would be the javascript function
showHideRow.

 if (document.all){
and
  } else if (document.getElementById){

check for two different document object types so logically you could add as many as you want.

function showHideRow(el, showhide) {
// add opera compat
   if (navigator.userAgent.indexOf("Opera") != -1) { //Opera
     whichRow = eval('document.all.' + el);
       if(showhide == "show" ) {
          whichRow.style.display = "table-row";
       } else {
          whichRow.style.display = "none";
       }
   }  else if (document.all){
       var whichRow = document.all[el];
       if(showhide == "show" ) {
          whichRow.style.display = "";
            } else {
          whichRow.style.display = "none";
              }
  } else if (document.getElementById){
       var whichRow = document.getElementById(el);
       if(showhide == "show" ) {
          whichRow.style.display = "table-row";
       } else {
          whichRow.style.display = "none";
       }
  }
}

This function adds in opera capability.

--Sean
0
 
LVL 8

Expert Comment

by:GEM100
ID: 11825045
You can't do what you need with PHP, the concept is wrong in itself. If browser doesn't support DOM, then you can't do it. Mozilla supports DOM, old versions don't, Netscape/Mozilla started supporting it much later than IE.
0
 
LVL 1

Expert Comment

by:Robertonio
ID: 11999258
Could you explain better what you need this code to do? so far, I see Sean's solution as the best and I dont think you'll find a lot of browsers that dont support the mentioned solution. I am pretty sure the vast majority of people that visit your site use either MSIE or the latest Mozilla.

Also, i think you need to understand a bit more of PHP and JS. Like it has been mentioned before, PHP works serverside and JS works clientside, which means that PHP functions work while the document is still being procesed at the web server and JS is processed when the document reaches the browser at the client machine which means you CANT do the onClick call of a PHP function UNLESS you actually referred to another URL.

All is not lost, though. You CAN do some things like submiting to a PHP document using java or you can also navigate to such document using a popup window (if you wish your main window to stay on the same document).

Just a couple of suggestions.. elaborating on your question would help us come up with a solution too.

Hope this helps.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error viewing ASP page 12 150
responsive divs setup - what am I doing wrong 2 67
remove border from element 5 55
Standard for Web Development 3 27
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now