AjaxForm, update html not working - php

I have a form to upload photos. After uploading the picture is displayed in the UL LI. The problem, if I bring up another image, the previous image is deleted and the new image is displayed only.
Would appreciate help
index.php:
<form name="multiple_upload_form" id="multiple_upload_form" enctype="multipart/form-data" action="image_upload.php">
<input type="hidden" name="image_form_submit" value="1"/>
<label>Add new</label>
<input type="file" name="images[]" id="images" multiple >
<div class="uploading none">
<label> </label>
<img src="images/uploading.gif"/>
</div>
</form>
<div class="gallery" id="images_preview">
<ul class="reorder_ul reorder-photos-list">
</ul>
</div>
JS:
$(document).ready(function(){
$('#images').on('change',function(){
$('#multiple_upload_form').ajaxForm({
target:'#images_preview ul',
beforeSubmit:function(e){
$('.uploading').show();
},
success:function(html){
$('.uploading').hide();
},
error:function(e){
}
}).submit();
});
});
image_upload.php:
<?php
if($_POST['image_form_submit'] == 1)
{
$images_arr = array();
foreach($_FILES['images']['name'] as $key=>$val){
$image_name = $_FILES['images']['name'][$key];
$tmp_name = $_FILES['images']['tmp_name'][$key];
$size = $_FILES['images']['size'][$key];
$type = $_FILES['images']['type'][$key];
$error = $_FILES['images']['error'][$key];
$target_dir = "../uploads/";
$target_file = $target_dir.time().$_FILES['images']['name'][$key];
if(move_uploaded_file($_FILES['images']['tmp_name'][$key],$target_file)){
$images_arr[] = $target_file;
}
//$extra_info = getimagesize($_FILES['images']['tmp_name'][$key]);
//$images_arr[] = "data:" . $extra_info["mime"] . ";base64," . base64_encode(file_get_contents($_FILES['images']['tmp_name'][$key]));
}
//Generate images view
if(!empty($images_arr)){ $count=0;
foreach($images_arr as $image_src){ $count++?>
<li id="image_li_<?php echo $count; ?>" class="ui-sortable-handle">
<img src="<?php echo $image_src; ?>" alt="">
</li>
<?php }
}
}
?>

You could save the old content in a variable before it is replaced by the response:
`
var old;
$('#images').on('change',function(){
old = $('#images_preview ul').html();
$('#multiple_upload_form').ajaxForm({
[...]
success: function(){
$('.uploading').hide();
$('#images_preview ul').append(old);
`

Related

Form not sending any data when submitting form : $_POST is empty

