Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 845987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:32:32+00:00 2026-05-15T06:32:32+00:00

I have a small survey, but when i submit it says that the msg

  • 0

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);
    }
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:32:32+00:00Added an answer on May 15, 2026 at 6:32 am

    There are two problems. Halfway down the PHP you have <php not <?php. But really you should be using json_encode. Just make a PHP array with your data, json_encode it, and it’ll do all the clever JSON stuff for you. It’ll be much easier to maintain that way.

    Another problem: JSON keys should be strings.

    Try something like this:

    $result = array();
    if ($calcular <= 9) {
        $result['mensagem'] = 1;
    }
    //... etc.
    
    echo json_encode($result);
    

    Edit: I’ve just done a check, and the following works for me:

    $(document).ready(function () {
        $.ajax({
            type: "POST",
            url: 'validar.php',
            dataType: 'json',
            success: function(msg) {
                alert('Mensagem '+ msg.mensagem);
            },
            error : function () {
                alert('Ocorreu um erro');
            }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have small application: http://jsfiddle.net/Bwdf6/4/ But with click on delete, tags that created after
have small problem, and would very much appreciate help :) I should convert byte
I have small query today I happen to see when I typed a website
I have small web app that generate PDF files as a report. I'm trying
I have small HTTP server script that receives HTTP client requests and replies. I
We have small class, that has a related class class Car has_one :engine, :dependent
I'm working on a small project that displays answers for a survey. I'm having
I have small application that is uploading pictures to another website via webservice. My
I have small problem with http 404 error, after click on 'allow' on facebook
I have small question regarding the following error: this.context.sourceCache On a custom control I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.