magicianiam
magicianiam

Reputation: 1579

Ajax validation using jquery and php where php file is also used as another page

So I have this validation using AJAX jQuery, everything worked fine when I used another file to host the PHP validation part that the ajax will check, then when I transferred it to the necessary file the one where it is also being used as a page in my site everything stopped working.

Note that this file since it is used in the site is filled with styles,scripts, lots of echo and etc. though I did use an if to isolate the PHP validation that ajax will use but it still doesn't work, though when I alert the success data it was filled with CSS style calls and my needed return value.

Is there any work around this problem or do I really have to use a separate file just to hold this validation code which I'm trying to avoid since I'm trying to have as limited number of files as possible.

Ajax code:

$("#formadder").click(function() {
    var formname = $('#formname').val();
    if(formname == "") {
        alert("Input Form Name!");
        return false;
    }
    $('#form-feedback').html('<img src="../images/ajax-loader.gif"/>');
    $.ajax({
        url: '../createForm.php',
        type: 'POST',
        data: "formname="+ formname,
        dataType: "json",
        success: function(d) {
            alert(d);
            if(d.stat == "1") {
                $('#form-feedback').html(d.error).css('color','red');
                return false;
            }
            else {
                $('#form-feedback').html('Form Created!').css('color','green');
                window.location.reload();
            }
        }
    });
});

PHP code for createForm.php is your typical page doctype, included styles and scripts, body tags, the whole package which I believe is interfering with the AJAX call.

The content of the success alert:

test

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

test is the sample output I want to retrieve via AJAX, but for some reason, the doctype is also returned. I removed JSON from AJAX on this one.

Upvotes: 0

Views: 158

Answers (2)

user2195741
user2195741

Reputation:

Is there any work around this problem or do i really have to use a separate file just to hold this validation code which **im trying to avoid since im trying to have as limited number of files as possible.**

You have you have as many files as possible, that doesnot matter. Search for what is abstraction in programing.

And if you want to validate then you just have to check if is reciving a post or not with isset function() in php. And you could just use the validation on server side or client side, its not necessary in both sides.

Upvotes: 0

aziz punjani
aziz punjani

Reputation: 25786

That's happening because the same html that is used to build the page is also getting sent to the ajax call. I my opinion it's better to have this logic in a separate PHP file, but if you really want to store it in one file then make sure to check if the $_POST array is empty, if it is then serve up the page Html, CSS, if not then do the validation and send back the response.

if( empty($_POST) ) {
// html, css and js here 
}
else 
{
   // php validation here 
}

Upvotes: 1

Related Questions