Problem with Json in Jquery

Posted by Davidslv on Stack Overflow See other posts from Stack Overflow or by Davidslv
Published on 2010-06-15T09:54:13Z Indexed on 2010/06/15 10:12 UTC
Read the original article Hit count: 220

Filed under:
|
|
|

Hi everyone,

I have a small survey, but when i submit it says that the msg is null, i don't see where is my error.

If you could help me i apreciate it.

Thank you

My Form with Jquery and Ajax Function

<!DOCTYPE HTML>
<hmtl lang="pt-PT">
        <head>
                <title>Formul&aacute;rio</title>
                <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
        </head>
<body>
<hr />
        <h3>Formulario</h3>

        <script type="text/javascript" charset="utf-8">
                $(function () {
                        $('#submiter').click( function() {
                                $.ajax({
                                  type: "POST",
                                  url: 'validar.php',
                                  dataType: 'json',
                                  success: function(msg) {
                                        alert('Mensagem '+ msg.mensagem);
                                },
                                error : function () {
                                        alert('Ocorreu um erro');
                                }
                                });

                        });
                });
        </script>

        <form id="formulario" action="" method="post" accept-charset="utf-8">
                <p>Pergunta 1</p>
                <label for="pergunta1">sim</label><input type="radio" id="p1" name="pergunta1" value="1" /><br />
                <label for="pergunta1">n&atilde;o</label><input type="radio" id="p1" name="pergunta1" value="0" />
                <br />

                <p>Pergunta 2</p>
                <label for="pergunta2">sim</label><input type="radio" name="pergunta2" value="0" /><br />
                <label for="pergunta2">n&atilde;o</label><input type="radio" name="pergunta2" value="1" />
                <br />         

                <p>Pergunta 3</p>
                <label for="pergunta3">sim</label><input type="radio" name="pergunta3" value="0" /><br />
                <label for="pergunta3">n&atilde;o</label><input type="radio" name="pergunta3" value="1" />
                <br />         

                <p>Pergunta 4</p>
                <label for="pergunta4">sim</label><input type="radio" name="pergunta4" value="0" /><br />
                <label for="pergunta4">n&atilde;o</label><input type="radio" name="pergunta4" value="1" />
                <br /> 


                <p><input type="submit" id="submiter" value="Continue &rarr;"></p>
        </form>
        </body>
</html>

My PHP

<?php
    if($_POST) {
        $pergunta1 = $_POST['pergunta1'];
        $pergunta2 = $_POST['pergunta2'];
        $pergunta3 = $_POST['pergunta3'];
        $pergunta4 = $_POST['pergunta4'];

        $calcular = $pergunta1 + $pergunta2 + $pergunta3 + $pergunta4;

        $var = array ('mensagem' => $calcular);

        echo json_encode($var);
    }
    else {
        $var2 = array('mensagem' => 'sem resultado');
        echo json_encode($var2);
    }
?>

© Stack Overflow or respective owner

Related posts about php

Related posts about jQuery