I'm trying to submit a simple form with video files in it, a title and a description. My code works on my laptop that has a 8.1.9 php version and not working on my desktop that has 8.1.0 php version.
I checked this page : Form submit is not sending any POST data but I didn't find any solutions.
I checked php.ini but everything was set normaly... I checked for overlappings tags but didn't find any...
I hope that this is not to obvious...:/
Here is my code :
add-videos.php
<?php
require_once './partials/header.php';
include_once './fetch-videos.php';
?>
<h2>Modal Example</h2>
<!-- Trigger/Open The Modal -->
<?php if (isset($_SESSION['user_is_admin'])) : ?>
<button id="myBtn">Ajouter une vidéo</button>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<form action="add-videos-logic.php" method="POST" enctype="multipart/form-data">
<label for="video">Fichier vidéo</label>
<input type="file" name="video" /> <br><br>
<label for="title">Titre de la vidéo</label>
<input type="text" name="title" /> <br><br>
<label for="description">Description</label>
<input type="text" name="description" /> <br><br>
<input type="submit" name="submit" value="envoyer">
</form>
</div>
</div>
<?php endif ?>
<?php while ($fetch = mysqli_fetch_array($query)) { ?>
<h2> <?= $fetch['title'] ?> </h2>
<p><?= $fetch['description'] ?></p>
<video width="320" height="240" cont rols>
<source src="<?= $fetch['source'] ?>" data-id="<?= $fetch['id'] ?>">
</video>
<?php if (isset($_SESSION['user_is_admin'])) : ?>
<button><a href="delete-video.php?video_id=<?= $fetch['id'] ?>&source=<?= $fetch['source'] ?>">Supprimer</button>
<button type="submit" name="update" value="modifier"> </button>
<?php endif ?>
<?php } ?>
<?php
require_once './partials/footer.php';
?>
add-videos-logic.php
<?php
require './config/database.php';
date_default_timezone_set('Asia/Manila');
var_dump($_POST);
var_dump(isset($_POST["submit"]));
if (isset($_POST["submit"])) {
$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
extract($post);
$file_name = $_FILES['video']['name'];
$file_temp = $_FILES['video']['tmp_name'];
$file_size = $_FILES['video']['size'];
if ($file_size < 50000000) {
$file = explode('.', $file_name);
$end = end($file);
$allowed_ext = array('avi', 'flv', 'wmv', 'mov', 'mp4');
if (in_array($end, $allowed_ext)) {
$name = date("Ymd") . time();
$location = 'video/' . $name . "." . $end;
if (move_uploaded_file($file_temp, $location)) {
mysqli_query($connection, "INSERT INTO `videos` VALUES('', '$name', '$location', '$description', '$title')");
header('location: ' . ROOT_URL . '/admin/add-videos.php');
}
} else {
echo "<script>alert('Wrong video format')</script>";
echo "<script>window.location = 'index.php'</script>";
}
} else {
echo "<script>alert('File too large to upload')</script>";
echo "<script>window.location = 'index.php'</script>";
}
}
file size configuration to modify in php.ini post_max_size was too small.
Sorry for this question.
Adios stackoverflow

add limit php upload image

I use a php ajax script for uploading image in my site and it's don't has limit for upload image.
I want a user can upload only for example 3 image. how can add this limit in my code?
This is my php code for uploading image:
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$vpb_upload_image_directory = "uploads/";
$vpb_allowed_extensions = array("gif", "jpg", "jpeg", "png");
foreach($_FILES as $file)
{
/* Variables Declaration and Assignments */
$vpb_image_filename = basename($file['name']);
$vpb_image_tmp_name = $file['tmp_name'];;
$vpb_file_extensions = pathinfo(strtolower($vpb_image_filename), PATHINFO_EXTENSION);
//New file name
$random_name_generated = time().rand(1234,9876).'.'.$vpb_file_extensions;
if($vpb_image_filename == "")
{
//Browse for a photo that you wish to use
}
else
{
if (in_array($vpb_file_extensions, $vpb_allowed_extensions))
{
if(move_uploaded_file($vpb_image_tmp_name, $vpb_upload_image_directory.$random_name_generated))
{
//Display Uploaded Files
$image .= '
<div class="vpb_wrapper" style="padding:10px;">
<img src="'.$vpb_upload_image_directory.$random_name_generated.'" class="vpb_image_style" />
</div>';
//$image .= '<div class="vpb_wrapper" style="padding:10px; text-decoration:none;">'.$vpb_image_filename.' uploaded</div>';
}
}
else
{
// Do not upload files which are not in the allowed file array
}
}
}
//Display the files
if($image != "") echo $image;
}
?>
In javascript, you can do something like this,
$(document).ready(function(){
$('#photoUploader').click(function(){
$('#photoUI').toggle('slow');
});
var ctr = 1;
$('#add').click(function(){
if(ctr < 3)
{
$('#ulPhoto').append("<li><input type='file' name='file[]' onchange='loadPreview(this,"+ctr+")' id='photo"+ctr+"' /><br /><img id='img"+ctr+"' src='#' alt='' style='width:100px' /><input type='text' autocomplete='off' name='caption[]' id='caption"+ctr+"' placeholder='ID Number' /></li>");
ctr++;
}
else
{
alert('Only 3 Images Allowed at a time');
}
});
});
HTML
<div id="photoUI" style="display:none; height: auto">
<ul id="ulPhoto">
<li>
<input type="file" name="file[]" id="photo0" />
<br />
<img id="img0" src="#" alt="" style="width:100px" />
<input type="text" autocomplete="off" name="caption[]" onchange="loadPreview(this,0)" id="caption0" placeholder="Default ID Number" readonly disabled />
</li>
</ul>
<span id="add" style="cursor:pointer">Add More...</span>
</div>

