0

I tried to integrate krajee file-input into my existing form. DEMO SITE

When i browse a file from my computer and click upload button (built-in with the plugin), i got this error: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data.

The author of this plugin told me that i have to write valid json response in my php file for this to work but he did not have time to help individual case like mine. So I read the documentation from the website, it has this part:(you can find it on the demo site above)

Sending Data (from server)

Your server method as set in uploadUrl must send data back as a json encoded object. The only key you must send is the error which will be the error message for the upload and will help the plugin to identify error in the file upload. For example the response from server would be sent as {error: 'You are not allowed to upload such a file.'}. Note: The plugin will automatically validate and display ajax exception errors.

IMPORTANT

You MUST send a valid JSON response from your server, else the upload process will fail. Even if you do not encounter any error, you must at least send an empty JSON object {} from your server.

To trap and display a validation error, your JSON response data must include the error key, whose value will be the error HTML markup to display. This is to be setup as mentioned above.

Unfortunately, I can't understand it because I am just a new php learner and this is out of my scope. But I have my php file here, hope some expert can help me to add the json response to it as the documentaion explained above. Thank you very much in advance!

Here is my php file:

<?php
if(isset($_POST["submit"])){
require("../configs/dbconnect.php");
/*Form variable   */
$owner = mysql_real_escape_string($_POST["owner"]);
$title = mysql_real_escape_string($_POST["title"]);
$description = mysql_real_escape_string($_POST["description"]);
$city = mysql_real_escape_string($_POST["city"]);
$brand = mysql_real_escape_string($_POST["brand"]);
$marketprice = mysql_real_escape_string($_POST["marketprice"]);
$price = mysql_real_escape_string($_POST["price"]);
$phone = mysql_real_escape_string($_POST["phone"]);
/*** the upload directory ***/
$upload_dir= 'uploads';

/*** numver of files to upload ***/
$num_uploads = 5;

/*** maximum filesize allowed in bytes ***/
$max_file_size  = 5000000;

/*** the maximum filesize from php.ini ***/
$ini_max = str_replace('M', '', ini_get('upload_max_filesize'));
$upload_max = $ini_max * 1024;

/*** a message for users ***/
$msg = 'Please select files for uploading';

/*** an array to hold messages ***/
$messages = array();
$err=array();

/*** check if a file has been submitted ***/
if(isset($_FILES['file']['tmp_name']))
{
    /** loop through the array of files ***/
    for($i=0; $i < count($_FILES['file']['tmp_name']);$i++)
    {
        // check if there is a file in the array
        if(!is_uploaded_file($_FILES['file']['tmp_name'][$i]))
        {
            $messages[] = 'No file uploaded';
        }
        /*** check if the file is less then the max php.ini size ***/
        //elseif($_FILES['image']['size'][$i] > $upload_max)
        //{
        //    $messages[] = "File size exceeds $upload_max php.ini limit";
        //}
        // check the file is less than the maximum file size
        elseif($_FILES['file']['size'][$i] > $max_file_size)
        {
            $messages[] = "File size exceeds $max_file_size limit";
        }
        else
        {
            //$temp = explode(".", $_FILES["file"]["name"][$i]);
            //$extension = end($temp);
            //$name[$i] = sha1(microtime()) . "." . $extension;
            $name[$i]=$_FILES["file"]["name"][$i];
            // copy the file to the specified dir 
            if(move_uploaded_file($_FILES['file']['tmp_name'][$i],$upload_dir.'/'.$name[$i]))
            {
                /*** give praise and thanks to the php gods ***/
                $messages[] = $name[$i].' uploaded';
                $image_path[$i]=$upload_dir.'/'.$name[$i];
            }
            else
            {
                /*** an error message ***/
                $messages[] = 'Uploading '.$name[$i].' Failed';
            }
        }
    }
}
$image_path_string=serialize($image_path);
$sql = "INSERT INTO memberpost(owner, title, description, city, brand, marketprice, price, phone, image) VALUES ('$owner', '$title','$description','$city','$brand','$marketprice','$price','$phone', '" . $image_path_string . "')";
$result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
 if(sizeof($messages) != 0)
{
    foreach($messages as $err)
    {
        echo $err.'<br />';
    }
}
}
?>
Community
  • 1
  • 1
Giang Nguyen
  • 495
  • 8
  • 22
  • 1
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). – Jay Blanchard Jan 05 '15 at 20:20

1 Answers1

0

Your echo i think... Put your error on any variable then echo json_encode(variable name). That's how to send JSON object.

wbhuana
  • 845
  • 1
  • 7
  • 8