Dropzone - send image to controller with other form data on submit - php

I'm working in laravel 5.3 project.
before asking question I have tried these links :
1st , 2nd , 3rd , 4th ,5th but no luck.
let me show you my code.
details.blade.php
<form class="form" method="post" action="{{url('add_user_photo_gallery')}}" id="my-awesome-dropzone" name="validate_resume">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
<div class="card-body">
<div class="row">
<div class="col-md-12 col-sm-12 text-center">
<div class="form-group floating-label">
<div id="dropzonephoto" class="dropzone dz-clickable form-fileupload">
<div class="dz-message dz-default" style="text-align:center;">
<span>Click Here to upload Photo </span>
</div>
</div>
</div>
<small>jpg, png , jpeg - upto 1 Mb max</small>
</div>
<div class="col-md-12">
<input type="text" name="first_name" />
</div>
<div class="col-md-12">
<input type="text" name="last_name" />
</div>
<div class="col-md-12">
<input type="email" name="email" />
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" id="btn_save_photo" class="custom-btn primary-btn pull-right" required>
<input type="reset" id="btn_reset_photo" class="custom-btn warning-btn pull-right" value="Cancle" required>
</div>
</div>
</div>
</div>
</form>
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone('#dropzonephoto', { // Make the whole body a dropzone
url: base_path + '/add_user_photo_gallery',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
paramName: "images",
parallelUploads: 1,
maxFilesize: 1.0,
maxFiles:20,
acceptedFiles: ".png,.jpg,.jpeg",
dictInvalidFileType: 'This file type is not supported.',
dictFileTooBig:'File size too Big',
addRemoveLinks: true
});
public function add_user_photo_gallery(Request $request){
dd($request->all());
}
In my controller function I print request that coming from form. I'm getting all the field except dropzone image.
using above dropzone script when I select image it's immediately go to controller and I can see all image details in console / network.I don't want that so I added below code in my script.
autoProcessQueue:false,
$('#btn_save_photo').on('click',function(e){
e.preventDefault();
myDropzone.processQueue();
});
After add this code image is upload on submit button but other form element is not submitted. and this upload process is shown in inspect element / network section .
So what I want is on submit button want to page refresh and print all form data with dropzone image.
Anyone is here to solve my problem ?

Related

Why is the Ajax code not posting to pho file?

I have a form which I am using to AJAX to submit. I have several forms using the same script all with different names posting to response.php however this particular form will not send.
When I remove my form name it tries to post in the normal way but when it has the correct name as the AJAX file I receive no reponse whatsoever leading me to think it is an error within the AJAX file.
HTML
<tr><Td>
<div class="row box" id="login-box">
<div class="col-md-9 col-md-offset-1">
<div class="panel panel-login">
<div class="panel-body">
<div class="row">
<div class="col-lg-12">
<div id="msg"></div>
<div class="alert alert-danger" role="alert" id="error" style="display: none;">...</div>
<form id="editMarshalTitleDetails-form" name="editMarshalTitleDetails_form" role="form" style="display: block;" method="post">
<div class="form-group">
<br><Br>
<input type="text" name="content" id="content" tabindex="2" class="form-control" value="<?php echo $content2 ?>">
<input type="hidden" name="marshalColumn" value="marshalPackFrontPageTitle">
<input type="hidden" name="userID" value="<?php echo "$userID"?>">
</div>
<div class="col-xs-12 form-group pull-right">
<button type="submit" name="editMarshalTitleDetails-submit" id="editMarshalTitleDetails-submit" tabindex="4" class="form-control btn btn-primary">
<span class="spinner"><i class="icon-spin icon-refresh" id="spinner"></i></span> Edit Title
</button>
</div>
</form>
</div>
</div>
</tr></td>
AJAX
$("#editMarshalTitleDetails-form").validate({
submitHandler: submitForm6
});
function submitForm6() {
var data = $("#editMarshalTitleDetails-form").serialize();
$.ajax({
type : 'POST',
url : 'response.php?action=editMarshalTitleDetails',
data : data,
beforeSend: function(){
$("#error").fadeOut();
$("#editMarshalTitleDetails_button").html('<span class="glyphicon glyphicon-transfer"></span> updating ...');
},
success : function(data){
$("#editMarshalTitleDetails_button").html('<span class="glyphicon glyphicon-transfer"></span> Template Updated');
var a = data.split('|***|');
if(a[1]=="update"){
$('#msg').html(a[0]);
}
}
});
return false;
}

storing image in server from modal input is not working

