Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
4.43/5 (3 votes)
See more:
Hi i have the following coding.

HTML
<input type="button" id="sample" value="click" onclick="sam_click()"/>


JavaScript
<script type="text/javascript">
function sam_click(clicked)
{
	var x="<?php ex(); ?>";
	alert(x);
	return false;
}
</script>


PHP
<?php
function ex()
{
$con=mysql_connect("localhost","root","");
mysql_select_db("my_db");
$res=mysql_query("call PrcEmployeeDisplay(777)");
$num=mysql_num_rows($res);
echo($num);
}
?>


Here when button is clicked it will call javascript function and that function will call php function. This works fine. i. e. Database is connected and retrieved the result and dispalying the number of rows in alert box. But i need to display the selected results in html table how to do this? pls help me.
Posted
Updated 30-Aug-18 8:26am
Comments
Sergey Alexandrovich Kryukov 16-Nov-12 0:06am    
Whatever you do, you are not calling PHP function, which can exist only on server side, from Javascript code executing on client side...
--SA
Red Chocolate 16-Nov-12 0:46am    
Thats ok but how to display my results in table?
skydger 16-Nov-12 1:46am    
You should create html template to fill it while fetching query data. Please refer php documentation about it. http://php.net/manual/en/book.mssql.php
Sergey Alexandrovich Kryukov 16-Nov-12 11:42am    
I don't see how it can be OK. Your question is about the call which cannot exist, even if the functionality may resemble it, as with Ajax (which is actually a key, in case of JavaScript).
--SA

Instead of this statement

JavaScript
var x="<?php ex(); ??>";


use a Jquery $.get to make a AJAX call to the server to do the operation and in the callback function to the get, use the statements
C#
alert(x);
    return false;
 
Share this answer
 
var function getOutput() {
   $.ajax({
      url:'myAjax.php',
      complete: function (response) {
          $('#output').html(response.responseText);
      },
      error: function () {
          $('#output').html('Bummer: there was an error!');
      }
  });
  return false;
}
 
Share this answer
 
If I understand your question correctly, a solution is to use php on the server side to not only retrieve the data but iterate through the record set to build the HTML. Then return the entire HTML (now a string) to the caller.

Depending upon the nuance of your circumstances, you need know nothing about how many rows are coming back, except, of course, a (server side) handler for an empty record set.

With a bit more to it (further capabilities, such as paging and excel dump), I have a generic php interface that retrieves the record set and column names, and therewith builds a table. It uses metadata from the php-SQL call to get the field names.

Something like this crude example: (where record set array is $data)
$rval = '<table class='your styles'>
foreach($data as $d) {
  $rval .= '<tr>';
  $rval .= "<td>{$d[0]}</td>";
  $rval .= "<td>{$d[1]}</td>";
   . . . (etc) . . .
  $rval .= '</tr>';
} // foreach($data as $d)
$rval .= '</table>';
return $rval;


 
Share this answer
 
v2
you can create any hummani string str='alert(4)';
and execute it with command 'eval'

eval("<?php somefunc() ?>");

or

let str='alert(4)';
eval(str);
 
Share this answer
 
Comments
Richard Deeming 30-Aug-18 13:39pm    
If you'd read the comments to the question, or either of the two previous solutions, you'd have realised that your code snippet is NOT calling PHP from Javascript.

You have effectively taken the code from the question, and posted it as a "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900