I don't really understand why this does not work - I have read a whole lot about this specific problem, but I must be missing something.
I want to alert the "echo" from the PHP - which it doesn't.
AJAX:
$("#SaveChangesEmail").click(function() {
var newemail = $("#mynewemail").val();
$.ajax({
type: "POST",
url: "checkemail.php",
data: {newemail:newemail},
datatype: "json",
success: function(data){
alert(data);
}
});
});
PHP (checkemail.php)
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if($email == $row['myemail']){
echo "This is your current email";
}
else if($email != $row['myemail']){
$results = $con->query("
UPDATE members SET
myemail='".$email."'
WHERE m_id = '".$m_id."'") or die(mysqli_error($con));
echo "Your email is changed";
}
} else {
echo "Please provide a correct email";
}
The database is updating, do the script itself runs perfectly, but after the "success" - it doesn't alert anything.
UPDATE
I have now tried this:
AJAX:
$("#SaveChangesEmail").click(function() {
var newemail = $("#mynewemail").val();
$.ajax({
type: "POST",
url: "checkemail.php",
data: {newemail:newemail},
datatype: "text",
success: function(data){
if(data == 1) {
alert("This is your current email");
}
else if(data == 2) {
alert("Your email is changed");
}
else if(data == 3) {
alert("Please provide a correct email");
}
}
});
});
PHP (checkemail.php)
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if($email == $row['myemail']){
echo "1";
}
else if($email != $row['myemail']){
$results = $con->query("
UPDATE members SET
myemail='".$email."'
WHERE m_id = '".$m_id."'") or die(mysqli_error($con));
echo "2";
}
} else {
echo "3";
}
The console is returning the raw data (1,2,3) but the alert is still not showing!
Related
PHP As you can see in the below PHP code I am trying the redirect the user but I am struggling to do it in jquery.
What I want is if login statement is a success login_success then the user will redirect on myprofile.php but nothing happened.
if($query)
{
$num=mysqli_fetch_array($query); //fetching all matching records
if($num > 0)
{
//if record found
$_SESSION["loggedin"] = TRUE;
$_SESSION['cid']=$num['cid'];
echo "login_success";
}
else
{
echo "invalid email or password.";
}
}
else
{
echo "something went wrong!";
}
Ajax:
$.ajax({
url:"sql/login_process.php",
method:"POST",
data:$('#login_form').serialize(),
beforeSend:function()
{
$('#login_response').html('<span class="text-info"><i class="fas fa-spinner"></i> Loading response...</span>');
},
success:function(data)
{
$('form').trigger("reset");
$('#login_response').fadeIn().html(data);
setTimeout(function(){
$('#login_response').fadeOut("slow");
}, 7000);
if(data == "login_success") location.href = "http://www.example.
On the line if(data == "login_success") location.href = "http://www.example. you need to change to if(data == "login_success") location.assign('<link to your profile page>').
I'm having trouble implementing if statement in ajax success function.
<?php
include('../Config/config.php');
$myquery = "SELECT * FROM voters WHERE Precinct = '".$_POST['precinct']."'";
$execute = mysqli_query($mysqli, $myquery);
if (mysqli_num_rows($execute) >= 1)
{
echo "Precinct is full.\n Recheck precinct number.";
}
?>
function checkerprecinct() {
var precinct = $("#precinct").val();
$.ajax({
type: "POST",
url: "precinctchecker.php",
data: "precinct=" + precinct,
success: function(data) {
console.log(data);
if (data === "") {
alert("Data is empty!");
} else {
alert(data);
}
}
});
}
I would like to use this as a validation.
I want to alert the user if the sent data contains similar data from the database.
try this code
change with your code
PHP Code:
$data = array();
if (mysqli_num_rows($execute) >= 1)
{
$data= array('code'=>100,'message'=>"Precinct is full.\n Recheck precinct number.");
//echo "Precinct is full.\n Recheck precinct number.";
}else{
$data= array('code'=>101,'message'=>"Data is empty!");
}
echo json_encode($data);
exit;
ajax code:
var data = JSON.parse(data);
if (data['code'] == 100) {
alert(data['message']);
}
I'm new to jquery, php and ajax and was writing this code with the help of a colleague.
My code:
if( name == '' ){
alert("Please enter your name");
$("#name", first).focus();
status = 'N';
}
else {
status = 'Y';
};
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
alert("Not a valid e-mail address");
$("#email", second).focus();
status = 'N';
}
else {
status = 'Y';
};
if( message == '' ){
alert("Please enter your message");
$("#message", third).focus();
status = 'N';
}
else{
status = 'Y';
};
if( status == 'Y'){
$.ajax({
type: "POST",
url: url,
data: {name:name, email:email, phone:phone, message:message},
success: function(data)
{
alert(data);
$("message_span").html(data);
// show response from the php script.
}
};
So my problem is that I want the .focus() to focus on the text area which is not filled in ascending order, which is not happening, which is why I'm using the first, second .. parameters.
Now the second problem is that the ajax is not giving me a pop up of "message sent successfully" which is there in my php code
My php code:
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$message=$_POST['message'];
$to='myemail';
$subject='Form Submition';
$message='Name: '.$name.'\n Phone: '.$phone.'\n Wrote the following messsage \n\n'.$message;
$headers='From: '.$email;
echo 'Message successfully sent';
Any help is much appreciated and I'm sorry if this question was asked before but I wasn't able to find it.
Thanks!
Here is a sample code to help you out. Try it:
$("#submit").click(function() {
var status = 'Y';
if ($("#name").val() == '') {
alert("Please enter your name");
$("#name").focus();
status = 'N';
return false;
}
/* if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
alert("Not a valid e-mail address");
$("#email").focus();
status = 'N';
}
*/
if ($("#message").val() == '') {
alert("Please enter your message");
$("#message").focus();
status = 'N';
return false;
}
if (status == 'Y') {
$.ajax({
type: "POST",
url: url,
data: {
name: name,
email: email,
phone: phone,
message: message
},
success: function(data) {
alert(data);
$("message_span").html(data);
// show response from the php script.
}
});
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' id='name' />
<input type='text' id='message' />
<button id="submit">Submit</button>
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();
}
}});
});
});
I am trying to get a form to check if a surname already exists in the database.
It seems like it is working, it checks the database and returns values, eg the number, as a string, of times the surname exists, or 'none' if they did not enter a surname.
The problem I am having is when I try to display the results.
JavaScript:
sName = $('#sName'), sInfo = $("#sInfo");
sName.blur(function() {
$.ajax({
type: "POST",
data: "sName="+$(this).attr("value"),
url: "check.php",
beforeSend: function(){
sInfo.html("<font color='blue'>Checking Surname...</font>");
},
success: function(data){
if(data == "none")
{
sInfo.html("<font color='red'>No Surname Entered</font>");
}
else if(data != "0")
{
sInfo.html("<font color='red'>Surname Exists</font>");
}
else
{
sInfo.html("<font color='green'>Surname Not Found</font>");
}
}
});
});
check.php:
//database connection stuff
extract($_REQUEST);
if(!empty($sName))
{
$sql = "SELECT sName FROM student WHERE sName='$sName'";
$rsd = mysqli_query($DBConnect, $sql) or die('Error: '.mysqli_error($DBConnect));
$msg = mysqli_num_rows($rsd);
mysqli_free_result($rsd);
mysqli_close($DBConnect);
}
else
{
$msg = "none";
}
echo $msg;
No matter what the value of data is (eg "1", "2", "0", "none") the sInfo div always displays "Surname Exists".
When I change it to display
else if(data != "0")
{
sInfo.html("<font color='red'>Surname Exists..."+data+"</font>");
}
The result is Surname Exists...0 or Surname Exists...none when it should instead display No Surname Entered or Surname Not Found...
What am I doing wrong?
thanks.
Change the line
data: "sName="+$(this).attr("value"),
to
data: {"sName":$(this).attr("value")},
jquery will format it correctly in the post body