I have 3 php file and 1 js file… register.php has a form which in action to signup.php, where validation is done through users.php and response is displayed on form through ajax…I don’t know why this code doesn’t insert $path to the table field call image_profile… please guide me…Thanks
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">×</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="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>
user.php
<?PHP
class user{
public function signup($user, $db){
if(empty($user['name'])){
return 'missing_fields';
}else
{
$valid_extentions =array('jpeg','jpg','png');
$path= "public/images/";
if(isset($_FILES['image']) && !empty($_FILES['image'])){
$img= $_FILES['image']['name'];
$tmp= $_FILES['image']['tmp'];
$ext = strlower(pathinfo($img,PATHINFO_EXTENSION));
$final_img = rand(1000,1000000).$img;
if(in_array($ext,$valid_extentions)){
$path=$path.strtolower($final_img);
move_uploaded_file($tmp,$path);
}
//`profile_image`,
$sql = "INSERT INTO `user`(`name`,`profile_images`,`verification_code`) VALUES(?,?,?)";
$statement = $db->prepare($sql);
if(is_object($statement)){
$code = generateCode();
$imgnewfil="testing";
$imgnewfile = $filename;
$statement->bindParam(1, $user['name'], PDO::PARAM_STR);
$statement->bindParam(2, $path, PDO::PARAM_STR);
$statement->bindParam(3, $code, PDO::PARAM_STR);
$statement->execute();
if($statement->rowCount()){
return 'success';
}
}
}
}
return 'error';
}
}
$user = new User;
?>
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 === 'missing_fields'){
echo json_encode([
'error'=> 'error',
'message'=> '<p class="alert alert-danger">All fields mandatory!</p>',
]);
}else if( $status === 'error'){
echo json_encode([
'error'=> 'error',
'message'=> '<p class="alert alert-danger">Failed to sign you up!</p>'
]);
}
?>
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);
}
});
}