Guest User

Untitled

a guest
Jan 15th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. <script type="text/javascript">
  2. function preview(input){
  3. if(input.files && input.files[0]){
  4. var reader = new FileReader();
  5. reader.onload = function(e){
  6. $('#show')
  7. .attr('src', e.target.result)
  8. .width(200)
  9. .height(200);
  10. }
  11. reader.readAsDataURL(input.files[0]);
  12. }
  13. }
  14. $("#prof_pic").change(function(){
  15. preview(this);
  16. });
  17. $(document).ready(function(e) {
  18. $('.err').hide();
  19. $('#add_student').submit(function(e) {
  20. e.preventDefault();
  21. $('.err').hide();
  22. prof_pic = $('#prof_pic')[0].files[0];
  23. name = $('#name').val();
  24. pName = $('#pName').val();
  25. month = $('#month').val();
  26. day = $('#day').val();
  27. year = $('#year').val();
  28. date = month + '/' + day +'/' + year;
  29. level = $('#class').val();
  30. reg_num = $('#reg_num').val();
  31. username = $('#username').val();
  32. pass1 = $('#pass1').val();
  33. pass2 = $('#pass2').val();
  34. pass = '';
  35. if(name == ''){
  36. $('#n_err').fadeIn();
  37. $('#name').focus();
  38. return false;
  39. }
  40. if(pName == ''){
  41. $('#pn_err').fadeIn();
  42. $('#pName').focus();
  43. return false;
  44. }
  45. if (month == "Month") {
  46. $('#month').css('border-color','red');
  47. $('#month').focus();
  48. return false;
  49. }
  50. if (day == "Day") {
  51. $('#day').css('border-color','red');
  52. $('#day').focus();
  53. return false;
  54. }
  55. if (year == "Year") {
  56. $('#year').css('border-color','red');
  57. $('#year').focus();
  58. return false;
  59. }
  60. if (level == 'Class') {
  61. $('#class').css('border-color','red');
  62. $('#class').focus();
  63. return false;
  64. }
  65. if (reg_num == '') {
  66. $('#reg_num').css('border-color','red');
  67. $('#reg_num').focus();
  68. return false;
  69. }
  70. if(username == ''){
  71. $('#user_err').fadeIn();
  72. $('#username').focus();
  73. return false;
  74. }
  75. if (pass1 == '') {
  76. $('#pass1_err').fadeIn();
  77. $('#pass1').focus();
  78. return false;
  79. }
  80. if (pass1 != pass2) {
  81. $('#pmis_err').fadeIn();
  82. $('#pass1').focus();
  83. return false;
  84. }
  85. else{
  86. pass = pass1;
  87. }
  88. var student = new FormData();
  89. student.append('prof_pic',prof_pic);
  90. student.append('name',name);
  91. student.append('pName',pName);
  92. student.append('month',month);
  93. student.append('day',day);
  94. student.append('year',year);
  95. student.append('level',level);
  96. student.append('reg_num',reg_num);
  97. student.append('username',username);
  98. student.append('pass',pass);
  99.  
  100. $.ajax({
  101. type : 'POST',
  102. url : 'student_process.php',
  103. data : student,
  104. processData: false,
  105. contentType: false,
  106. success: function(response){
  107. if(response == 'inserted'){
  108. window.alert("Student's Info Inserted into Database" );
  109. }
  110. else{
  111. window.alert("Oops! Students data Could Not be Inserted");
  112. }
  113. }
  114. })
  115. });
  116. });
  117. </script>
  118.  
  119. <?php
  120. function test_input($data) {
  121. $data = trim($data);
  122. $data = stripslashes($data);
  123. $data = htmlspecialchars($data);
  124. return $data;
  125. }
  126. include('config.php');
  127. $conn = mysqli_connect(DB_DSN,DB_USERNAME,DB_PASSWORD,'users');
  128. $prof_pic = $_FILES['prof_pic']['name'];
  129. $temp_prof_pic = $_FILES['prof_pic']['tmp_name'];
  130. $folder = '../pics/students';
  131. $prof_pic_dir = $folder.'/'.$prof_pic;
  132. $upload = move_uploaded_file($temp_prof_pic, $prof_pic_dir);
  133. $name = test_input($_POST['name']);
  134. $pName = test_input($_POST['pName']);
  135. $month = $_POST['month'];
  136. $day = $_POST['day'];
  137. $year = $_POST['year'];
  138. $date = $month." ".$day." ".$year;
  139. $class = $_POST['level'];
  140. $reg_num = $_POST['reg_num'];
  141. $reg_num = "GKQC".$reg_num;
  142. $username = test_input($_POST['username']);
  143. $password = test_input($_POST['pass']);
  144. $enc_password = hash("sha512",$password);
  145. $insert = mysqli_query($conn,"INSERT INTO student (name, parent_name, dob, class, reg_num, prof_pic, username, password) VALUES ('$name', '$pName', '$date', '$class','$reg_num','$prof_pic_dir', '$username', '$enc_password')");
  146. if ($upload && $insert) {
  147. echo "inserted";
  148. }
  149. else{
  150. echo "error";;
  151. }
  152. mysqli_close($conn);
Add Comment
Please, Sign In to add comment