How to use javascript in PHP class functions?

As you can see, I have a class whose name is a table; and I have many functions on it, one of them is "mainface"; these functions are similar to my interface, it has a checkbox and a combo box; I want to write JavaScript on the "All" -> onclick=" " checkbox.
I want that when the user clicks the "All" button and the option disappears on the screen, but I can’t find a way to do this.
Is there any way to do this? Can I add a JavaScript function to onclick?

 <script type="text/javascript" > function hide(){ if(document.form.All.checked){ document.getElementById('option').style.visibility="hidden"; } } </script> </head> <body> <?php class table{ public function __construct() { echo table:: mainface(); } public function mainface() { $arayuz=" "; $arayuz.="<form name=form action=index.php method=post>"; $arayuz.="<fieldset>"; $arayuz.="<input type=checkbox name=All value=All onclick='SOME JAVA SCRIPT FUNCTION'>; } ?> 
+4
source share
4 answers

php file:

 public function mainface() { $output = "<script type=\"text/javascript\" src=\"/path_to_your_js/table.js\">-</script>"; $output.= "<form name =\"form\" action=\"index.php\" method=\"POST\">"; $output.= "<fieldset>"; $output.= "<input type=\"checkbox\" name=\"ALL\" value=\"ALL\" onclick=\"checkAll(this)\">"; } 

And then in table.js:

 function checkAll( myCheckBox ) { //do some stuff you need here myCheckBox.style.display = "none"; } 

I hope this helps ...

+3
source

You mix the languages ​​on the server side and on the client side script. PHP runs on the server side (on the web server), javascript launches the client part (in the web browser). Onclick is javascript, so it works on the client side. You cannot call PHP functions directly from the Onclick action.

http://en.wikipedia.org/wiki/Client-side_scripting

http://en.wikipedia.org/wiki/Server-side

To achieve what you want, you do not need PHP scripting. All of this can be done in Javascript. You need to add an identifier to the form:

 $arayuz.="<form name='form' id='myform' action='index.php' method='post'>"; 

Then, as an onclick action, use the following:

 $arayuz.="<input type=checkbox name=All value=All onclick='document.getElementById(\"myform\").style.visibility = \"hidden\";'>" 
+1
source

This will work, but listing the inline JavaScript seems to be disgusting, I would recommend that you look into unobtrusive JavaScript and use something like jQuery. A jquery example that will work for all checkboxes:

$(":checkbox").live('click' , function () { $("#IdOfForm").hide(); });

0
source

Id form used onclick

  $arayuz.="<form name='form' id='form1' action='index.php' method='post'>"; 

it will be your onclick

  $arayuz.="<input type=checkbox name=All value=All onclick='document.getElementById(\"form1\").style.visibility = \"hidden\";'>" 
0
source

All Articles