注意:未定义的索引:图像 - 图像不被识别为已定义的索引

时间:2016-11-29 00:49:23

标签: javascript php forms

我正在运行php和javascript脚本,我不断收到错误 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 我有两个文件,第一个是index.php包含表单和javascript代码,而第二个submit.php包含php代码 我的问题是其他索引没有问题,除了这里的图像是我的PHP代码(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";
    }
}

&GT;

这是我的表格

<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>

和我的javascript代码

<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 ...');    
        });
    });

});

我对发生的事情感到困惑,我喜欢解决方案

0 个答案:

没有答案