I am running php and javascript scripts and i keep getting errors like
Notice: Undefined index: image in C:\xampp\htdocs\dot\panel2\local\submit.php on line 17
Notice: Undefined index: in C:\xampp\htdocs\dot\panel2\local\submit.php on line 18
I have two files, the first which is index.php contains the form and the javascript code while the second submit.php contains the php code
My problem is that other indexes do not have the issue except the image here is my php code(submit.php)
<?php
include_once 'dbconfig.php';
if(isset( $_POST ))
{
$name = mysql_real_escape_string($_POST['name']);
$title = mysql_real_escape_string($_POST['title']);
$link = mysql_real_escape_string($_POST['link']);
$cat = mysql_real_escape_string($_POST['category']);
$date = date("Y-m-d h:i:s");
$name = trim($name);
$title = trim($title);
$link = trim($link);
$cat = trim($cat);
$image = (mysql_real_escape_string($_POST['image'])) ? mysql_real_escape_string($_POST['image']) : '';
$file = rand(1000,100000)."-".$_FILES[$image]['name'];
$file_loc = $_FILES[$image]['tmp_name'];
$file_size = $_FILES[$image]['size'];
$file_type = $_FILES[$image]['type'];
$folder="div1/";
// new file size in KB
$new_size = $file_size/1024;
// new file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case
$final_file=str_replace(' ','-',$new_file_name);
mysql_connect("localhost","root","");
mysql_select_db("local") or die(mysql_error());
$imageName = mysql_real_escape_string($_FILES[$image]["name"]);
$imageData = mysql_real_escape_string(file_get_contents($_FILES[$image]["tmp_name"]));
$imageType = mysql_real_escape_string($_FILES[$image]["type"]);
if(move_uploaded_file($file_loc,$folder.$final_file))
{
if(substr($imageType,0,5) == $image)
{
mysql_query("INSERT INTO first_segment(id,p_name,name,image,title,link,profile_id,category,date_time) VALUES('','$name','','$image','$title','$link','1','$cat','$date')");
echo "image uploaded";
}
else
{
echo "Only images are allowed";
}
}
?>
and here is my form
<form id="news-form" class="form-horizontal form-label-left" target="_self" action="upload1.php#step-2" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Your Name: <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input name="name" type="text" id="name" size="100" required="required" class="form-control col-md-7 col-xs-12"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="image">News image: <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input name="image" type="file" id="image" required="required" class="form-control col-md-7 col-xs-12"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="title">Title of the news: <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<textarea name="title" cols="100" rows="5" id="title" required="required" class="form-control col-md-7 col-xs-12" placeholder="for example: I've nothing to do with Avengers - Jonathan cries out "></textarea>
</div>
</div>
<div class="form-group">
<label for="link" class="control-label col-md-3 col-sm-3 col-xs-12">The link to the news: <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input name="link" type="text" id="link" size="100" required="required" class="form-control col-md-7 col-xs-12" placeholder="for example: /local/2016/Nothing-to-do-with-avengers-says-president-jonathan.php"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">News category: <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input name="category" type="text" id="category" size="100" required="required" class="form-control col-md-7 col-xs-12" placeholder="for example: headline, article etc"/>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="submit" name="submit" value="upload" required="required" class="btn btn-default" style="float:right;" />
</div>
</div>
</form>
and my javascript code
<script type="text/javascript">
$(document).ready(function() {
// submit form using $.ajax() method
$('#news-form').submit(function(e){
e.preventDefault(); // Prevent Default Submission
$.ajax({
url: 'submit.php',
type: 'POST',
data: new FormData(this), // it will serialize the form data
contentType: false,
cache: false,
processData:false,
})
.done(function(data){
$('#st').fadeOut('slow', function(){
$('#st').fadeIn('slow').html(data);
});
})
.fail(function(){
alert('Ajax Submit Failed ...');
});
});
});
I am really confused at what is happening and i'd love solutions