I am storing some data from modal input. Everything works fine but when I work with file type data in this case I am taking an image which is to be stored in server folder and its location is to be stored in database.
I check if user wants to add data
then push the data to corresponding method of controller via ajax
there, I store the image in selected folder and retake location of the image, then push all data to model to store them in database.
Code:
modal:
<div class="modal fade" id="modal_form" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h3 class="modal-title">Blog Form</h3>
</div>
<div class="modal-body form">
<form id="form" class="form-horizontal" method='post' action='' enctype="multipart/form-data>
<input type="hidden" value="" name="id"/>
<div class="form-body">
<div class="form-group">
<label class="control-label col-md-3">Author Name</label>
<div class="col-md-9">
<input name="author" placeholder="author" class="form-control" type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Blog Title</label>
<div class="col-md-9">
<input name="title" placeholder="title" class="form-control" type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Blog Category</label>
<div class="col-md-9">
<input name="tag" placeholder="tag" class="form-control" type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Blog Details</label>
<div class="col-md-9">
<input name="details" placeholder="details" class="form-control" type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Blog Picture</label>
<div class="col-md-9">
<input name="picture" id="picture" placeholder="Add a photo" type="file">
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Save</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
Save function in script:
function save()
{
var url;
if(save_method == 'add')
{
url = "<?php echo site_url('index.php/admin/blog_add')?>";
}
else
{
url = "<?php echo site_url('index.php/admin/blog_update')?>";
}
$.ajax({
url : url,
type: "POST",
data: $('#form').serialize(),
dataType: "JSON",
success: function(data)
{
//if success close modal and reload ajax table
$('#modal_form').modal('hide');
location.reload();// for reload a page
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
}
});
}
Blog_add method in Admin controller:
public function blog_add(){
$picture_name = $this->store_photo_return_photo_location();
$data = array(
'author' => $this->input->post('author'),
'title' => $this->input->post('title'),
'details' => $this->input->post('details'),
'tag' => $this->input->post('tag'),
'picture' => $picture_name,
);
$insert = $this->admin_model->blog_add($data);
echo json_encode(array("status" => TRUE));}
store_photo_return_photo_location function:
public function store_photo_return_photo_location(){
$filename = $_FILES['picture']['name'];
// Location
$location = 'assets/images/'.$filename;
// file extension
$file_extension = pathinfo($location, PATHINFO_EXTENSION);
$file_extension = strtolower($file_extension);
// Valid image extensions
$image_ext = array("jpg","png","jpeg","gif");
$response = 0;
if(in_array($file_extension,$image_ext)){
// Upload file
if(move_uploaded_file($_FILES['picture']['tmp_name'],$location)){
$response = $location;
}
}
return $response;}
model:
public function blog_add($data)
{
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
Until I worked with data without image, it was working ok, but when I'm working with image, it is not.
I think that pushing image name through ajax is not working.
Thanks in advance
you are calling this function for uploading an image but not passing a 'post' data to that function
$this->store_photo_return_photo_location();
call it like this
$image = $this->input->post['picture'];
$this->store_photo_return_photo_location($image);
and modify your function
public function store_photo_return_photo_location($image) {
//..your code
}
javascript's serialize() function won't be able to grab file from the html form. You should use FormData() instead
data: new FormData($('#form')),
Also in your store_photo_return_photo_location() function verify that a file actually exists before proceeding with the upload
if(!empty($_FILES['picture']['name'])){
//proceed
}
There's a different approach to uploading a file via ajax.
Please check this: https://stackoverflow.com/a/2320097/10412708
An easier approach is to put the submit URL in your form's action and let the form submit and refresh the page. Anyway, your JS reloads the page on success, why did you even use ajax for? Try to have your submit URL pre-conditioned in PHP if possible.

dropzone $_FILES is empty

I try to submit a form with some dropzone-added files and additional fields.
I don´t want those dropzone Ajax Features, because i want to add some information by additional inputs.
So, at least a very simple Submit-Form with multiple Files, which gets processed after pressing the submit button.
So far, the client side works fine, but when I submit the form, $_FILES is empty! Why?
What I got so far:
<?
if(isset($_POST))
{
print_r($_POST);
print_r($_FILES);
exit;
}
?>
<form id="smart-form" enctype="multipart/form-data" method="POST" action="upload.php">
<div class="form-body">
<div class="section">
<div class="dropzone" id="myDropzone">
<h3 class="dz-drop-title"> Drop files here or </h3>
<p class="dz-clicker"> <span> Browse File </span> </p>
<div class="fallback">
<input name="file[]" type="file" multiple />
</div>
</div>
</div><!-- end section -->
</div><!-- end .form-body section -->
<label style="padding: 5px;" for="another_field_1">another field 1 <input id="another_field_1" type="text" name="testinput1"></label><br>
<label style="padding: 5px;" for="another_field_2">another field 2 <input id="another_field_2" type="text" name="testinput2"></label>
<div class="form-footer">
<button type="submit" class="button btn-primary"> Send Form </button>
</div><!-- end .form-footer section -->
</form>
<script type="text/javascript">
jQuery(document).ready(function($){
jQuery(".dropzone").dropzone({
url: "upload.php",
dictDefaultMessage: "Datei bitte hier ablegen!",
success: function() {
jQuery('.success-mark').show();
jQuery('.error-mark').hide();
},
error: function() {
jQuery('.success-mark').hide();
jQuery('.error-mark').show();
}
});
});
</script>

dont refresh the page when you submit the form

hii guys im new to web programing and im creating a website
in this website i want to add a contact us form in the footer of the page like this
<form action="footer_contactus1.php" method="post">
<div class="row">
<div class="form-grope col-lg-4">
<label><span class="glyphicon glyphicon-user"></span>Name</label><br>
<input name="cuname" type="text" class="form-cotrol">
</div>
<div class="form-grope col-lg-4">
<label><samp class="glyphicon glyphicon-envelope"></samp>Email</label><br>
<input name="cuemail" type="email" class="form-cotrol">
</div>
<div class="form-grope col-lg-4">
<label><i class="glyphicon glyphicon-earphone"></i>Phone Number</label><br>
<input name="cutele" type="tel" class="form-cotrol">
</div>
<div class="form-grope col-lg-12">
<br>
<label><span class="glyphicon glyphicon-paste"></span>Message</label><br>
<textarea name="cumessage" class="form-cotrol" rows="6" style="width: 100%;" ></textarea>
</div>
<div class="form-grope col-lg-12">
<br>
<input type="hidden" name="save" value="contact">
<button type="submit" class="btn btn-info">Submit</button>
</div>
</div>
</form>
and when i click the submit button i want the php file to get executed so the inputs got inserted into the database
the php file
<?php
$pdo = new PDO('mysql:host=localhost;dbname=mywsite_db', 'root', '');
$req = $pdo->prepare('INSERT INTO message (id_message,nom,email,tele,message)
VALUES(null,?,?,?,?)');
$req->execute(array($_POST['cuname'],$_POST['cuemail'],$_POST['cutele'],$_POST['cumessage']));
header('Location: HomePage.php');
?>
i wanted this form to get submited without refreshing the page by ajax
i tried different tutorials and nothing worked can you plzz gives me the correct ajax script for my form so i can submit it to the php file and insert the data on the database then a message (alert) pop up telling me that the data has been inderted without refreshing the page plzz
<form action="footer_contactus1.php" method="post" id="myForm1">
...
</form>
<script type="text/javascript">
var frm = $('#myForm1');
frm.submit(function (ev) {
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data) {
alert('The data has been inserted');
}
});
ev.preventDefault();
});
</script>

