Ajax not working

Solved
Mephistole Posted messages 55 Registration date   Status Membre Last intervention   -  
Mephistole Posted messages 55 Registration date   Status Membre Last intervention   -
Hello,

I am using Ajax so that a person can only register on my site if the email address they provide is not already in the database.

Here is my controller:
 if (isset($_POST['checkMail'])) { session_start(); include_once '../models/dataBase.php'; include_once '../models/user.php'; $mailUnique = new user(); $mailUnique->mail = strip_tags($_POST['checkMail']); $checkMail = $mailUnique->checkMailUnique(); if ($checkMail !== false) { if ($checkMail == 1) { $_SESSION['formError'] = true; } echo json_encode($checkMail); } } 


Here is my JS script:
 $('#errorCheckMailUnique').css('display', 'none'); function checkMailUnique() { $.post('../../controllers/addUser-Controller.php', { checkMail: $('#mail').val() }, // We get the results from the controller function (checkMailResult) { // If we find an email address in the database, then we display an error message if (checkMailResult === 1) { $('#errorCheckMailUnique').css('display', 'block'); $('#submitRegistrer').attr('disabled', 'disabled'); alert('1'); } else if (checkMailResult === 0) { $('#errorCheckMailUnique').css('display', 'none'); $('#submitRegistrer').removeAttr('disabled', 'disabled'); alert('2'); } }, 'JSON' ); }; 


Here is my model:
 public function checkMailUnique() { $queryCheckMail = 'SELECT COUNT(`mail`) AS countMail ' . 'FROM `'.self::prefix.'user`' . 'WHERE `mail` = :mail'; $checkMailUnique = $this->db->prepare($queryCheckMail); PDO:: is a constant $checkMailUnique->bindValue(':mail', $this->mail, PDO::PARAM_STR); if ($checkMailUnique->execute()) { $checkMailUniqueResult = $checkMailUnique->fetch(PDO::FETCH_OBJ); return $checkMailUniqueResult->countMail; } else { return false; } } 


EDIT: Code tag correction (Added language for syntax highlighting!)(jordane)

My JS script doesn't seem to be loading since I added the following code:
$('button').css('background-color', 'red');
this line of code doesn't work. Yet this same line in another JS script works. However, I have correctly added
<script src="assets/js/checkMailUnique.js" type="text/javascript"></script>
in the <head> tag.

1 réponse

jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   4 830
 
Hello,

When working with Javascript (and/or ajax), you should always start by checking the CONSOLE of the browser for errors.
(for ajax, remember to enable the option in certain browsers like Chrome (xhr))
(personally, for debugging, I prefer using Firefox...)

Anyway, one of your lines of code seems strange to me.....
 $.post(de donnée ou non '../../controllers/addUser-Controller.php', {


PS: In the future, please specify the LANGUAGE in the code tags to get syntax highlighting and proper code indentation (I edited your message to include them).
Explanations available here: https://codes-sources.commentcamarche.net/faq/10686-le-nouveau-codes-sources-comment-ca-marche#balises-code

--
Best regards,
Jordane
0
Mephistole Posted messages 55 Registration date   Status Membre Last intervention   1
 
Thank you for your response :).

I solved the problem.

I thought I had specified the language in the tags. I must have made a manipulation error.
0
Mephistole Posted messages 55 Registration date   Status Membre Last intervention   1
 
Thank you for your response :)
I resolved the issue.

It seemed to me that I specified the language in the tags. I must have made a manipulation error.
0