php and jquery image upload button after load

I'm preparing the script facebook style wall post. Using Php and Jquery. But I have a problem. I wrote a function to upload photos. Upload a picture from your computer picture is selected when the button is clicked. and passage of the picture preview image (image loading) part opens. Photos button after installing upload pictures come back again.
The problem is that (loading image) warning does not disappear. Also (loading image) warning not lost are not coming back button to upload pictures.
Thanks in advance for your help.
jQuery
$('#photoimg').die('click').live('change', function()
{
var values=$("#uploadvalues").val();
$("#previeww").html('<img src="wall_icons/loader.gif"/>');
$("#imageform").ajaxForm({target: '#preview',
beforeSubmit:function(){
$("#imageloadstatus").show();
$("#imageloadbutton").hide();
},
success:function(){
$("#imageloadstatus").hide();
$("#imageloadbutton").show();
},
error:function(){
$("#imageloadstatus").hide();
$("#imageloadbutton").show();
} }).submit();
var X=$('.preview').attr('id');
var Z= X+','+values;
if(Z!='undefined,')
{
$("#uploadvalues").val(Z);
}
});
HTML
<div class="tb-content">
<div class="ct-tab1">
<textarea name="update" id="update" placeholder="Ne düşünüyorsun?" class="contenttextarea" ></textarea>
<div id="button_hide">
<div class="secretdiv">
<div id="webcam_container" class='border'>
<div id="webcam" >
</div>
<div id="webcam_preview">
</div>
<div id='webcam_status'></div>
<div id='webcam_takesnap'>
<input type="button" value=" Resimçek " onclick="return takeSnap();" class="camclick resimcekbutton"/>
<input type="hidden" id="webcam_count" />
</div>
</div>
<div id="imageupload" class="border">
<form id="imageform" method="post" enctype="multipart/form-data" action='message_image_ajax.php'>
<div id='preview'>
</div>
<div id='imageloadstatus'>
<img src='<?php echo $base_url;?>wall_icons/ajaxloader.gif'/> Resim yükleniyor lütfen bekleyin....
</div>
<div id='imageloadbutton'>
<input type="file" name="photoimg" id="photoimg" class="dosyasec" />
</div>
<input type='hidden' id='uploadvalues' />
</form>
</div>
</div>
<div class="videovecamerabutonlari">
<div class="imgbutonu"><img src="wall_icons/videobutonu.png" border="0" /></div>
<div class="videobutonu"><img src="wall_icons/camerabutton.png" border="0" /></div>
</div>
<div class="shr">
<input type="submit" id="update_button" class="update_button" value="Paylaş" />
<input type="submit" id="cancel" value="İptal" />
</div>
</div>
</div>
<div id='flashmessage'>
<div id="flash" align="left" ></div>
</div>
Php
<?php
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
$ext = getExtension($name);
if(in_array($ext,$valid_formats))
{
if($size<(10024*10024))
{
$actual_image_name = time().$uid.".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
$data=$Wall->Image_Upload($uid,$actual_image_name);
$newdata=$Wall->Get_Upload_Image($uid,$actual_image_name);
if($newdata)
{
echo '<img src="'.$path.$actual_image_name.'" class="preview" id="'.$newdata['id'].'"/>';
}
}
else
{
echo "Fail upload folder with read access.";
}
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format.";
}
else
echo "Please select image..!";
exit;
}
?>

image upload on change event of field

