ajax php mysql json

  ajax, javascript, json, php

I have four files 3 PHP and 1 js, I am trying to do user registration, register.php, contains a form, which is validated through signup.php and user.php, which has class user.. everything works fine if I comment out below code my script.js file from the moment I uncomment below code doesn’t work. please guide me..especially the image path insertion to the database… Thanks

if(!response.signout){
                    setTimeout(function(){
                    $footer.html( response.message );
                    window.location = response.url;
                },5000);
            }

             $footer.html( response.message );
            
             
         }
         else if(response.error){
            $footer.html( response.message );
         }

register.php

<div class="modal fade my-modal" id="signup" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">

<div class="modal-dialog modal-dialog-scrollable" 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">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Signup for a new account</h4>
          </div>
          <div class="modal-body">
            <form class="form" method="post" action="signup.php" enctype="multipart/form-data">
              <div class="form-group">
                <label for="InputName">Full name</label>
                <input type="text" class="form-control" name="name" id="exampleInputName" placeholder="Name">
              </div>
              <div class="form-group">
                <label for="InputEmail">Email address</label>
                <input type="email" class="form-control" name="email" id="exampleInputEmail" placeholder="Email">
              </div>
              <div class="form-group">
                <label for="InputPassword">Password</label>
                <input type="password" class="form-control" name="password" id="exampleInputPassword" placeholder="Password">
              </div>
              <div class="form-group">
                <label for="InputCPassword">Confirm Password</label>
                <input type="password" class="form-control" name="cpassword" id="exampleInputCPassword" placeholder="CPassword">
              </div>    
              <div class="form-group">
                <label for="InputContact">Contact</label>
                <input type="password" class="form-control" name="contact" id="exampleInputContact" placeholder="Contact">
              </div>
              <div class="form-group">
                <label for="inputAddress">Address</label>
                <input type="text" class="form-control" id="inputAddress" placeholder="address" name="address">
            </div>
            <div class="form-row">
                <div class="form-group col-md-4">
                <label for="inputCity">City</label>
                <input type="text" class="form-control" id="inputCity" name="city">
                </div>
                <div class="form-group col-md-4">
                <label for="inputState">State</label>
                <input type="text" class="form-control" id="inputState"name="state">
                <!--<select id="inputState" class="form-control">
                    <option selected>Choose...</option>
                    <option>...</option>
                </select>-->
                </div>
                <div class="form-group col-md-4">
                <label for="inputZip">Zip</label>
                <input type="text" class="form-control" id="inputZip" name="zip">
                </div>
            </div>
                            
              <div class="form-group">
                <label for="exampleFormControlSelect2">Choose your Profession</label>
                <select class="form-control" name="prof">
                <option>Actor</option>
                <option>Singer</option>
                <option>Model</option>
                <option>Musician</option>
                </select>
            </div>
            <div class="form-group">
            <label for="exampleInputFile">File input</label>
            <input type="file" name="image" class="form-control-file" id="exampleInputFile" aria-describedby="fileHelp">
                        
            </div>
              <button type="submit" class="btn btn-primary btn-lg">Sign me Up!</button>
            </form>
          </div>
          <div class="modal-footer">
            
            
          </div>
        </div>
      </div>
    </div>

signup.php

<?php
require_once 'includes/init.php';


$status = $user->signup($_POST, $db);

if( $status === 'success'){
    echo json_encode([
        'success'=> 'success', 
        'message'=> '<p class="alert alert-success">You are signed up successfully!</p>',
        'url' => 'index.php',
        //'signout' => 1,
        
    ]);
}
/*else if( $status === 'less_name'){
    echo json_encode([
        'error'=> 'error', 
        'message'=> '<p class="alert alert-danger">Name should have atleast 3 letters!</p>',
        
    ]);
}*/
else if( $status === 'improper_name'){
    echo json_encode([
        'error'=> 'error', 
        'message'=> '<p class="alert alert-danger">invalid name!</p>',
        
    ]);
}
else if( $status === 'improper_contact'){
    echo json_encode([
        'error'=> 'error', 
        'message'=> '<p class="alert alert-danger">Conatct Field must have 10 number only!</p>',
        
    ]);
}
else if( $status === 'improper_password'){
    echo json_encode([
        'error'=> 'error', 
        'message'=> '<p class="alert alert-danger">Improper Password!</p>',
        
    ]);
}
// else if( $status === 'invalid format'){
//  echo json_encode([
//      'error'=> 'error', 
//      'message'=> '<p class="alert alert-danger">Only jpg,jpeg,pngfiles allowed!</p>',
        
