Check if email already exist error undefined index: user_id

I’m trying to implement a validation on an email field when i want to edit a user on a modal popup. The email filed is filled with the user email and when i do the validation it says the email has already been used. So I’ve decided to filter all the emails from the database using an array and excluding the user email from the array. It seems fine if i set the user id variable manually but i can’t get the user id dynamically from the form and i wish someone can help. I believe the problem is here:

url: form.action,
type: form.method,
data: $(form).serialize(),

Many thanks

This is my html form:

<div class="modal fade" id='EditUserModal' tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
      <div class="modal-content">

        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="close">
            <span aria-hidden="true"></span>
          </button>
          <h4 class="modal-title">Modifica informazioni cliente</h4>
        </div>

        <div class="modal-body">
          <form id="formedituser" action="../controllers/ctrl_admin_user_app/ctrl_admin_update_user_details.php" method="post">

            <div class="form-body">

              <div class="row">

                  <div class="col-md-6">
                    <div class="form-group">
                        <label class="control-label">Nome Cliente
                        <span class="required"> * </span>
                        </label>
                        <div class="input-icon">
                            <i class="fa fa-id-badge"></i>       
                            <input type="text" id="firstName" class="form-control" placeholder="Inserici il nome del cliente" name="firstName">
                        </div>             
                    </div>
                  </div>

                  <div class="col-md-6">
                      <div class="form-group">
                          <label class="control-label">Cognome Cliente
                          <span class="required"> * </span>
                          </label>
                          <div class="input-icon">
                              <i class="fa fa-id-badge"></i>       
                              <input type="text" id="lastName" class="form-control" placeholder="Inserisci il cognome del cliente" name="lastName">
                          </div>             
                      </div>
                  </div>

              </div>

              <div class="row">

                  <div class="col-md-6">
                    <div class="form-group">
                        <label class="control-label">Email Cliente
                        <span class="required"> * </span>
                        </label>
                        <div class="input-icon">
                            <i class="fa fa-envelope"></i>       
                            <input type="text" id="userEmail" class="form-control" placeholder="Inserisci email cliente" name="userEmail">
                        </div>             
                    </div>
                  </div>

                  <div class="col-md-6">
                    <div class="form-group">
                        <label class="control-label">Telefono Cliente
                        <span class="required"> * </span>
                        </label>
                        <div class="input-icon">
                            <i class="fa fa-phone"></i>
                            <input type="text" id="userTel" class="form-control" placeholder="Inserisci telefono utente" name="userTel"> 
                        </div>
                    </div>
                  </div>

              </div>

              <div class="row">

                <div class="col-md-6">
                  <div class="form-group">
                      <label class="control-label">Codice Fiscale</label>
                      <div class="input-icon">                           
                          <i class="fa fa-id-card"></i>                       
                          <input type="text" id="userFiscalcode" class="form-control" placeholder="Inserisci codice fiscale" name="userFiscalcode">
                      </div>
                  </div>
                </div>

                 <div class="col-md-6">
                    <div class="form-group">
                        <label class="control-label">Data di nascita</label>
                        <div class="input-icon">   
                            <i class="fa fa-calendar"></i>
                            <input type="text" id="userBirth" class="form-control" placeholder="Inserisci la data di nascita" name="userBirth"> 
                        </div>
                    </div>
                </div>

              </div>

              <div class="row">

                <div class="col-md-6">
                  <div class="form-group">
                      <label class="control-label">Documento di identità</label>
                      <div class="input-icon">
                          <i class="fa fa-id-card"></i>
                          <input type="text" id="userDocument" class="form-control" placeholder="Inserisci documento del cliente" name="userDocument">
                      </div>
                  </div>
                </div>

                <div class="col-md-6" id="showrole">
                 <div class="form-group">
                  <label class="control-label">Ruolo Cliente
                   <span class="required"> * </span>
                  </label>
                  <select class="form-control bs-select" id="userRole" name="userRole">
                   <option value="client">Cliente</option>
                   <option value="admin">Amministratore</option>

                  </select>

                 </div>
                </div>

              </div>

            <div class="row" id="showgroup">
               <div class="col-md-6">
                 <div class="form-group">
                  <label class="control-label">User Group
                   <span class="required"> * </span>
                  </label>
                  <select class="form-control bs-select" id="userGroup" name="userGroup">

                  <?php 

                    $select_group_query="SELECT group_id, group_name FROM user_group";  
                    $run= mysqli_query($conn, $select_group_query);

                    while($row= mysqli_fetch_array($run)) {     

                        echo "<option value= '".$row['group_id']."' >" . $row['group_name'] . "</option>";

                    }

                  ?>  

                  </select>

                 </div>
                </div>
            </div>

  <input type="hidden" id="user_id" name="user_id"/>


  </div>

  <div class="form-actions">
    <button type="submit" class="btn blue" onclick='UpdateUserDetail()'>Aggiorna</button>


  </div>
  </form>

  </div>            

  </div>

  </div>
</div>

This is the php

require_once('../../../config/config.php');
$user_email = $_GET['userEmail'];
$user_id = $_GET['user_id'];
    $query = "SELECT user_email FROM users WHERE user_id NOT LIKE ? ";
    $stmt = mysqli_prepare($conn, $query);
    mysqli_stmt_bind_param($stmt, 'i', $user_id);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    while($row = mysqli_fetch_assoc($result)) {
        $times[] = $row['user_email'];
    }
    if(in_array($user_email, $times)){
        echo 'false';
    }else{
        echo 'true';
    }

this is the javascript

function GetUserDetail(id) {

    $("#user_id").val(id);

    var user_id = $('#user_id').val();

    $.ajax({  

      url:"../controllers/ctrl_admin_user_app/ctrl_admin_get_user_details.php",  
      method:"POST",  
      data:{user_id:user_id},  
      dataType:"json",  


      success: function(data){  

        console.log(data);

       $('#firstName').val(data.user_first);  
       $('#lastName').val(data.user_last);  
       $('#userEmail').val(data.user_email);  
       $('#userTel').val(data.user_telephone);  
       $('#userFiscalcode').val(data.user_fiscalcode);  
       $('#userBirth').val(moment(data.user_birth).format('DD/MM/YYYY')); 
       $('#userDocument').val(data.user_iddocument); 
       $('#userRole').val(data.user_role);
       // ricarico il campo per falo funzionare con il plugin bs-select 
       $('#userRole').selectpicker('refresh');

       // Mostro il campo user group solo pe ri clienti e non gli amministratori
       if($("#userRole").val() == "client"){
        $("#showgroup").show();
        $("#showrole").show();
       }else{
        $("#showgroup").hide();
        $("#showrole").hide();
       };

sorted out editing Jquery Validation plugin

remote: {
                    url: "../controllers/ctrl_admin_user_app/check_email_validation_existing_user.php",
                    type: "post",
                    data: {
                      user_id: function() {
                        return $( "#user_id" ).val();
                      }
                    }
                },

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.