2012-05-01 4 views
0

これはWordPressサイトにあります。私は、入力ボックスからユーザーの電子メールを受け取り、それを私のmailchimpリストに追加するために、次のPHPを使用しています。そこにすべての重いmailchimpのものを行いJSとphpのページであるが、それは私の質問のためには重要ではありません。成功後のテキストのクリア値は?

<?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(); } 
?> 

次に、ユーザが電子メールを入力するページに、それが応答して表示をつかみますメッセージの結果ここはその部分です:

<div id="emailList"> 
    <form id="signup" action="<?=$_SERVER['PHP_SELF']; ?>" method="get" > 
    <label>signup for our email list</label> 

    <input type="text" id="email" class="email" name="email" /> 
    <input type="submit" value="OK" id="emailSubmit" name="submit" /> 

    </form> 
    <script type="text/javascript" src="path/to/js/jquery-1.4.2.min.js"></script> 
    <script type="text/javascript" src="path/to/js/mailing-list.js"></script> 
</div><!-- end emailList --> 
<div id="response"> 
    <? require_once('inc/store-address.php'); 
    if($_GET['submit']) 
    { 
     echo storeAddress(); 
    } ?> 
</div><!-- end response--> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#emailSubmit").click(function() { 
        $('#response').fadeIn(1500).delay(3500).fadeOut(1500); 
        }); 
       }); 
</script> 

私の質問は簡単です。成功した場合、「成功!サインアップを確認する電子メールを確認してください」というメッセージが返されるように、どうすれば実装できますか?入力ボックスの値をそのまま残すのではなく、入力ボックスの値をクリアするだけですか?入力ボックスIDは「電子メール」です。

私は使用する必要があると思います。どういうわけか、val("")ですが、成功した場合にのみメールをクリアする方法はわかりません。

答えて

1

一つの簡単な解決策は以下のとおりです。

PHP

if($api->listSubscribe($list_id, $_GET['email'], '') === true) { 
    // It worked! 
    return '<span class="success">Success! Check your email to confirm sign up.</span>'; 
}else{ 
    // An error ocurred, return error message 
    return 'Error: ' . $api->errorMessage; 
} 

JS

$(document).ready(function() { 
    if($('.success').length > 0) { 
     $('#email').val(''); 
    } 
}); 

編集:私はあなたの提案を試み、成功スパンクラスを追加

$(document).ready(function() { 
    var clearOnSuccess = function() { 
     if ($('.success').length > 0) { 
      $('#email').val(''); 
     } 
    }; 
    clearOnSuccess(); 

    $('#signup').submit(function() { 
     var data = $(this).serializeArray(); 
     data.push({'name': 'ajax', 'value': 1}); 
     var jqXHR = $.ajax({ 
      url: $(this).attr('action'), 
      type: $(this).attr('method'), 
      data: data 
     }); 

     jqXHR.done(function(data) { 
      $('#response').html(data).fadeIn(1500).delay(3500).fadeOut(1500); 
      clearOnSuccess(); 
     }); 
     return false; 
    }); 
}); 
+0

フルAjaxのソリューションしかし、それは動作しませんでした。成功のためのjqueryのif文には0より大きい値がありません。 – thindery

+0

ajaxを使用しますか?このコードは、ドキュメントの読み込み時にspan要素が存在する場合にのみ機能します。 ajaxを使用する場合、完了後にifステートメントを呼び出します。 –

関連する問題