开发者

Call PHP function from an HTML table which displays a PHP Table

I have a PHP p开发者_Python百科age, which builds up a table in the php code, and then previews the table in the HTML code, like this basically:

  <?php
     $display="<table><tr><td></td></tr></table>"
  ?>

  <HTML>
  <?php echo $display; ?>
  </HTML>

Now, I need to call a function INSIDE the php script again, whenever a button is clicked. Thing is, this button is created with the php before displaying it, like this:

  <?php
  function phpFunction(){ echo "Hello World"; }

  $display="<table><tr><td><input type='button' onClick='phpFunction();'</td></tr></table>"

   ?>

  <HTML>
  <?php echo $display; ?>
  </HTML>

But this doesn't seem to work... Is it possible to do what I want to do here?

If so, what's wrong in my code?

Thanks


Ive looked at this before and because PHP is processed on the server side nothing can be executed once the page is loaded(calling function etc) ,

The best alternative as far as i know would to write some javascript that can do this as javascript is processed/ executed by the browser on the client side so can be run after the page has loaded.

Im sure someone else here will be able to give you a code sample of how to do it because im not too sure


PHP is a server side script. this means it is executed on the server and the resulting HTML is sent to the client. You probably want to use something like javascript to do what you want, as it runs on the client computer.


You're mixing up client-side and server-side code. You can't put a server-based PHP function call as the client-side onclick handler for a button on the page.

You could implement an onclick handler that refreshes your page's content with a call back to the server. You can either reload the entire page, or use an AJAX call to selectively replace the table in-place (the implementation of which is beyond the scope of this reply).


Three approaches I can think of:

  • The server-side approach: when you click the button, the entire page is refreshed with new state being passed to the server-side script, which then regenerates every page. Not terribly difficult, but not a great user experience because of all the refreshes, and it can be cumbersome to properly encode the current state into the request and/or keep it cached server side in session data.
  • The hybrid approach: create several PHP scripts, or one PHP script that provides different functionality based on query string options. One of the options could be a signal to execute a particular method on the PHP script. Then you embed JavaScript in the page to process the button click using an AJAX request to the PHP script, and to update the page accordingly when the reply comes in.
  • The client-side approach: unless the page needs data that are only available as server-side resources that aren't exposed by a web service or direct file access via HTTP, just forget the PHP script and do it all using JavaScript on the client side.


PHP is a server-side script, which means that the PHP code is interpreted and executed fully before the final HTML page is sent to the client. What you want is client-side scripting, which is executed in the client's browser. This can be accomplished with Javascript, like so:

<html>

<script language="javascript">
function myfunc(element) {
  el = document.getElementById(element);
  el.innerHTML = el.innerHTML + "hello world!";
}
</script>

<body>
<table><tr><td id="thetd"><input type="button" onclick="javascript:myfunc('thetd')"></td></tr></table>
</body>

</html>
<!-- probably not the best way to do it, but it should work. You get the idea. -->
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