Phonegap本机应用程序,通过PHP邮件程序在实时服务器上提交表单

我试图通过我的原生应用程序中的实时服务器处理我的表单而不离开页面。 下面是我用于表单,JQuery和PHP的代码,但是当我发送它时,页面变为空白并将字符串放在URL栏中。 我可以帮助我获得一些代码方面的帮助。 我缩短了这个表格以便于阅读,我还有很多其他领域。 我是否需要将它们全部列在javascript中?

表格上的HTML

JavaScript的:

 $.post('http://mobile.koolantkoolers.com/mailer.php', { // These are the names of the form values Name: $('name').val(), Email: $('email').val(), Company: $('company').val() // HTML function }, function (html) { // Place the HTML in a astring var response=html; // PHP was done and email sent if (response=="success") { alert("Message sent!"); } else { // Error postback alert("Sorry please fill all fields!"); return false; } }); 

然后是PHP

  

我最终在我的php上使用以下代码和post方法。 您只需要确保您的div命名约定与您的代码匹配。 这会发布表单的结果并将谢谢消息交换出div。 请参阅下面的我的HTML和Javascript。

 

使用Javascript:

 $(document).ready(function() { //When the form with id="myform" is submitted... $("#myform").submit(function() { //Send the serialized data to mailer.php. $.post("mailer.php", $("#myform").serialize(), //Take our repsonse, and replace whatever is in the "formResponse" //div with it. function(data) { $("#myform").html( $("#formResponse").html() ); } ); return false; }); }); 

一个简单的方法是使用jQuery进行这样的post调用。

event.preventDefault(); 阻止以通常方式提交表单,以便您可以保持在同一页面上。

$(“#ID_OF_FORM”)。submit(function(event){

event.preventDefault();

$ .post(“http://mobile.koolantkoolers.com/mailer.php”,$(“ID_OF_FORM”)。serialize()); }

这非常简单,在脚本中你用你要发布的数据指定form-id和发送它的URL,每个表单将数据发送到你指向它的URL而不改变PHP的任何内容,只是将此内容添加到您在提交按钮中调用的javascript函数中。

 var form = document.getElementById('form-id'); var formData = new FormData(form); 

这个新的FormData实例是传递send()调用所需的全部内容:

 var xhr = new XMLHttpRequest(); // Add any event handlers here... xhr.open('POST', '/upload/path', true); xhr.send(formData); 

检查链接