//  ]);
// }

else if( $status === 'missing_fields'){
    echo json_encode([
        'error'=> 'error', 
        'message'=> '<p class="alert alert-danger">All fields mandatory!</p>',
        
    ]);
}
else if( $status === 'email_exists'){
    echo json_encode([
        'error'=> 'error', 
        'message'=> '<p class="alert alert-danger">Email address already in use!</p>',
    ]);
}
else if( $status === 'mismatch_password'){
    echo json_encode([
        'error'=> 'error', 
        'message'=> '<p class="alert alert-danger">Mismatch password and confirm password!</p>',
    ]);
}


else if( $status === 'error'){
    echo json_encode([
        'error'=> 'error', 
        'message'=> '<p class="alert alert-danger">Failed to sign you up!</p>'
    ]);
}

user.php

class User{
public function signup($user, $db){
                

        if(empty($user['name']) OR empty($user['email']) OR empty($user['password']) OR empty($user['address']) OR empty($user['city']) OR empty($user['state']) OR empty($user['zip'])){
            return 'missing_fields';
        }
        /*else if(!str_word_count($user['name']) > 2){
            return 'less_name';
         }*/
        else if(!$this->name($user['name'])){
            return 'improper_name';
         }
        else if (!preg_match("#.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#", $user['password'])){
            return "improper_password";
            } 
        
        else if($user['password'] !== $user['cpassword']){
            return 'mismatch_password';
        }
        else if($this->emailExists($user['email'], $db) ){
            return 'email_exists';
         }
         else if(!preg_match("/^[6-9][0-9]{9}$/", $user['contact'])){
            return 'improper_contact';
        }   
        
        else{   
                                
            
            $sql = "INSERT INTO `users`(`name`, `email`, `password`, `contact`, `address`, `profession`,`profile_image`,`verification_code`) VALUES(?,?,?,?,?,?,?,?)";
            $statement = $db->prepare($sql);
            
            if(is_object($statement) && isset($_FILES['image'])){    
                $imgfile=$_FILES["image"]["name"];
                $imgnewfile = $imgfile;
                move_uploaded_file($_FILES["image"]["tmp_name"],"public/images/".$imgnewfile);
                
                $hash = password_hash($user['password'], PASSWORD_DEFAULT);
                $code = generateCode();         
                
                        
                $totaladdress = $user['address'].",".$user['city'].",".$user['state'].",".$user['zip'];
                $statement->bindParam(1, $user['name'], PDO::PARAM_STR);
                $statement->bindParam(2, $user['email'], PDO::PARAM_STR);
                $statement->bindParam(3, $hash, PDO::PARAM_STR);
                $statement->bindParam(4, $user['contact'], PDO::PARAM_STR);
                $statement->bindParam(5, $totaladdress, PDO::PARAM_STR);
                $statement->bindParam(6, $user['prof'], PDO::PARAM_STR);
                $statement->bindParam(7, $imgnewfile, PDO::PARAM_STR);
                $statement->bindParam(8, $code, PDO::PARAM_STR);
                $statement->execute();


                if($statement->rowCount()){
                    return 'success';
                    
                } 


            }
        }
        return 'error';
    }
}

script.js

$(function(){
    
    $('.form').on('submit', function( e ){
        e.preventDefault();

        $form = $(this);

        submitForm($form);

    });


    $('#forgot-password').on('click', function( e ){
        e.preventDefault();

        $('#login').modal('hide');

    });

});


function submitForm($form){

    $footer = $form.parent('.modal-body').next('.modal-footer');
    
    $footer.html('<img src="public/images/ajax-loader.gif">');
        

    $.ajax({
        url: $form.attr('action'),
        method: $form.attr('method'), 
        data: $form.serialize(),
        success: function(response){
            response = $.parseJSON(response);
            
            if(response.success){
                

            if(!response.signout){
                    setTimeout(function(){
                    $footer.html( response.message );
                    window.location = response.url;
                },5000);
            }

             $footer.html( response.message );
            
             
         }
         else if(response.error){
            $footer.html( response.message );
         }

             console.log(response)

         }
     });
}

Source: Ask PHP

LEAVE A COMMENT