function checkAvailability() {
$("#loaderIcon").show();
jQuery.ajax({
url: "check_availability.php",
data:'email='+$("#email").val(),
type: "POST",
success:function(data){
$("#user-availability-status").html(data);
$("#loaderIcon").hide();
},
error:function (){}
});
}
the code is working fine.......but if email already exist in database then form should not be submitted...i m new to java script what to do ?
<?php
include('conection.php');
if(!empty($_POST["email"])) {
$result = mysql_query("SELECT * FROM user WHERE email='" . $_POST["email"]
. "'");
$row = mysql_fetch_row($result);
$user_count = $row[0];
if($user_count>0) {
echo "<span class='status-not-available'> Email Already
Registered</span>";
}else{
echo "<span class='status-available'> Email Available.</span>";
}
}
?>
Related
I have a webapplication where I push data to a table (User_Info) which includes a random generated token and Sur- and Lastname. Now when I type in the cordova application the token it gets sends to a table named (User_Token). Now I want to check if that token in User_Token matches the generated token in User_Info. If they match I want to show that specific data.
This is my cordova html:
<input type="text" class="valueToken" name="usertoken" >
<button type="submit" class="postToken" id="submit" />PostToken </button>
This is my cordova js:
$('.postToken').click(function(){
console.log($('.valueToken').val());
var tokenValue = $('.valueToken').val();
$.ajax({
type: "POST",
url: "http://example.com/fysioWebapp/php/get_token.php",
data: { dataString: tokenValue },
cache: false,
success: function(){
alert("Order Submitted");
}
});
});
and this is the php on server side:
<?php
include("connect.php");
$stringData = $_POST['dataString'];
echo $stringData;
ini_set('display_errors', 1);
$insertToken = "INSERT INTO User_Token(Oefening_Token) VALUES ('$stringData')";
$tokenresult = mysqli_query($conn, $insertToken);
if($tokenresult){
echo "Successful";
}else {
echo "Error description: " . (mysqli_error($conn));
}
?>
So how can I achieve to check if it matches after that button press and if it matches show data.
This is btw the ajax get call:
$.ajax({
url: "http://example.com/fysioWebapp/php/get_schema.php",
type: "GET",
success: function(data) {
/* ...use the data to fill in some HTML elements... */
$('#myDiv').html(data);
},
error: function() {
/* ...show an error... */
}
});
and this the php to get data from that table:
<?php
include("connect.php");
$sqlGetSchema = "SELECT * FROM `Oefeningen`";
$result = $conn->query($sqlGetSchema);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$token = $row["Token"];
$surname = $row['Surname'];
$lastname = $row['Lastname'];
echo "<div class='". $token . "'><p>Token: " . $token . "</p>";
echo "<p>Surname: " . $surname ." Lastname:" . $lastname. "</p>";
}
} else {
echo("Error description: " . mysqli_error($conn));
}
$conn->close();
?>
So basically what I want to achieve is to compare the input value from cordova app in table User_Token with token in the other table. When it matches it has to show all content of matched token row.
I have problem .i am making ajax call to a php file that will check credentials, if true i want to redirect to page , but if false i want to show error. I have little bit problem . Here is my code ajax Code.
function Login(val1, val2) {
alert(val1 + "\n" + val2);
$.ajax({
type: "POST",
url: "login.php",
data: 'username='+val1+'&password='+val2,
success: function(data) {
alert(data);
if(data=="true"){
window.location.replace('./admin/index.php');
}else
$("#Area").html(data);
}
});
and this is my php code.
<?php
require_once '/admin/DbHelper.php';
if (isset($_POST['password'])) {
$pass = $_POST['password'];
$name = $_POST['username'];
$query = "select * from user_login where userName='$name' and userPass='$pass'";
$result = mysqli_query($link, $query);
$row= mysqli_fetch_array($result);
if ($row > 0) {
$_SESSION['userName'] = $name;
echo "true"; } else {
echo '<label style="color:red"> Invalid User Name or Passowrd !</label>';
}
}
i can't understand where i should set session. please help me.
I've got an AJAX script that lets the user login on the login page however the script seems to stop after it runs the beforeSend.
<script>
$(document).ready(function()
{
$('#login').click(function() {
var username=$("#username").val();
var password=$("#password").val();
var dataString = 'username='+username+'&password='+password;
if($.trim(username).length>0 && $.trim(password).length>0) {
$.ajax({
type: "POST",
url: "login.php",
data: dataString,
cache: false,
beforeSend: function(){
$("#login").val('Connecting...');
},
success: function(data){
if(data) {
$("body").load("<?php echo $dom; ?>").hide().fadeIn(1500).delay(6000);
window.location.href = "<?php echo $dom; ?>";
} else {
$('#shakeme').shake();
$("#login").val('Login')
$("#error").html("<span style='color:#cc0000'>Error:</span> Invalid username and password. ");
}
}
});
} return false;
});
});
</script>
The AJAX script sends the data to login.php
if(isset($_POST['username']) && isset($_POST['password'])) {
$username=clean($_POST['username']);
$password=clean($_POST['password']));
$sql = "SELECT password FROM user WHERE username='" . $username . "'";
$result = $conn->query($sql)->fetch_assoc();
$pass = $result['password'];
$salt = getSalt($pass);
$password = $salt . $password;
$password = $salt . hash('sha256', $password);
if(strcmp($pass,$password)==0) {
$_SESSION['login_user']=$username;
}
}
Am I doing something wrong with the PHP script? Am I meant to return data somehow? This is my first time using AJAX.
You need to return values from your login.php file as
login.php
if(strcmp($pass,$password)==0) {
$_SESSION['login_user']=$username;
echo true;
}else{
echo false;
}
exit;
and within js
success: function(data){
if (data == true) {
$("body") . load("<?php echo $dom; ?>") . hide() . fadeIn(1500) . delay(6000);
window.location.href = "<?php echo $dom; ?>";
} else {
$('#shakeme') . shake();
$("#login").val('Login')
$("#error") . html("<span style='color:#cc0000'>Error:</span> Invalid username and password. ");
}
}
Apologies if this is a repeat question, but any answer I have found on here hasn't worked me. I am trying to create a simple login feature for a website which uses an AJAX call to PHP which should return JSON. I have the following PHP:
<?php
include("dbconnect.php");
header('Content-type: application/json');
$numrows=0;
$password=$_POST['password'];
$username=$_POST['username'];
$query="select fname, lname, memcat from members where (password='$password' && username='$username')";
$link = mysql_query($query);
if (!$link) {
echo 3;
die();
}
$numrows=mysql_num_rows($link);
if ($numrows>0){ // authentication is successfull
$rows = array();
while($r = mysql_fetch_assoc($link)) {
$json[] = $r;
}
echo json_encode($json);
} else {
echo 3; // authentication was unsuccessfull
}
?>
AJAX call:
$( ".LogIn" ).live("click", function(){
console.log("LogIn button clicked.")
var username=$("#username").val();
var password=$("#password").val();
var dataString = 'username='+username+'&password='+password;
$.ajax({
type: "POST",
url: "scripts/sendLogDetails.php",
data: dataString,
dataType: "JSON",
success: function(data){
if (data == '3') {
alert("Invalid log in details - please try again.");
}
else {
sessionStorage['username']=$('#username').val();
sessionStorage['user'] = data.fname + " " + data.lname;
sessionStorage['memcat'] = data.memcat;
storage=sessionStorage.user;
alert(data.fname);
window.location="/awt-cw1/index.html";
}
}
});
}
As I say, whenever I run this the values from "data" are undefined. Any idea where I have gone wrong?
Many thanks.
hi i am working on an authentification page , so my code is the following
$(document).ready(function(){
var form = $("#connexion");
var login =$("#logins");
var password=$("#passe");
$("#go").click(function(e){
e.preventDefault();
$.ajax({type: "POST",
url: "check_con.php",
data: { email:login.val() , password:password.val() },
success:function(result){
if(result == 'true')
{
alert(result);
}
}});
});
});
i get the form , the login and password and i pass them to my php script .
<?php
//data connection file
//require "config.php";
require "connexion.php";
extract($_REQUEST);
$pass=crypt($password);
$sql = "select * from Compte where email='$email'";
$rsd = mysql_query($sql);
$msg = mysql_num_rows($rsd); //returns 0 if not already exist
$row = mysql_fetch_row($rsd);
if($msg == 0)
{
echo"false1";
}
else if($row[1] == crypt($password,$row[1]))
{
echo"true";
}
else
{
echo"false2";
}
?>
everything is goood , when i give the good email and password i get true otherwise i get false, that's not the problem , the problem is i am trying to redirect the user to another page called espace.php if the result is true so i've tried this .
$(document).ready(function(){
var form = $("#connexion");
var login =$("#logins");
var password=$("#passe");
$("#go").click(function(e){
$.ajax({type: "POST",
url: "check_con.php",
data: { email:login.val() , password:password.val() },
success:function(result){
if(result == 'true')
{
form.submit(true);
}
else form.submit(false);
}});
});
});
now even if the login and password are not correct the form is submitted , how could i manage to do that i mean , if the informations are correct i go to another page , otherwise i stay in the same page.
use json to get result from authanication page
<?php
//data connection file
//require "config.php";
require "connexion.php";
extract($_REQUEST);
$pass=crypt($password);
$sql = "select * from Compte where email='$email'";
$rsd = mysql_query($sql);
$msg = mysql_num_rows($rsd); //returns 0 if not already exist
$row = mysql_fetch_row($rsd);
$result = array();
if($msg == 0)
{
$result['error'] = "Fail";
}
else if($row[1] == crypt($password,$row[1]))
{
$result['success'] = "success";
}
else
{
$result['error'] = "try again";
}
echo json_encode($result); die;
?>
And in the ajax,, check what is the response.
$(document).ready(function(){
var form = $("#connexion");
var login =$("#logins");
var password=$("#passe");
$("#go").click(function(e){
$.ajax({type: "POST",
url: "check_con.php",
data: { email:login.val() , password:password.val() },
success:function(result){
var response = JSON.parse(result);
if(response.error){
//here provide a error msg to user.
alert(response.error);
}
if(response.success){
form.submit();
}
}});
});
});