-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddimage.php
74 lines (63 loc) · 2.64 KB
/
addimage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
include_once 'scripts/functions/main.php';
include_once 'scripts/functions/spotinfo.php';
$functions = new Main();
//get id of spot that is being edited
$id = $_GET['id'];
//get save spot details
$spot = new Spot($id);
//put details into variable
$name = $spot->getName();
$caption = $spot->getCaption();
$image_path = $spot->getImagePath();
//if there is not image use place holder
if ($image_path == null) $image_path = 'images/layout/image-preview.png';
else $image_path = substr($image_path, 6);
?>
Name: <input type="text" name="name" id="name" value="<?php echo $name; ?>"/><br />
<image id="image-preview" src="<?php echo $image_path; ?>" alt="image preview" /><br />
Caption: <textarea name="caption" id="caption"><?php echo $caption; ?></textarea>
<div id="submit">
<button id="submit-button">Save</button>
</div>
<input type="file" name="image" id="userimage" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
//This replaces the file input with something nicer
document.querySelector('#image-preview').addEventListener('click', function(e) {
// Use the native click() of the file input.
document.querySelector('#userimage').click();
}, false);
var randomName = Math.floor(Math.random()*10000000001);
var response = '';
//shows preview of image
$('#userimage').change(function(){
var imageToUpload = document.getElementById("userimage").files[0];
var formData = new FormData();
formData.append("file", imageToUpload);
formData.append("newname", randomName);
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "scripts/functions/upload.php", false);
xmlhttp.send(formData);
randomName = xmlhttp.responseXML.getElementsByTagName('filename')[0].childNodes[0].nodeValue;
document.getElementById("image-preview").setAttribute("src", "images/temporary/"+randomName);
$('#submit-button').show();
});
//uploads all spot information
$('#submit-button').on('click', function() {
document.getElementById("name").innerHTML = randomName;
var submitName = $('#name').val();
var caption = $('#caption').val();
var submitData = new FormData();
submitData.append("id", "<?php echo $id ?>");
submitData.append("spotname", submitName);
submitData.append("caption", caption);
submitData.append("filename", randomName);
var submitHttp = new XMLHttpRequest();
submitHttp.open("POST", "scripts/functions/submit.php", false);
submitHttp.send(submitData);
$("#lightbox, #lightbox-panel").fadeOut(300);
});
});
</script>