jquery ajax form submit plugin not posting file input - ajax

I've got this form:
<form id="imageinputpopup" class=suggestionsubmit style="display: none">
<span>Add a thing!</span><br/>
<label>url: </label><input name="imageurl" type="url"><br/>
<label>file: </label><input name="imagefile" type="file"><br/>
<input type='hidden' name='schoolid' class="schoolid">
<input type="submit" value="Submit">
</form>
And this document.ready:
<script type="text/javascript">
$(document).ready(function() {
$('.schoolid').val(get_gmap_value('school_id'));
$(".allow-submission").live('click', function(){
if($(this).attr('inputtype')=="colorpicker"){
.....
} else if($(this).attr('inputtype')=="image"){
remove_hidden("#imageinputpopup");
add_fieldname($(this), $("#imageinputpopup"));
$("#imageinputpopup").dialog();
} else if($(this).attr('inputtype')=="text"){
....
} else {
//nothing
}
});
$(".suggestionsubmit").submit(function(){
event.preventDefault();
alert($(this).html());
$(this).ajaxSubmit({
url: '/save-school-suggestion/',
type: 'post',
success: function(response){
response = jQuery.parseJSON(response);
// Check for login redirect.
// if ( response.requireLogin ) {
// alert('Sign up or log in to save your answer');
// } else {
$('.suggestionsubmit').dialog('close');
// }
}
});
});
});
function add_fieldname(element, addto){
var elementname = document.createElement('input');
elementname.type = 'hidden';
elementname.name = 'fieldname';
elementname.value = element.attr('fieldname').replace(' ', '_');
$(elementname).addClass('fieldname');
addto.append(elementname);
}
function remove_hidden(element){
$(element+' .fieldname').remove();
}
But the file field isn't showing up server side.
Why?
I found this in the documentation:
Why aren't all my input values posted?
jQuery form serialization aheres closely to the HTML spec. Only successful controls are valid for submission.
But I don't understand why my file control would be invalid.
I have another submission form in a different place on my site that is almost identical and works perfectly...
EDIT: this is the other form that does work (it has some extra stuff in it, but the form tag just has an id, like the problem one, and the input tags are the same).
<form id="photos-submission-form6">
<input type="hidden" name="section" value="photos">
<input type="hidden" name="school" id="photos-submit-school6">
<div style="margin-bottom: .5em">
<p style="position: relative; width:80%; font-size: 14px; display: inline" id="photos-anonymity-header6">Post as: null</p>
<img id="helpicon6" src="/static/img/help-icon.png" style="float: right; cursor: pointer; padding-left:1em;">
<div id="explanation6" style="display: none; padding:1em; background-color:white; border:2px solid gray; position: absolute;z-index:30; right:5px; top:5px">For more posting options, <a id="profilelink6" href="/profile/">fill out your profile</a></div>
</div>
<div id="photos-anonymity-select6" style="margin-bottom: .75em; width:412px" class="ui-slider ui-slider-horizontal ui-widget ui-widget-content ui-corner-all"><a class="ui-slider-handle ui-state-default ui-corner-all" href="#" style="left: 100%; "></a></div>
<input type="hidden" id="photos-anonymity-level6" name="anonymity-level" value="username">
<span style="line-height: 40px;">
<label class="photouploadlabel">URL</label><input type="text" name="image-url" style="width: 335px"><br>
<label class="photouploadlabel">File</label><input type="file" name="image-file" style="width: 335px"><br>
<label class="photouploadlabel">Caption</label><input type="text" id="image-caption6" name="image-caption" style="width: 335px; color: rgb(128, 128, 128); ">
</span>
<div style="height: 30px; margin-top: 1em; width: 413px;">
<label id="photos-tagsbutton6" style="margin-right: .5em; cursor: pointer; vertical-align: bottom; float:left; line-height: 1.8em;">Tags</label>
<input id="photos-tagsinput6" style="display: none;" type="text" name="tags">
<button id="send-photos-suggestion6" disabled="" style="float:right; position: relative; bottom: 7px; right: -4px;" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-disabled ui-state-disabled ui-button-text-only" role="button" aria-disabled="true"><span class="ui-button-text">Post</span></button>
</div>
</form>

