I want to upload a pdf file from user input into my mysql database. I have supplied code that shows a users registration form with the pdf upload input type. I want to post this via AJAX to my php script to be entered into my database. I cannot get this code working. Any suggestions would be appreciated?
Snippet of form input -
<form id='form1'>
<label>Are you a:</label>
<select name="type" id="type">
<option>Select...</option>
<option value="teacher">Teacher</option>
<option value="school">School</option>
</select><br>
<label>Teaching Council Number / School Roll
Number</label>
<input type="text" name="userNo" id="userNo"
value="" required><br>
<label>Full Name / School Name</label>
<input type="text" name="name" id="name" required>
<br>
<label>Level:</label>
<select name="level" id="level">
<option>Select...</option>
<option value="primary">Primary</option>
<option
value="secondary">Secondary</option>
</select><br>
<label>Phone Number</label>
<input type="number" name="phoneNo" id="phoneNo"
data-clear-btn="false" pattern="[0-9]*" value=""><br>
<label>Address</label>
<input type="text" name="location" id="location"
placeholder="" required><br>
<label>Email</label>
<input type="email" id="email" data-clear-
btn="false" required><br>
<label for="password">Password</label>
<input type="password" name="password"
id="password" value="" required><br>
<label for="password">Confirm Password</label>
<input name="password_confirm" required="required"
type="password" id="password_confirm" oninput="check(this)" /><br>
<label for="file">Curriculum Vitae <i>(PDF only)
</i></label>
<input type="file" name="file" id="cv" value="cv"
accept=".pdf"><br>
<label for="file">Garda Vetting <i>(PDF only)</i>
</label>
<input type="file" name="gardavetting"
id="gardavetting" value="gardavetting" accept=".pdf"><br>
<label>LinkedIn URL <i>(optional)</i></label>
<input type="text" id="linkedin" name="linkedin">
<br>
<button type="submit" id="submit1" name="submit1"
onclick="myFunction1();">Submit</button><br>
</form>
</div>
PHP -
<?php
// Selecting Database
include_once 'dbh.php';
//Here we fetch the data from the URL that was passed from our HTML
form
$type2 = $_POST['type'];
$userNo2 = $_POST['userNo'];
$name2 = $_POST['name'];
$level2 = $_POST['level'];
$phoneNo2 = $_POST['phoneNo'];
$location2 = $_POST['location'];
$email2 = $_POST['email'];
$password2 = $_POST['password'];
$cv2 = $_POST['cv'];
$gardavetting2 = $_POST['gardavetting'];
$linkedin2 = $_POST['linkedin'];
$sql = "INSERT INTO users (type, userNo, name, level, phoneNo,
location, email, password, cv, gardavetting, linkedin) VALUES
('$type2', '$userNo2', '$name2',
'$level2','$phoneNo2','$location2','$email2','$password2','$cv2','$gardav etting2','$linkedin2');";
mysqli_query($conn, $sql);
?>
JS -
function myFunction1() {
var type = document.getElementById("type").value;
var userNo = document.getElementById("userNo").value;
var name = document.getElementById("name").value;
var level = document.getElementById("level").value;
var phoneNo = document.getElementById("phoneNo").value;
var location = document.getElementById("location").value;
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
var cv = document.getElementById("cv").value;
var gardavetting = document.getElementById("gardavetting").value;
var linkedin = document.getElementById("linkedin").value;
var dataString = '&type=' + type + '&userNo=' + userNo + '&name=' +
name + '&level=' + level + '&phoneNo=' + phoneNo + '&location=' +
location + '&email=' + email+ '&password=' + password + '&cv=' + cv +
'&gardavetting=' + gardavetting + '&linkedin=' + linkedin ;
if ( type== '' || userNo == '' || name == '' || level == '' ||
phoneNo == '' || location == '' || email == '' || password == '' || cv
== '' || gardavetting == '')
{
alert("Please Fill All Fields");
}
else
{
//AJAX code to submit form.
$.ajax({
type: "POST",
url: "http://localhost:8888/EduSubOct/signup.php",
data: dataString,
cache: false,
success: function(html) {
alert("Information Entered Successfully");
}
});
}
return false;