I'm pretty new programmer. Or times ago I started with mysqli and php. But now I started the whole programming thing again.
And there is a whole new area. I use ajax to process form data. I am updating user information. Everything is going great, until when accessing email verification. There is no problem registering, or adding a user. Because the old data is not an email that is fetching from the database.
Now that the data is fetching from the database for the form, and when I try to edit it and check if the email already exists, that will also check the information which is already on this form. That is, when I check to see if the email already exists, it also checks the email in the form. Can I with ajax somehowe check if input field is changed, or what I should do? And yes, I know that I should use pdo, but first I want to learn the whole thing (ajax, javascript etc.). My english is not so very good, but I hope you get something clear.
Sorry if a different language in code brings confusion. Database connection function is not essential. There are no errors. So I'm not putting it here now.
This is the first question I ask in this forum. So be patient.
Here is html form
<div >
<div id="loytyyko-email-edit"></div>
<form method="post" id="muokkaa_kayttaja">
<div >
<div >
<label>Etunimi <!--Firstname--></label><span > *</span><span id="add-virhe-etunimi"></span>
<input type="text" name="edit_etunimi" value='<?php echo htmlspecialchars($row1['etunimi']);?>' />
</div>
<div >
<label>Sukunimi <!--Lastname--></label><span > *</span><span id="add-virhe-sukunimi"></span>
<input type="text" name="edit_sukunimi" value='<?php echo htmlspecialchars($row1['sukunimi']);?>' />
</div>
<div >
<label>Sähköposti <!--email--></label><span > *</span></span><span id="add-virhe-email"></span>
<input type="text" name="edit_email" id="edit_email" value='<?php echo htmlspecialchars($row1['email']); ?>' />
</div>
<div >
<label>Yritys <!--company--><small>(valinnainen)</small></label>
<input type="text" name="edit_yritys" value='<?php echo htmlspecialchars($row1['yritys']);?>' />
</br>
</div>
<div >
<label>Haluatko muuttaa käyttäjän ylläpitäjäksi? <!--make admin?--><small>(valinnainen)</small> </label></br>
<p></p><input type="checkbox" name="yllapito" value="1"> Kyllä, muuta ylläpitäjäksi</p>
</br>
</div>
<input type="hidden" name="kayttaja_id" value='<?php echo $row1['kayttaja_id'];?>' /> <!--user_id-->
<div >
<input type="submit" name='tallentaa' value='Tallenna' /><!--save button-->
<button type="button" data-dismiss="modal">Peruuta <!--Cancel button--></button>
</div>
</form>
</div>
</div>
</div>
</div>
Ajax
$(document).ready(function(){
$('#muokkaa_kayttaja').on("submit", function(event){
event.preventDefault();
var kayttaja_id = $("#kayttaja_id").val();
var etunimi = $("#etunimi").val();
var sukunimi = $("#sukunimi").val();
var yritys = $("#yritys").val();
var email = $("#email").val();
var yllapito = $("#yllapito").val();
//is admin checked
$('.valittu').each(function () {
var id = $(this).attr('id');
if ($('#' id).prop('checked')) {
var yllapito = $("#yllapito").val('1');
}
});
//a variable is defined as true (if there are no errors)
var isValid = true;
//firstname cannot be empty
if(etunimi === ''){
isValid = false;
$("#add-virhe-etunimi").html(' pakollinen');
}else{
$("#add-virhe-etunimi").html("");
}
//email cannot be empty
if(email === ''){
isValid = false;
$("#add-virhe-email").html(' pakollinen');
}else{
$("#add-virhe-email").html("");
}
//surname cannot be empty
if(sukunimi === ''){
isValid = false;
$("#add-virhe-sukunimi").html(' pakollinen');
}else{
$("#add-virhe-sukunimi").html("");
}
//if not mistakes, isValid is true
if(isValid === true){
$.ajax({
url:"muokkaakayttaja.php", //edit user php-file
method:"POST",
data:$('#muokkaa_kayttaja').serialize(),
success:function(data){
if (data == 'taken' ) {
$("#loytyyko-email-edit").html('<div >Sähköposti on jo käytössä toisella käyttäjällä!</div>');
return false;
}else{
$('#loytyyko-email-edit').html('');
window.location = window.location.href;
}
}
});
}
});
});
And php
//variables
$email = mysqli_real_escape_string($con, $_POST['email']);
$etunimi = mysqli_real_escape_string($con, $_POST['etunimi']);
$sukunimi = mysqli_real_escape_string($con, $_POST['sukunimi']);
$yritys = mysqli_real_escape_string($con, $_POST['yritys']);
$sukunimi = ucfirst($sukunimi);
$etunimi = ucfirst($etunimi);
$yllapitoyp = mysqli_real_escape_string($con, $_POST['yllapito']);
$kayttaja_id= mysqli_real_escape_string($con, $_POST['kayttaja_id']);
//if admin is selected then 1
if($yllapitoyp == '1')
{
$yllapito = '1';
}
//if admin is not slected then 0
else
{
$yllapito = '0';
}
//get db email
$user_check_query = "SELECT * FROM kayttaja WHERE email= '".$email."'";
$result = mysqli_query($con, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user->error)
{
echo 'taken';
return false;
}
else
{
//Update user
$tulos = mysqli_query($con,"UPDATE kayttaja SET etunimi = '".$etunimi."', sukunimi='".$sukunimi."', email = '".$email."', yritys = '".$yritys."', yllapito = '".$yllapito."' WHERE kayttaja_id = '".$kayttaja_id."'") or dIE();
if($tulos)
{
//set in session notification (Update successful)
echo $_SESSION['success'] = "<script type='text/javascript'>toastr.success('Tiedot tallennettu')</script>";
}
else
{
//set in session notification (sometihing gone wrong)
$_SESSION['error'] = "<script type='text/javascript'>toastr.error('Jotain meni vikaan, yritä uudelleen')</script>";
return false;
}
}
}
CodePudding user response:
is not a $user->error
, error checking is on mysqli_error
$user_check_query = "SELECT * FROM kayttaja WHERE email= '".$email."'";
$result = mysqli_query($con, $user_check_query);
if (!$result) {
echo "There was an error" . mysqli_error($con);
return false;
}
// this the result, when it was not empty that was indicate the query exists
$user = mysqli_fetch_assoc($result);
if (!empty($user)) {
echo "Taken";
return false;
}
CodePudding user response:
first things you need to do is add unique attribute in you database field. check this for more information https://www.mysqltutorial.org/mysql-unique-constraint/ asuming you submit the form data to the server, for checking the form value is dupplicate value or not in database u can do somthing like this .
$result = mysql_query("SELECT * FROM xxxx WHERE email = '[email protected]'", $conn);
$check_duplicate= mysql_num_rows($result);
if ($check_duplicate > 0)
{
die('This email already exists in the database');
}
i dont understand what do you mean about checking email on the form , it is checking the inputed value is an valid email or not or something else?.
and the last problem is about checking the for has changed or not , since you using jquery you can use .change event handler. for example
$( ".target" ).change(function() {
alert( "Handler for .change() called." );
});
this is called event handling triggering something when something happen for example: when key pressed ,when key up ,on click etc.
for more information check this link: https://api.jquery.com/change/
and you should know about form validation to check the value of the form is correct value.
just a suggestion if you want to learn about web developing more deeper after learning basic php and javascsript learn framework as fast as you can for php i recomend to learn is laravel. but before that you must to learn and understand about mvc architecture. it confusing if you dont understand mvc architecture. for front end react.js is recomended.
CodePudding user response:
since your problem is the email duplicate validation is still runing even you are not editing the email when updating data so the solution is dont update the email field when data is duplicate. so the logic is check the email input data and the email data in database is duplicated or not , if duplicated just update all data except email.
public function checkDuplicateEmail(){
$result = mysqli_query("SELECT * FROM xxxx WHERE email =
'[email protected]'", $conn);
$result= mysql_num_rows($result);
//return 0 if not duplicated ,retun 1 if duplicated
return $result;
}
//check if diplicated or not
if(checkDuplicateEmail()>0){
//if duplicated update data without email
mysqli_query($con,"UPDATE kayttaja SET etunimi = '".$etunimi."',
sukunimi='".$sukunimi."', yritys = '".$yritys."', yllapito =
'".$yllapito."' WHERE kayttaja_id = '".$kayttaja_id."'") or dIE();
}
else{
//if not duplicated update all data
mysqli_query($con,"UPDATE kayttaja SET etunimi = '".$etunimi."',
sukunimi='".$sukunimi."', email = '".$email."', yritys =
'".$yritys."', yllapito = '".$yllapito."' WHERE kayttaja_id =
'".$kayttaja_id."'") or dIE();
}