我有asked before的帮助来让我的php表单在phonegap ipa中工作.我完全按照我的指示做,即使在ipa内,一切都正常.我唯一的问题是,在页面加载时我会立即收到此警报,很明显,当客户端忘记填写必填字段时,该警报应该会显示.
这是我所做的:
contact.html中的FORM
<form action="http://mobile.alicante-intermedia.com/submit_contact.php" method="get">
<div class="form-element">
<label for="txtfullname">Firstname</label>
<input id="txtfullname" name="FirstName" type="text" placeholder="required" required />
</div>
<div class="form-element">
<label for="txtemail">Lastname</label>
<input id="txtemail" name="LastName" type="text" placeholder="required" required />
</div>
<div class="form-element">
<label for="txtcontact">Email</label>
<input id="txtcontact" name="Email" type="email" placeholder="optional" />
</div>
<div class="form-element">
<label for="txtmessage">Message</label>
<textarea id="txtmessage" name="MessageText" placeholder="required" rows="5" required ></textarea>
</div>
<input type="button" onclick="submit()" value="submit contact"/>
</form>
然后,我创建了一个jquery_form.js文件,该文件仅在conatct.html内部加载
$.post('http://mobile.alicante-intermedia.com/submit_contact.php', {
// These are the names of the form values
FirstName: $('#FirstName_input').val(),
LastName: $('#LastName_input').val(),
Email: $('#Email_input').val(),
MessageText: $('#MessageText_input').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("Please fill all fields!");
return false;
}
});
和PHP看起来像这样:
<?php
// VARS
$FirstName=$_GET["FirstName"];
$LastName=$_GET["LastName"];
$Email=$_GET["Email"];
$MessageText=$_GET["MessageText"];
$Headers = "From:" . $Email;
//VALIDATION
if(
$FirstName=="" ||
$LastName=="" ||
$Email=="" ||
$MessageText==""
) {
echo "Error";
} else {
mail("myemail@email.com","mobile app message",$MessageText, $Headers);
echo "Success";
}
?>
除警报屏幕外,其他所有功能均正常.有人知道哪里出了问题吗?
解决方法:
您的JavaScript代码是“裸露的”,没有包装在任何函数中或未附加到任何事件处理程序,因此在加载后立即执行-因此,在首次解析jQuery脚本时,它会立即发布空表格.
将其放入提交按钮的onclick事件处理程序中:
// When the document has loaded...
$(document).ready(function() {
// Bind this action as a function to be executed when the button is clicked...
$('input[type="button"][value="submit contact"]').click(function() {
$.post('http://mobile.alicante-intermedia.com/submit_contact.php', {
// These are the names of the form values
// EDIT: You have the wrong ids on these...
FirstName: $('#txtfullname').val(),
LastName: $('#txtemail').val(),
Email: $('#txtcontact').val(),
MessageText: $('#txtmessage').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("Please fill all fields!");
return false;
}
});
});
});
由于它绑定在JavaScript代码中,因此请从标记中的按钮中删除onclick:
<input type="button" value="submit contact"/>
编辑:
您拥有的PHP正在$_GET中寻找值,但是您已经从jQuery中发布了它们.而是在$_POST中查找.
$FirstName=$_POST["FirstName"];
$LastName=$_POST["LastName"];
$Email=$_POST["Email"];
$MessageText=$_POST["MessageText"];