2010-06-03 91 views
1

我有一个表单需要运行一个php脚本,一旦点击提交按钮,它需要是ajax。需要在提交表单时运行php脚本

<form method="post" action="index.php" id="entryform" name="entryform"> 
<input type="submit" name="submit" value="Submit" onclick="JavaScript:xmlhttpPost('/web/ee_web/include/email-notification.php', 'entryform')" /> 
</form> 

在这种情况下,使用if(表单提交){得到的结果和运行脚本}是不是一种选择,我需要外部运行脚本,这就是为什么我需要它来执行电子邮件通知。 php onclick

当我将我的网页浏览器指向domain.com/include/email-notification.php时,它运行完美。

任何帮助表示赞赏。

这会在提交表单之前执行脚本,现在我需要等待一旦点击提交按钮来执行脚本,可以做到这一点?

$.ajax({ 
    type: "POST", 
    url: "/include/email-notification.php", 
    dataType: "script" 
}); 
+0

我想你应该把它指向/include/email-notification.php在表单中呢? – baloo 2010-06-03 15:06:37

+0

是的,我想知道同样的事情。布拉德,是'/ web/ee_web'部分应该在那里? – KyleFarris 2010-06-03 15:09:56

+0

/web/ee_web /是绝对路径 – Brad 2010-06-03 15:12:44

回答

0

试着改变你的 “点击” 值:

<input type="submit" name="submit" value="Submit" onclick="JavaScript:xmlhttpPost('/web/ee_web/include/email-notification.php', 'entryform'); return false" /> 

这是假设的 “xmlhttpPost()” 的东西,实际工作自身。

1

我建议使用jQuery。由于其灵活性和conveinience

如果您想发送电子邮件通知,然后只发布的数据,那么请执行下列操作:

创建一个单独的页面来处理像email.php

// fill in headers and prepare content 
$result = mail(............................); 
if($result) { return 1; } else { return 0; } 

您的电子邮件通知然后在你的表格上

$('#mysubmitbutton').click(function() { 
     $.post(
      'email.php', 
      { emailadd: '[email protected]', content: 'mycontent' }, //send parameters to email.php 
      function(data) { 
       //now check whether mail sending was successfull 
       if(data==1) { 
        //message sending was successful so carry out further operation 
        //................................. 
       } 
       else { alert("Sorry, email notification was unsuccessfull"); } 
      }); 
});