user1715245
user1715245

Reputation:

Jquery $.post() data is returning incorrectly

I run $.post() the first time witch returns the correct data json data to run the if statement but the next time $.post() is used it returns incorrectly causing the if statement to not work as intended.

My current Jquery code:

$.post('functions/write_settings.php', { dbname: $('#dbname').val(), dbhost: $('#dbhost').val(), dbpassword: $('#dbpassword').val(), dbusername: $('#dbusername').val(), dbprefix: $('#dbprefix').val() }, function(data){
    alert("here");
    try{var returndata = $.parseJSON(data);}catch(e){/*cont*/}
    alert("here1");
      if($(returndata).size() >= 1){
          for(var i = 0; i < $(returndata).size(); i++){
              $("#" + returndata[i]).css('background', 'url(style/install/database/imgs/textbox_error.png)');
          }
          ResetButton();
          alert("here2");
    }else{
        $.post('functions/check_settings.php', function(data){
            alert(data);
            var postdata = data;
            $.trim(postdata);
            if(postdata == 'error'){
                alert("test2");
                $('#pdbe').show();
                $('#pdbe').effect("pulsate", { times:10 }, 10000);
                ResetButton();
            }else if(data == '0'){
                alert('connected!');
            }
        });
    }
});

This is check_settings.php

$cwd = getcwd();
$file = $cwd.'/dbconfig.php';
//check dbconfig created
if(file_exists($file)){
    include('./dbconfig.php');
}else{
    echo("error2");
    die();
}

//grab required vars
$host = $config['db']['host'];
$dbname = $config['db']['dbname'];

//test basic connection
try{
   $dbh = new PDO("mysql:host=$host;dbname=$dbname", $config['db']['username'], $config['db']['password']);
   $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
   echo("error");
   die();
}
echo("0");

Like i said the first $.post() returns the correct data while the second time when it uses it it will return incorrectly and cause the if statement to not work correctly.

any suggestions to fix this issue would be greatly appreciated!

Upvotes: 1

Views: 122

Answers (1)

Vishnu
Vishnu

Reputation: 461

Is your problem the extra spaces? If so, in the check_settings.php you may be unknowingly printing spaces or new line. For example, you may be having space or new line before the first

Upvotes: 1

Related Questions