views:

562

answers:

3

I need to fire a php class from a javascript function.

code:

<input type="button" name="Submit" value="Submit" class="opinionbox" 
onclick="verifyControl('<?=$control_no?>')"/>

function verifyControl(rNo) {
 Cont_no=document.getElementById("ContNo").value;
  if(rNo==Cont_no) {
    frames['frame1'].print();
    showPage('payment');
  }
  else if(rNo!=Cont_no) {
    alert("invalid  control no");
  } 
}

i need to run the code

$data = $obj_com -> getSelectedData('tbl',
                      'control_no', $contno);
$control_no = $contno;
$obj_com -> recordPay('tbl',$contno);

inside the verifyControl() how can I do this?

+7  A: 

You cannot "call" a PHP class from Javascript because Javascript is run on the client side (ie, the browser) while PHP is run on the server. What you can do, however, is call a PHP script asynchronously, get its output, and do fun stuff with javascript. This is known as AJAX. If you're going to go down this road, you are highly advised to use a library like jQuery and learn from there. Here are a few questions to get you started (check out the answers):

Paolo Bergantino
+2  A: 

To call PHP code from Javascript, given that PHP is executing on the server and Javascript is executing on the client, you will need to set up some sort of interface at the server that can be accessed remotely.

You may also want to be aware of the security implications of doing so. In particular, if you want to ensure that only your users will be calling your server in this way - that is, if a malicious user calling this code could do damage, you will need some sort of authentication.

You will also need to decide on a protocol for communicating between the client and server.

Protocols such as SOAP and XML-RPC define everything you need to remotely call procedures on the server. Or you can roll your own, just by calling a certain URL and receiving a certain result, in a certain format (JSON can help) from the server.

thomasrutter
A: 

you can use Brent Ashley jsrsClient.js or $.ajax of jQuery Javascript lib.

Thau