AJAX PHP error Insert data

Im currently attempting to create a upload feature using AJAX and ive run into some errors and have asked around but no one can spot it so i thought i would ask this community but what happens is you enter the details validation works perfectly but when you click upload it does not do it and i cant see why thanks

The page below is the submit page

<?php
include("../process/header.php");
include("../process/config.php");
?>
<div class="ui container">

<div class="main-form">
<h2>Upload file.</h2>
<hr/>
<form id="upload">
<label>File Name</label>
<input type="text" name="title" placeholder="File Name" class="form-control"/>
<label>File Description</label>
<input type="text" name="description" placeholder="File Description" class="form-control"/>
<label>File Image</label>
<input type="url" name="image" placeholder="File Image" class="form-control"/>
<label>File Price</label>
<input type="text" class="form-control" name="price" value="10.00" placeholder="10.00"/>
<label>Select File</label>
<input type="file" name="file"/>
<button type="submit" id="btn" class="btn btn-default">Upload</button>
<script type="text/javascript">
	$("#upload").on("submit",(function(e) {
		e.preventDefault();
		$.ajax({
        	url: "<?php echo $url;?>admin/ajax.php",
			type: "POST",
			data:  new FormData(this),
			contentType: false,
    	    cache: false,
			processData:false,
			beforeSend: function() 
        {
            $("#res").html('Uploading..Please wait!');
        },  
        success: function(response)
        {
            $("#res").html(response);
        }        
	   });
	}));
</script>
<div id="res"></div>
</form>
</div>
</div>
</body>
</html>

The page below is the ajax page

<?php
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
include("../process/config.php");
if(!preg_match('/^([1-9][0-9]*|0)(\.[0-9]{2})?$/', $_POST['price']))
{
	echo '<span class="text-danger">Invalid price value.</span>';
	die();
}
if(empty($_POST['title']))
{
	echo '<span class="text-danger">File name is required.</span>';
	die();
}
if(empty($_POST['description']))
{
	echo '<span class="text-danger">Description is required.</span>';
	die();
}
if(empty($_POST['image']))
{
	echo '<span class="text-danger">Image Link is required.</span>';
	die();
}
$file_title = mysqli_real_escape_string($conn, $_POST['title']);
$description = mysqli_real_escape_string($conn, $_POST['description']);
$image = mysqli_real_escape_string($conn, $_POST['image']);
$price = $_POST['price'];
if(isset($_FILES['file'])){
	$file_name = $_FILES['file']['name'];
	$file_name = preg_replace("/[^a-zA-Z0-9áéíóúüñÁÉÍÓÚÜÑ.\']/", "", $file_name);
	$tmp_name   = $_FILES['file']['tmp_name'];
	$file_size = $_FILES['file']['size'];
	if($file_size < 1)
	{
		echo '<span class="text-danger">No file selected.</span>';
		die();
	}
	$file_type = pathinfo($file_name);
	$file_type = $file_type['extension'];
	$allowed_file = array('zip');
	if(!in_array($file_type, $allowed_file)){
		echo '<span class="text-danger">Please select a .zip file.</span>';
	}
	else
	{
		$new_file_name = time().$file_name;
		if(move_uploaded_file(''.$tmp_name.'', 'assets/files/'.$new_file_name.''))
		{
			$sql_upload = "INSERT INTO files (title, description, image, file, price) VALUES ('$file_title', '$description', '$image', '$new_file_name', '$price')";
			if($conn->query($sql_upload) === TRUE)
			{
				echo '<span class="text-success">File added for selling.</span>';
			}
		}
	}
}
else {
	echo '<span class="text-danger"> No file selected.</span>';
}
}
else {
	header('location: ../index.php');
}
?>

Just a 10 second overview, you might need to set the content type of the form.

<form enctype="multipart/form-data">

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