За последние 24 часа нас посетили 15960 программистов и 1666 роботов. Сейчас ищут 988 программистов ...

Форма PHP+AJAX+JQUERY, Помощь в JSON

Тема в разделе "JavaScript и AJAX", создана пользователем crazylex20087, 26 июн 2011.

  1. crazylex20087

    crazylex20087 Активный пользователь

    С нами с:
    26 июн 2011
    Сообщения:
    1
    Симпатии:
    0
    Добрый день! Всем заранее спасибо,кто прочитает и может поможет. Делаю форму по этому примеру
    http://tutorialzine.com/2010/04/carbon-signup-form/
    Хочу слегка модифицировать,хочу чтобы по завершения проверки формы вместо перенаправления(как в статье) эти данные вылезали в блоке вверху страницы(AJAX без перезагрузки),как например вылезают блоки с ошибкой

    Модифицированный JS CODE:
    [js]
    // Issuing a POST ajax request to submit.php (the action attribute of the form):
    $.post($('#signupForm').attr('action'),$('#signupForm').serialize()+'&fromAjax=1',function(response){

    if(!response.status)
    {
    // Some kind of input error occured

    // Looping through all the input text boxes,
    // and checking whether they produced an error
    $('input[type!=submit]').each(function(){
    var elem = $(this);
    var id = elem.attr('id');

    if(response[id])
    showTooltip(elem,response[id]);
    });
    }
    else {
    $(response.html).hide().insertBefore('#signupForm').slideDown(900);
    $('#body').val('');}
    }

    [/js]

    Модифицированный PHP:
    PHP:
    1.  
    2.  
    3. if($_POST['fromAjax'])
    4. {
    5.     if(count($errors))
    6.     {
    7.         $errString = array();
    8.         foreach($errors as $k=>$v)
    9.         {
    10.             // The name of the field that caused the error, and the
    11.             // error text are grouped as key/value pair for the JSON response:
    12.             $errString[]='"'.$k.'":"'.$v.'"';
    13.         }
    14.        
    15.         // JSON error response:
    16.         die ('{"status":0,'.join(',',$errString).'}');
    17.     }
    18.        
    19. function markup($name,$email,$pass){
    20.         return '
    21.             <div class="comment" id="new">
    22.                 <div class="name">'.$name.'</div>
    23.                 <div class="date" title="Added at '.$email.'</div>
    24.                 <p>'.$pass.'</p>
    25.             </div>
    26.         ';
    27. };
    28.     // JSON success response.
    29.         echo json_encode(array('status'=>1,'html'=>markup($_POST['name'],$_POST['email'],$_POST['pass'])));
    30.  
    31.     exit;
    32. }
    33.  
    Мне кажется дело в // JSON success response. Видно что то не так передаю или делаю. Заранее спасибо!