Topic: How to convert PHP AJAX code to Ruby/Rails
I'm converting a PHP website to Ruby/Rails. I have a form and when a User signs up he is asked for an email address among other things. I check if the email is a duplicate via AJAX. So my only field in the form which requires an AJAX check is the email field. I set up the JavaScript like so:
function checkEmail()
{
$.ajaxSetup({async:false});
$.post('duplicateEmail.php', {email: $('#email').val()},
function(data)
{
if(!data.success)
{
$('#email').addClass('emptyField');
setError('error');
setError('dupEmail');
}
else if(data.success)
{
$('#email').removeClass('emptyField');
resetError('dupEmail');
}
},
'json');
}Then in a PHP file I have the following:
$email = $_POST['email'];
$query = "SELECT id FROM users WHERE email='$email'";
$result = $mysqli->query($query);
$data['error'] = mysql_error();
if($result->num_rows == 0)
{
$result->free();
$data['success'] = true;
$data['message'] = '';
}
else
{
$result->free();
$data['success'] = false;
$data['message'] = 'This email address has already been registered.';
}
echo json_encode($data);Can anyone point me in the right direction as to where to place the Ruby code? Or am I thinking about this in the wrong way?