This is probably not the case but are sure there are no spelling mistake server side? like you would be using $_FILE instead of $_FILES? Could you post the relevant php also?
Also, definitely not an issue but it is recommended to close your input tags, now like this:
<input ... />

Add enctype="multipart/form-data" attribute to your form.

Try to change the type of the input imageurl from url to text:
FROM:
<label>url: </label><input name="imageurl" type="url"><br/>
TO:
<label>url: </label><input name="imageurl" type="text"><br/>
I am not sure, but maybe the jquery plugin fails serializing the form due to invalid type attribute of image_url.

Hey you just forgot to add ---> enctype="multipart/form-data" in the form tag. This will help you out.

I think you have a problem with the binding in javascript not recognising your file.
Try binding your submit trigger event with another live() function
i.e. change
$(".suggestionsubmit").submit(mooFar(e));
to
$(".suggestionsubmit").live('submit', mooFar(e));

...........I was looking in the wrong place in the request for the file.
Server side should have been:
if not s.url_field and 'imagefile' in request.FILES:
s.image_field = request.FILES['imagefile']
instead of
s.image_field = request.POST.get('imagefile', None)
Complete and utter fail on my part.

Make sure the files you're testing aren't outside the max file size, it would be worth setting this in your HTML.
<input type="hidden" name="MAX_FILE_SIZE" value="500" />
Also, testing without the display:none might be worth while until you have the form working; which browser are you testing in?

Related

How can I filter dates between start and end using query builder wherebetween

I have put two input tag to get the start date and end date. I want to use the wherebetween clause of laravel query builder to take in those inputs and get back data between those two days
blade.php file
<form action="{{ route('search.between.dates',[$expense_category_data[0]->id]) }}" method="POST">
#csrf
<h3 style="color:Blue; text-align: center;text-decoration:underline"><b>Filter Date </b></h3>
<input class="form-control" type="text" id="datepicker1" name="start_date" placeholder="Start Date" style="width: 20%; display: inline-block; margin-left:20%; margin-bottom: 2% ">
<input class="form-control" type="text" id="datepicker2" name="end_date" placeholder="End Date" style="width: 20%; display: inline-block; margin-left:20%; margin-bottom: 2%">
<br>
<button type="submit" class="btn btn-success" style="position: absolute;
left: 45%;">Find Expenses</button>
</form>
Script
<script>
$( function() {
$( "#datepicker1" ).datepicker({
dateFormat: 'dd-mm-yy'
});
} );
$( function() {
$( "#datepicker2" ).datepicker({
dateFormat: 'dd-mm-yy'
});
} );
</script>
Controller
public function search_between_dates(Request $request,$exp_category_id){
$start_date = dmyToYmd($request->start_date);
$end_date = dmyToYmd($request->end_date);
$expense_category_data = DB::table('expense_category')
->leftjoin('expense_table','expense_category.id','=','expense_table.exp_category')
->select('expense_category.id as category_id','expense_category.exp_category AS category_name','expense_category.exp_category_details','expense_category.exp_category_image','expense_table.*')
->where('expense_category.id',$exp_category_id)
->whereBetween('expense_table.exp_date',[date($start_date),date($end_date)])
->get();
return view('admin.Expense_sheet.view_expense_category_details',compact('expense_category_data'));
}
I don't know what that function dmyToYmd is doing. My Sql uses the date format which is Y-m-d while your datepicker is using the format dd-mm-yy that's why it is not fetching the expected results. You will need to use the carbon to format the date.
->whereBetween('expense_table.exp_date',[Carbon/Carbon::createFromFormat('d-m-y', $start_date)->format('Y-m-d'),Carbon/Carbon::createFromFormat('d-m-y', $end_date)->format('Y-m-d')])
/*
it is sending the format
dd-mm-yy => 01-01-21
Mysql required format
Y-m-d => 2021-01-01
*/

Image sprite - form submit button

