0

How do I validate my code in PHP without getting error messages defined in the ajax definition in main.js?

Note: Chrome console returning: XMLHttpRequest cannot load file:///C:/Documents/Mini%20Revision%20Projects/Project%20Website%203/ajax.php. Received an invalid response. Origin 'null' is therefore not allowed access.

Below is my code:

main.html

<!DOCTYPE HTML>
<html>
    <head>  
        <script type="text/javascript" src="C:\Documents\jQuery\jquery2.js"></script>
    </head> 

    <body>

        <ul id="info1">
                <li>Put anything in the field below.</li>
        </ul>
        <form id="form1">
                <input type="text" name="field1" id="field1">
                <input type="submit" name="submit" id="submit" value="Submit Form">
        </form>

        <script type="text/javascript" src="main.js"></script>  

    </body>

</html>

main.js

$(document).ready(function() {

    $("#form1").submit(function( event ) {
        event.preventDefault();
        //alert("happy");
        $.ajax({
            type: 'POST',
            url: 'ajax.php',
            data: $(this).serialize(),
            dataType: 'json',
            success: function (data) {
                console.log(data);
                $("#info1").html(data.msg);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("Status: " + textStatus);
                alert("Error: " + errorThrown);
            }
        });
    });
});

ajax.php

<?php
    class ajaxValidate {

            function formValidate() {
                    //Put form elements into post variables (this is where you would sanitize your data)
                    $field1 = @$_POST['field1'];

                    //Establish values that will be returned via ajax
                    $return = array();
                    $return['msg'] = '';
                    $return['error'] = false;

                    //Begin form validation functionality
                    if (!isset($field1) || empty($field1)){
                            $return['error'] = true;
                            $return['msg'] .= '<li>Error: Field1 is empty.</li>';
                    }

                    //Begin form success functionality
                    if ($return['error'] === false){
                            $return['msg'] = '<li>Success Message</li>';
                    }

                    //Return json encoded results
                    return json_encode($return);
            }

    }

    $ajaxValidate = new ajaxValidate;
    echo $ajaxValidate->formValidate();
?>
4
  • are you at least using any LAMP ? from the chrome error - I'm guessing not... Commented Jun 18, 2014 at 15:52
  • @LorDex - I don't know what LAMP is unfortunately. I guess that answers the question :S. Commented Jun 18, 2014 at 15:54
  • 1
    you need to have PHP server in order to PHP scripts to work. install something like XAMPP (windows) or MAMP (linux) Commented Jun 18, 2014 at 15:58
  • 1
    @LorDex - I didn't have a PHP server. DOH! Put that as the answer please I will mark it. Commented Jul 7, 2014 at 16:42

2 Answers 2

1

First of all, verify if PHP is not returning a warning or critical error. Insert this code in top of your code. If PHP returns a hidden error, the success data value will be null.

ini_set("display_errors", "On");
error_reporting(E_ALL);

PHP Error reporting manual

PHP run-time display_error

I think that if you are returning a JSON array in php, you need to call an array.

$("#info1").html(data['msg']);

Try to return the post values to verify if $_POST is not empty:

return json_encode($_POST);

You don't need to define an empty array if you define directly a first row.

//$return = array();
$return['msg'] = '';
Sign up to request clarification or add additional context in comments.

Comments

0

you need to have PHP server in order to PHP scripts to work. install something like XAMPP (windows) or MAMP (linux)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.