-4

This is my php:

header('Content-type: application/json');

try {
$conn = new PDO('mysql:host=localhost;dbname=grand_debrouillage', 'gd_user', 'gd_p455w0rd');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if (!empty($_POST['email'])) {
    $stmt = $conn->prepare('SELECT id FROM users WHERE courriel = :courriel');
    $stmt->bindParam(':courriel', $_POST['email'], PDO::PARAM_INT); // <-- Automatically sanitized by PDO
    $stmt->execute();

    if ($stmt->rowCount() == 0) {
        echo json_encode(true);  //good to register
    }
    else {
        echo json_encode(false); //already registered
    }
}
else {
    echo json_encode(false); //invalid post var
}  

} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

This is my my js

    $('#form').validate({ // initialize the plugin
    rules: {
        prenom: {required:true},
        nom: {required:true},
        email: {
            required:true,
            email:true,
            remote: {
                type: 'POST',
                url:"email_check.php"
            }       
        },

    ...
Monica
  • 1,524
  • 4
  • 27
  • 44
  • I am trying to console.log a true or false and nothing happens, doesn't print anything – Monica Dec 11 '13 at 17:14
  • This really calls for step-by-step debugging. What fails when? Does the E-Mail check ever run? (Look in the element inspector's "network" tab to see what requests are made) Also check out [How to squeeze error message out of PDO?](http://stackoverflow.com/q/3726505) – Pekka Dec 11 '13 at 17:33
  • Oh, scrap that last link, you are already catching exceptions properly. But would you see the output if something goes wrong with the query? – Pekka Dec 11 '13 at 17:33
  • @Pekka I just changed the POST to GET to make some tests and I keep getting true even though I already have that email on the db – Monica Dec 11 '13 at 17:36
  • Well, you are checking for the POST variable in the PHP code, so your query is bound to return 0 lines. You'd have to use `$_GET` instead. (And for future reference, really make sure you include that information from the start! Without it, there is no way for people to provide good information.) – Pekka Dec 11 '13 at 17:38
  • yes and I changed to GET so I can test only the php script and send the email through the url – Monica Dec 11 '13 at 17:40
  • When you change the request method to GET, you need to do the same in the PHP script. `$_POST['email']` will no longer work. You'll need to use `$_GET['email']`. – Pekka Dec 11 '13 at 17:41

2 Answers2

2

most likely it fails on console.log($_POST); which is scarcely a PHP code.

check your web-server logs for the syntax errors.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-1

So the problem was that when I set the rule I have to put the remote in double quotes.

                "remote": {
                type: 'POST',
                url: 'email_check.php',
                data: {
                    email: function() {
                        return $('#form :input[name="email"]').val();
                    }
                }                   
            } 
Monica
  • 1,524
  • 4
  • 27
  • 44
  • **No**, that _absolutely_ **cannot** be the problem. **Surrounding the method/rule names with quotes _is not mandatory_ for any of the methods/rules used by the jQuery Validate plugin; and in JavaScript, single or double quotes would not make any difference.** You most likely changed something else at the same time you added these quotes. Compare [this demo without quotes](http://jsfiddle.net/JTT5V/) to [this functionally identical demo using all kinds of mixed quotes](http://jsfiddle.net/JTT5V/2/). – Sparky Dec 11 '13 at 20:21
  • See [over a hundred accepted SO answers](http://stackoverflow.com/search?q=%5Bjquery-validate%5D+is%3Aanswer+isaccepted%3Ayes+remote) where the `remote` method is not specified within quotations of any kind. Also see [the sample code in the official documentation](http://jqueryvalidation.org/remote-method/). – Sparky Dec 11 '13 at 20:36