Can i use image sprites for my form submit button, tried a few things but nothing seems to work.
.button, .download, .OK, .file{
background: url(app/sprites.png) no-repeat;
}
.button{
background-position: -1px -212px ;
width: 170px;
height: 30px;
}
My form
<form action="add.php" method="post" value="Username">
<div style="float:left; margin-left:15px;"><input type="text" name="fbid" required></div>
<div style="float:left; margin-left:2px;"><input type="submit" value="Submit"></form>
Right now you are missing a class on the input.
<input type="submit" class="button" value="Submit">
With that it'll work.
I put it into a jsFiddle, using a different image because I don't have access to yours. You can play with the position, in incriments of 32px.
jsfiddle.net/CzWCv
Have you tried
<div class="button" style="float:left; margin-left:2px;"><input type="submit"
value="Submit"></form>
??

django handle form submit without returning a response

I've tried everything here, and I must be missing something, as my ajax post to Django isn't returning anything. I've got it down to the bare bones at this point, just trying to get it working and no joy.
My view:
def saveProjectEntry(request):
form = ProjectEntryUpdateForm(request.POST)
if form.is_valid():
msg = json.dumps({'msg':'success'})
return HttpResponse(msg, mimetype='application/json')
else:
msg = json.dumps({'msg':'failed'})
return HttpResponse(msg, mimetype='application/json')
My url.py entry:
url(r'^chargeback/savepe/$','chargeback.views.saveProjectEntry'),
My jQuery:
$('#peUpdateForm').submit(function() {
$.ajax({
url:'/chargeback/savepe/',
data: $("#peUpdateForm").serialize(),
type: "POST",
success: function(e) {
alert("success");
},
error: function(e) {
alert("failed");
}
});
return false;
});
I also tried the jQuery post method:
$('#peUpdateForm').submit(function(e){
$.post('/chargeback/savepe/', $('#peUpdateForm').serialize(), function(data){
alert("success");
});
e.preventDefault();
});
I'm not getting anything. Not getting any of my alerts. Not getting any errors from the view (if I just navigate to the view directly I get the expected {'msg':'failed'} displayed in the browser, so the json response is fine. The url calls the correct view. The only thing I can think of is my jQuery code is wrong, but I can't figure out where, and there are no errors in the console. I've tested, in the console, the
$('#peUpdateForm').serialize()
and I get the expected value. Pulling my hair out here... Thanks.
EDIT: Adding HTML as my post method isn't even getting called when I place a breakpoint there, so something may be wrong with how my submit is being set up.
<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable ui-resizable ui-dialog-buttons" style="outline: 0px; z-index: 1002; position: absolute; height: auto; width: 376px; top: 75px; left: 408px; display: block;" tabindex="-1" role="dialog" aria-labelledby="ui-id-8"><div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
<span id="ui-id-8" class="ui-dialog-title">Project Entry Update</span><span class="ui-icon ui-icon-closethick">close</span></div>
<div id="addPEForm" style="width: auto; min-height: 0px; height: 365px;" class="ui-dialog-content ui-widget-content" scrolltop="0" scrollleft="0">
<form method="post" id="peUpdateForm" action="">
<div style="display:none">
<input type="hidden" name="csrfmiddlewaretoken" value="wvzwSBl87vC1tbkbdfxsD82GnjtvJCSz"></div>
<p><label for="id_departmentid">Department:</label>
<select id="id_departmentid" class="projEntryControl" name="departmentid">
<option value="" selected="selected">Choose a Department</option>
<option value="1">ABND</option>
<option value="2">ATT</option>
<option value="3">AVI</option>
<option value="4">CCS</option>
<option value="5">PBW</option>
</select></p>
<p><label for="id_projectid">Project:</label>
<select id="id_projectid" class="projEntryControl" name="projectid">
<option value="-1">Choose a Project</option>
<option value="undefined">Bexar Street</option>
<option value="undefined">Chalk Hill</option>
<option value="undefined">Crown Road</option>
</select>
</p>
<p><label for="id_progNumId">Program Number:</label>
<select id="id_progNumId" class="projEntryControl" name="progNumId">
<option value="" selected="selected">Choose a Program Number</option>
<option value="1">31664</option>
<option value="2">DD-7081</option>
</select></p>
<p><label for="id_hoursWorked">Hours Worked:</label>
<input name="hoursWorked" value="0.0" class="projEntryControl" maxlength="5" type="text" id="id_hoursWorked"></p>
<p><label for="id_notes">Notes:</label>
<textarea id="id_notes" rows="10" cols="40" name="notes"></textarea></p>
</form>
</div>
...
AFAIK, Here's the way to do it right, if you want to keep the button outside:
$('#button').on('click', function(){
$.ajax({
data: $('#form').serialize(),
...});
});
This because `$('#form').submit() gets called only if you keep the submit button inside the form tags.

Jquery, each time click button a textfield appears

with jquery i want to make multiple text fields appear..
when click this text
<form>
<input style="margin-top:20px;margin-right :50px; width:255px;height:23px;float:left;" type="text" name="CrawlerField"id="CrawlerField"/>
<input style="margin-top: 15px; margin-right :50px; width:255px;height:23px;float:left;" type="text" name="CrawlerField1" id="CrawlerField1"/>
<input style="margin-top: 15px; margin-right :50px; width:255px;height:23px;float:left;" type="text" name="CrawlerField1" id="CrawlerField2"/>
<font id="jqueryAdd">Add button</font>
</form>
this is the text when clicked a new text field appears....
<font id="jqueryAdd">Add button</font>
script
at start all textfields are hidden
$("#CrawlerField1").hide();
$("#CrawlerField2").hide();
when click the word Add button defined with jqueryAdd
check if the previous textfield is hidden next textfield appears
$("#jqueryAdd").click(function ( event ) {
if($("#CrawlerField").is(":visible"))
$("#CrawlerField1").show();
return false;
});
$("#jqueryAdd").click(function ( event ) {
if($("#CrawlerField1").is(":visible"))
$("#CrawlerField2").show();
return false;
});
this code results in the appearance of all buttons at start !!
add class to your all <input> tags like
<input class ="in" style="margin-top:20px;margin-right :50px; width:255px;height:23px;float:left;" type="text" name="CrawlerField"id="CrawlerField"/>
<input class ="in" style="margin-top: 15px; margin-right :50px; width:255px;height:23px;float:left;" type="text" name="CrawlerField1" id="CrawlerField1"/>
<input class ="in" style="margin-top: 15px; margin-right :50px; width:255px;height:23px;float:left;" type="text" name="CrawlerField1" id="CrawlerField2"/>
and in jquery add this line to hide all input initially.
$(".in").hide();

Why is Firefox not properly rendering this textarea element?

I made some changes to one of my web pages (CSS and HTML) and now Firefox is not rendering this textarea properly. It has a sort of jitter effect happening in the middle of the top line. I'm not sure if it has anything to do with the changes I made, but what could be causing this?
<div id="CommentForm">
<form action="/posts/add-comment" method="post">
<input type="hidden" id="Table_Name" name="Table_Name" value="Posts" />
<input type="hidden" id="Table_ID" name="Table_ID" value="47" />
<input type="hidden" id="IsLiveBroadcast" name="IsLiveBroadcast" value="1" />
<textarea name="Comment_Body" id="Comment_Body" rows="1"></textarea>
<div id="error-messages"></div>
<input type="submit" class="floatRight" id="postComment" value="Post your response" />
</form>
<div class="clear"></div>
</div>
And the CSS being applied:
#live_broadcast_post #CommentForm {
margin: 0;
padding: 0;
width: 460px;
height: auto;
background: none;
}
#live_broadcast_post #CommentForm textarea {
font-family: "Lucida Grande",Verdana,"Bitstream Vera Sans",Arial,sans-serif;
font-size: 12px;
padding: 5px;
width: 98%;
}
The strange thing is that this wasn't happening before I made the changes, and now it only affects Firefox, not any other browser I have tested.
Update:
I noticed that when the page loads, it renders correctly. Only after an iframe (also on this page) is finished loading does the rendering change.
Seeing as even the text itself is affected, this looks rather like a OS / graphics card / rendering problem.
I would try it on a different machine first before questioning the markup.
I've been seeing this jitter in some websites, not with Firefox only.
Probably graphics rendering (cards) related.

Resources