2 Replies - 7479 Views - Last Post: 02 March 2011 - 09:25 PM Rate Topic: -----

#1 newbieHQ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 20-January 11

call php function from javascript

Posted 02 March 2011 - 08:58 PM

hi, evryone. ia m trying to call php function from javascript and return the alert box ... i was planning whenever i click the button of confirmation OK .. it will send out the email ... here is the code... i have trying so hard and cant run it .... can u all help me in this? thank you !

<html>
<head>
<script type="text/javascript">
function confirm()
{
var r=confirm("Press me!");
if (r==true)
  {
  alert("OK!");
  sendmail();
  }
else
  {
  alert("Cancel!");
  }
}
</script>
</head>
<body>

<input type="button" onclick="confirm()" value="Show a confirm box" />

</body>
</html>

<?php
function sendmail(){
error_reporting(0);
ini_set("SMTP", "gw.usm.my");
ini_set("sendmail_from", "quan880801@yahoo.com");

$to="slayhk_88@hotmail.com";

$subject ="Hello there!";

$message ='This is Fun!';

// Set content-type header
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; 
		charset=iso-8859-1' . "\r\n";

if (mail($to, $subject, $message, $headers)) {
       echo 'E-mail sent';
}
}
?>



thank you so much!

Is This A Good Question/Topic? 0
  • +

Replies To: call php function from javascript

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 793
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: call php function from javascript

Posted 02 March 2011 - 09:02 PM

Look into AJAX to accomplish this. A decent starter resource is here:
http://www.w3schools...jax/default.asp

It would be an idea to look into a JS lib that simplifies the process for you as well, so you don't have to write out (and fully understand) the AJAX code. I would recommend jQuery for this, and looking at the .load() function in that library.
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3512
  • View blog
  • Posts: 10,137
  • Joined: 08-June 10

Re: call php function from javascript

Posted 02 March 2011 - 09:25 PM

in short, you cannot call a PHP function, only a PHP script, because PHP runs on the server and Javascript on the client.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1