Php 提交/成功后如何清除表单字段?
我使用的是完全相同的MailChimp PHP/AJAX订阅处理表单——但在成功后如何真正将表单字段清除掉,我感到十分困惑 显然,这是Felix在这里推荐的——代码只有在文档加载时存在span元素时才能工作,如果使用Ajax,则必须在完成后调用if语句 谁能帮帮我吗?用户成功完成表单字段后,如何清除该字段 代码如下:Php 提交/成功后如何清除表单字段?,php,ajax,Php,Ajax,我使用的是完全相同的MailChimp PHP/AJAX订阅处理表单——但在成功后如何真正将表单字段清除掉,我感到十分困惑 显然,这是Felix在这里推荐的——代码只有在文档加载时存在span元素时才能工作,如果使用Ajax,则必须在完成后调用if语句 谁能帮帮我吗?用户成功完成表单字段后,如何清除该字段 代码如下: <?php // store-address.php function storeAddress(){ // Validati
<?php
// store-address.php
function storeAddress(){
// Validation
if(!$_GET['email']){ return "No email address provided"; }
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) {
return "Email address is invalid";
}
require_once('MCAPI.class.php');
// grab an API Key from http://admin.mailchimp.com/account/api/
$api = new MCAPI('my-api-key');
// grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
// Click the "settings" link for the list - the Unique Id is at the bottom of that page.
$list_id = "my-list";
if($api->listSubscribe($list_id, $_GET['email'], '') === true) {
// It worked!
return 'Success! Check your email to confirm sign up.';
}else{
// An error ocurred, return error message
return 'Error: ' . $api->errorMessage;
}
}
// If being called via ajax, autorun the function
if($_GET['ajax']){ echo storeAddress(); }
?>
有两种解决方案 不那么优雅的方法是创建一个JS函数,手动重置每个表单字段的值 更好的方法是在表单中使用ID存储隐藏的重置输入:
<input type="reset" id="blagh" style="display:none"/>
具有
resetForm()
。就说吧。好的,谢谢你的建议。但是,在我上面的代码中,我应该在什么地方放置jQuery位?@user2485157,此时您需要重置表单。谢谢您的提示。但是,在我上面的代码中,我应该把这些代码放在哪里呢?在单独的脚本中?您的电子邮件验证正则表达式不允许某些字符,例如plus。由于任何人都可以提交虚构的“有效”电子邮件地址,因此通常最好使用非常简单的格式,例如/.++.+(?:\…+*/
),然后发送确认电子邮件以确保他们说的是实话。
// Load Events Listeners
Event.observe(window, 'load', init, false);
function init(){
Event.observe('signup','submit',storeAddress);
}
// AJAX call sending sign up info to store-address.php
function storeAddress(event) {
// Update user interface
$('response').innerHTML = 'Getting email address...';
// Prepare query string and send AJAX request
var pars = 'ajax=true&email=' + escape($F('email'));
var myAjax = new Ajax.Updater('response', 'inc/store-address.php', {method: 'get', parameters: pars});
Event.stop(event); // Stop form from submitting when JS is enabled
}
<input type="reset" id="blagh" style="display:none"/>
$('#blagh').click();