1

I am having a small problem with my jQuery function, and below is the code:

function registerUser()
{
    var formData = $('#registerForm').serialize();
    var dataPost = $.post('classes/login.php', {type:'register', data: formData} ,function (data)
    {
        console.log(data);
    });
};

Then my login.php file looks like:

$type = $_POST['type'];
$email = $_POST['data']['0'];
$password = $_POST['data']['1'];

function register_user($email, $password)
{
 global $db;
//lets add some code to register user
    $stmt = $db->prepare("INSERT INTO user (email, password, role, banned) VALUES (:email, :password, 'level1', 'N')");
    $stmt->execute(array(':email'=> $email, ':password'=> $password));
    $rowCount  = $stmt->rowCount();
   if($rowCount > 0)
   {
    // Success
    //  set session data and etc.
    return "success";
   }
   else
   {
    //  Failed
    return "Error, please try again!";
   }
}
$result = register_user($email, $password);

When I executed it I did not get any errors or anything but a simple blank line that does not show any data and the data does not get inserted into the database?

ZygD
  • 22,092
  • 39
  • 79
  • 102
Bryce
  • 447
  • 2
  • 9
  • 24

2 Answers2

3

You want to post extra parameter with form serialization.

function registerUser()
{
    var formData = $('#registerForm').serializeArray();
    formData.push({name: 'type', value: 'register'});
    var dataPost = $.post('classes/login.php', formData ,function (data)
    {
        console.log(data);

    });
};

Now Server side update

$type = $_POST['type'];
$email = $_POST['LoginEmail'];
$password = $_POST['LoginPassword'];

function register_user($email, $password)
{
 global $db;
//lets add some code to register user
    $stmt = $db->prepare("INSERT INTO user (email, password, role, banned) VALUES (:email, :password, 'level1', 'N')");
    $stmt->execute(array(':email'=> $email, ':password'=> $password));
    $rowCount  = $stmt->rowCount();
   if($rowCount > 0)
   {
    // Success
    //  set session data and etc.
    return "success";
  }
  else
  {
//  Failed
    return "Error, please try again!";
  }
}
$result = register_user($email, $password);

Reference: click here

Community
  • 1
  • 1
jagad89
  • 2,603
  • 1
  • 24
  • 30
  • nice catch with the string issue found in `var_dump($_POST['data']);`. – Sean Jun 18 '15 at 17:45
  • okay so it now has the correct strings and broken up format! But I still fail every time. Thinking it has something to do with my PDO statement now. – Bryce Jun 18 '15 at 17:56
  • Have you still verified `var_dump($_POST['data']);` ? – jagad89 Jun 18 '15 at 17:57
  • yup! I verified and that problem is fixed! :D Just trying to figure out why it is always false; – Bryce Jun 18 '15 at 17:59
  • `$isSuccess = $stmt->execute(array(':email'=> $email, ':password'=> $password)); var_dump($isSuccess);` check value is true or false. – jagad89 Jun 18 '15 at 18:06
  • The results are: bool(false) – Bryce Jun 18 '15 at 18:08
  • Your statement did not executed with success. now do `var_dump($stmt->errorInfo());` after `execute` – jagad89 Jun 18 '15 at 18:11
  • 1
    fixed it! It gave an error about the table configuration and the input that was being sent! :) Thank you so much for help! – Bryce Jun 18 '15 at 18:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80919/discussion-between-jagad89-and-user1626342). – jagad89 Jun 18 '15 at 18:17
1

You are not calling that function, it's just there. You need to put

register_user($email, $password);

at the bottom of your .php page.

And even better: update your function to return true or false instead of "success" and "Error, please try again!". And then you can have the following

if(register_user($email, $password)){
    echo "success";
} else {
    echo "failure";
}
chrisjacob
  • 170
  • 12
  • $result = register_user($email, $password); is what I have at the very bottom of the page – Bryce Jun 18 '15 at 17:30
  • In the code you provided, check_login isn't a function. It's helpful to include any relevant code to your question. – chrisjacob Jun 18 '15 at 17:31
  • sorry I meant register_user() and I didn't mean to forget it. – Bryce Jun 18 '15 at 17:37
  • Thank you for that suggestion. I change my function to that! Now I get returned false no matter what. Something to do with my data according to jaga89 – Bryce Jun 18 '15 at 17:42
  • Look at this answer http://stackoverflow.com/a/3817220/3112492 You want to use parse_str on $_POST['data'] – chrisjacob Jun 18 '15 at 17:45