I am using form action event to load image on server ,how can i identify return of method so that i can set backgound image of canvas element with that image???
<form id="imageform" class="form-horizontal" action="ajaximage.php" method="post" enctype="multipart/form-data" >
<div class="control-group">
<label class="control-label" for="InputImage">Upload Image for Liberary</label>
<div class="controls">
<span class="btn btn-file">
<input type="file" name="photoimg" id='fileImage' class="fileUpload" required />
<div id='preview' style=""></div> <!-- style="width: 90px;" onchange="this.style.width = '100%';" <div id='categoryImage' style='border:1px solid black; height:20px' ></div> --> <div id='libImage' style="display: none"></div> <div id='preview'></div> </span>
</div>
</div>
</form>
ajaximage.php
$valid_formats = array("jpg", "png", "gif", "bmp");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
//echo "<script>alert(".$name.")</script>";
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024))
{
$actual_image_name = $txt.".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
echo "<img src='../products/".$actual_image_name."' class='preview'>";
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
else
echo "Please select image..!";
exit;
}
you can fire change event using following code with jQuery
$("document").ready(function(){
$("#fileImage").change(function() {
$( "#imageform" ).submit();
});
});
OR you can use this also
$('#fileImage').on("change", function(){ $( "#imageform" ).submit(); });
see Fiddle

myproblem in uploading in php

i have this page for upload:
<?php
require ('incs/db.php');
require_once ('incs/funcs.php');
?>
<?php
if (array_key_exists('upload', $_POST)) {
$directory = str_replace(basename($_SERVER['PHP_SELF']),'',$_SERVER['PHP_SELF']);
$uploadHandler = $_SERVER['DOCUMENT_ROOT']. $directory . 'images/';
// $uploadHandler = "echtitipi".$_SERVER['HTTP_HOST']. '/images/';
$max_file_size = 30000;
define('UPLOAD_DIR', $uploadHandler);
$ext= end(explode(".", $_FILES['image']['name']));
$name = rand(1111111,9999999).'.'.$ext;
if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadHandler. $name))
{
$upload = true;
$title = $_POST ['title'];
$sql = "INSERT INTO photo (id, keyword, photoName)
VALUES ('','$title','$name')
";
$result = mysql_query ( $sql, $con );
}
else
{
$upload = false;
$msg = 'Cant Upload!';
}
}
?>
<?php
include ('incs/header.php');
?>
<?php
getUrlQuery();
?>
<script language="javascript">
<!--
function pick(symbol, path) {
if (window.opener && !window.opener.closed)
window.opener.document.form.img.value = symbol;
window.opener.document.form.imgbox.src = path;
window.close();
}
// -->
</script>
<form action="upload.php" method="post" enctype="multipart/form-data" name="uploadImage" id="uploadImage">
<p>
<label for="image">
Tanım:
</label>
<input type="text" name="title" id="title" />
<label for="image">
Upload image:
</label>
<input type="file" name="image" id="image" />
</p>
<p>
<input type="submit" name="upload" id="upload" value="Upload" />
</p>
</form>
<?php
if($upload == true)
{
echo "<a hrf(because spam!)=\"javascript:pick('$name','images/$name')\"><im(g) src=\"images/$name\" border=\"0\" alt=\"use\"></a>";
}
?>
<?php
include ('incs/footer.php');
?>
`
this upload image to curretnt root's images folder. My current folder is admin:
root/admin/images
root/images
when i use
$uploadHandler = "http://".$_SERVER['HTTP_HOST']. '/images/';
script doesnot work.
<?php
if($upload == true)
{
echo "<a hrf=\"javascriptick('$name','{$uploadHandler}$name')\"><im(g) src=\"{$uploadHandler}$name\" border=\"0\" alt=\"use\"></a>";
}
?>
the image couldnot add to editor. I guess There is a problem with javascript.
what is wrong in script
echo "<a hrf=\"javascriptick('$name','{$uploadHandler}$name')\"><im(g) src=\"{$uploadHandler}$name\" border=\"0\" alt=\"use\"></a>";
change into
echo "<img src=\"{$uploadHandler}$name\" border=\"0\" alt=\"use\">";
I guess this will help...
Im sorry for bad dictation because i cant write the right script because sending errors(link and images)
above code uploaded code to
/www/admin/images
and save information to database and add image to tinymce editor. But I want to upload code to:
www/images
when I use :
$uploadHandler = $_SERVER['DOCUMENT_ROOT'].'/images/';
and
"<img src=\"images/$name\" border=\"0\" alt=\"use\">"
the image couldnot add to editor. This is my problem.

Categories