间歇性的JSON结果

时间:2012-11-27 22:55:13

标签: php jquery ajax json

晚上好,

我正在处理一个表单处理器,它确定向哪个表单发送进程和电子邮件/有时需要将结果返回到我的jquery以进行进一步的客户端处理。我遇到的问题是,第一次运行脚本时它会返回数组,但任何后续运行都不会返回数据。

PHP

    switch ($form){
    case 'cta':
        echo json_encode(array('callback' => 'cta'));
        //echo"";
        break;
    case "questionaire":

        echo json_encode(array('callback' => 'quetionaire'));
        break;
    case "contact":

        echo json_encode(array('callback' => 'contact'));
}

Jquery

function doEmail(form){
$.ajax({
   data: $(form).serialize(),
   type: 'POST',
   url: 'core/process.php',
   dataType: 'json',
   success: function(data){
    $(form +' input, textarea').each(function(){$(this).val("") })
    $('#lightbox').fadeOut(600)
    $('#responder').fadeOut(600)
    alert('form name '+ data.callback);

   }
 })     
}
function inputProcessor(event, form){
var $name = $(form + ' input#name').val();
var $phone = $(form + ' input#phone').val();
var $email = $(form + ' input#email').val();
var $msg = $(form + ' textarea#msg').val();

var $i = 0;
var $i1 = 0;
$(form +' [req=yes]').each(function() {
    console.log('step 1')
        $i++;
        $(this).css('background-color', '');
        if(!$(this).val().length) {
            event.preventDefault();
            $(this).css('background-color', '#ffd7d7');                
        }
    });
    $(form +' [req=yes]').each(function() {
        console.log('step 2')
        if($(this).val().length > 0){
            event.preventDefault();
            $i1++;                
        }
    });
    //if($i1 === $i){            
        var result = regex.test($email)
        console.log('step 3')
        if (result === true){
            event.preventDefault();
            console.log('Testing Email')
            doEmail(form);
        }else{
            $(form +' input#email').css('background-color', '#ffd7d7');                
        }                
    //}
}

还应注意,此网站永远不会刷新隐藏的div中所有内容,这些内容在点击“页面”链接时显示。

提前致谢

编辑“我实际上必须退出浏览器并重新启动才能获得新阵列”

编辑2

3种形式中的1种

<form id="cta"  method="post">
<input type="text" req="yes" id="name" class="input"   name="name" placeholder="Full Name"/><br>
<input type="text" req="yes" id="phone" class="input" name="phone" placeholder="Phone Number"/><br>
<input type="text" req="yes" id="email" class="input" name="email" placeholder="Email Address"/><br>     
<input type="hidden" name="form" value="cta">
<span id="submit_btn">Submit entry</span>
</form>

3种形式中的2种

<form id="contact_form" class=contact_form method="post">                            
<input type="text" req="yes" id="name" class="input3" name="name" placeholder="Name"/>
<br>  <br>
<input type="text" req="yes" id="phone" class="input3" name="phone" placeholder="Phone"/>
<br><br>
<input type="text" req="yes" id="email" class="input3" name="email" placeholder="Email"/>
<br><br>
<textarea id="msg" name="msg" placeholder="Type your message here" class="input3" rows="4"></textarea><br>
<input type="hidden" name="form" value="contact">
<span class="contact_button" id="SubmitContact"><div id="contact_submit">Send message</div></span>
</form>

3种形式中的3种

<form id="response" class=form1  method="post">

<input type="text" req="yes" id="name" class="input2" name="name" placeholder="Name"/>
<br>
<input type="text" req="yes" id="phone" class="input2" name="phone" placeholder="Phone"/>
<br>
<input type="text" req="yes" id="email" class="input2" name="email" placeholder="Email"/>
<br>
<textarea id="msg" name="msg" placeholder="Type your message here" class="input2" rows="4"></textarea><br>
<br>
<input type="hidden" name="form" value="questionaire">
<span class="submit_button" id="Submit"><div id="submit">Send message</div></span>
</form>

1 个答案:

答案 0 :(得分:0)

好的,所以我在睡了一夜之后回答了我自己的问题,并意识到我需要取消设置表格变量。

 var result = regex.test($email)
        console.log('step 3')
        if (result === true){
            event.preventDefault();
            console.log('Testing Email')
            formId = $(form +' input#formId').val();
            if(formId === 'cta'){
                $('#cta_form').children().fadeOut(600)
                window.location.href = 'docs/medical-alert.pdf';
            }
            doEmail(form, formId);
        }else{
            $(form +' input#email').css('background-color', '#ffd7d7');                
        }
        $(form).remove()