How to submit form on one page to a different page using ajax call in jquery

I have a page called page2.php. It has a form that allows you to search via jquery ajax call. The code is below. My question is, I have a different form on page1.php. How can I submit the form on page1.php to go to page2.php and have it execute the page2.php code below using the data from the form on page1.php? I know this is most likely simple, having a brain fart right now.
$(function() {
$.validate({
form: '#form_search',
validateOnBlur: false,
errorMessagePosition: 'top',
onSuccess: function(form) {
var formval = $(form).serialize();
var formurl = '/page2.php?a=search';
$('#form_results').html('<div class="form_wait_search">Searching, please wait...<br><img src="/images/search-loader.gif"></div>');
$.ajax({
type: 'POST',
url: formurl,
data: formval,
success: function(data){
var json = $.parseJSON(data);
$('#form_errors').html(json.message);
$('#form_results').html(json.results);
}
});
return false;
}
});
});
UPDATE
Here is the forms Im referring to.
On page1.php this is like a module on the right side bar. Just a form that I want to post to page2.php
<div class="scontent_box1">
<strong class="box_title"><i class="fa fa-search fa-flip-horizontal"></i> Find Locations</strong>
<form method="post" action="/page2.php">
<div class="form-group">
<label>Street Address</label>
<input type="text" class="form-control" name="ad" placeholder="Enter Street Address...">
</div>
<div class="form-group">
<label>City, State or Zip Code</label>
<input type="text" class="form-control" name="ct" placeholder="Enter City, State or Zip Code...">
</div>
<button type="submit" class="btn btn-default blue_btn">Search</button>
</form>
</div>
Now here is the form on page2.php that executes the ajax code above. I want page1.php to submit to page2.php and envoke the same jquery code above.
<div class="row no_gutters">
<div class="search_page">
<div id="form_errors"></div>
<form method="post" id="form_search">
<div class="form-group">
<label for="ad">Street Address<span class="reqfld">*</span></label>
<input type="text" class="form-control" data-validation="required" id="ad" name="ad" placeholder="Enter Street Address..." value="">
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="ct">City, State or Zip Code<span class="reqfld">*</span></label>
<input type="text" class="form-control input-sm" data-validation="required" id="ct" name="ct" placeholder="Enter City Name..." value="">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 col-xs-12">
<button type="submit" class="btn btn-default blue_btn btn-block">Search Locations</button>
</div>
<div class="col-md-8"></div>
</div>
</form>
</div>
<div id="form_results"></div>
</div>

Categories