1,105,177 Community Members

POST data don't work on server

Member Avatar
OsaMasw
Posting Whiz in Training
212 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Hey there I have a strange problem, this is the first time I see something like this
I have a form and this form data posted to php file through ajax

$('form.ajax').on('submit',function(){
        var that = $(this),
            url = that.attr('action'),
            method = that.attr('method'),
            data = {};
        that.find('[name]').each(function(index,value) {
            var that = $(this),
                name = that.attr('name'),
                value = that.val();
            data[name] = value;
        });
        $.ajax({
           url: url,
            method: method,
            data: data,
            success: function(response) {
                    $("#notes").slideDown(600).html(response).show().delay(5000).slideUp(600)
            }
        });
        return false;
    });

on localhost on my PC its working fine, but when uploaded to server its sending data but never reached the php file

8e5cf9b2dfd126ac1df98d84a9bf1d7e

Attachments
Member Avatar
Atli
Practically a Master Poster
691 posts since May 2007
Reputation Points: 182 [?]
Q&As Helped to Solve: 107 [?]
Skill Endorsements: 16 [?]
 
0
 

Hey.

What do you mean by: "never reached the php file"? According to the image you posted, there was no error code returned, and the request yielded a response.

What did PHP return? What status code did the request give you?

Member Avatar
OsaMasw
Posting Whiz in Training
212 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Because I have this code in my php file

if ($_POST['btn']){
    $userlog = $_POST['username'];
    $passlog = $_POST['password'];
    $remember = (int)$_POST['remember'];
    if($userlog != "" && $passlog != "") {
    // do login process
    }
    else {
        echo '<span class="label label-important">Please enter Username or Password</span>';
        }
}

the output always "Please enter Username or Password", but in my local server the code works fine without any problem.

Member Avatar
Atli
Practically a Master Poster
691 posts since May 2007
Reputation Points: 182 [?]
Q&As Helped to Solve: 107 [?]
Skill Endorsements: 16 [?]
 
0
 

Ok. First things first.

Sending the button value is completely unnecessary. You don't need it, nor should you even be using it like that. You should always make sure the values you need have been sent. That a submit button's value was sent is of no consequence. - Use the isset and/or empty functions to make sure values exist before they are used.

// Make sure the username and password were sent, and that they
// are not empty.
if (!empty($_POST["username"]) && !empty($_POST["password"])) {
    // Fetch the remember value, defaulting to 0 if none was sent.
    $remember = 0;
    if (isset($_POST["remember"])) {
        $remember = (int)$_POST["remember"];
    }

    // Do login process
}
else {
    // Return username and password error
}

You should also consider having PHP return it's results to JavaScript using codes, maybe even in JSON format, instead of having it return HTML. JavaScript is more than capable of creating that HTML; there is no need to waste the bandwidth on having PHP generate it all.

the output always "Please enter Username or Password",

In your PHP code, use the var_dump function to show you the contents of the $_POST data. Then check out what it shows you in the Firebug response tab. That should give you some insights into why the PHP code isn't behaving as you'd expect.

Member Avatar
OsaMasw
Posting Whiz in Training
212 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

OK I'll try what you suggest, but Why its working fine on my PC ? it it because of OS ??

Member Avatar
Atli
Practically a Master Poster
691 posts since May 2007
Reputation Points: 182 [?]
Q&As Helped to Solve: 107 [?]
Skill Endorsements: 16 [?]
 
0
 

Could be, but it's hard to say. There are any number things that can be different from server to server. OS related issues tend to be more about file handling (Windows and Unix handle files differently) or things related to extension installations. It's more likely something to do with the configuration of PHP or the HTTP server.

Member Avatar
OsaMasw
Posting Whiz in Training
212 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Sorry for late in response, after investigating the problem I found that if I didn't filter the inputs, the script works fine on server, I have custom function to do this, its simple

function clean($text) {
    $text = strip_tags($text);
    $text = str_ireplace("'", "", $text);
    $text = str_ireplace('"', '', $text);
    $text = mysql_real_escape_string($text);
    return $text;
}

I don't know why, I used this function a million times before that, and now I have problem while using it :(

Member Avatar
OsaMasw
Posting Whiz in Training
212 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

My Problem solved :D
when writing the above post I've noticed am using "mysql_real_escape_string" while my script using Mysqli :D, so this was my problem,
thanks for the help.

Question Answered as of 8 Months Ago by Atli
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: