Can't fill_in value because Capybara don't see popup - ruby

As I know, when popup/modal opens it is on the top of page where I opened it from, so when try to fill value in it don't see where it need to do that.
Then tried to look inside modal with within. Tried other classes, but same Capybara::ElementNotFound: Unable to find visible css "modal183"
within('modal183') do
fill_in 'ctl00_ContentPlaceHolder1_tbId', :with => '10'
end
Tried also find with xpath, but also nothing
Capybara::ElementNotFound: Unable to find visible xpath "//input[#id='ctl00_ContentPlaceHolder1_tbId']"
<div class="modal183">
<div class="popup_Titlebar" id="PopupHeader">
<div class="TitlebarLeft">
<span id="ctl00_ContentPlaceHolder1_lblTitle">text</span>
</div>
</div>
<div class="popup_Body">
<div class="popup_TextNoTop">
<span id="ctl00_ContentPlaceHolder1_lbl1" class="label-left">text</span>
<input name="ctl00$ContentPlaceHolder1$tbId" type="text" id="ctl00_ContentPlaceHolder1_tbId" class="txtInputDec width100" onFocus="ClearTheTextbox(this, '')" onBlur="FillTheTextbox(this, '')" onkeypress="return onlyNumbers()" />
<span id="ctl00_ContentPlaceHolder1_cmrvIB" style="color:Red;display:none;"><a href='#' class='tooltipCons'><img src='/App_Themes/Default/img/exclamation.png' alt='' /><span>text</span></a></span>
<span id="ctl00_ContentPlaceHolder1_revIB" style="color:Red;display:none;"><a href='#' class='tooltipCons'><img src='/App_Themes/Default/img/exclamation.png' alt='' /><span>text
</span></a></span>
<span id="ctl00_ContentPlaceHolder1_rvTb" style="color:Red;display:none;"><a href='#' class='tooltipCons'><img src='/App_Themes/Default/img/exclamation.png' alt='' /><span>text</span></a></span>
<br />
<span id="ctl00_ContentPlaceHolder1_lbl2" class="label-left">text</span>
<input name="ctl00$ContentPlaceHolder1$tb1Id" type="text" id="ctl00_ContentPlaceHolder1_tb1Id" class="txtInputDec width100 top5" onFocus="ClearTheTextbox(this, '')" onBlur="FillTheTextbox(this, '')" onkeypress="return onlyNumbers()" />
<span id="ctl00_ContentPlaceHolder1_rev1IB" style="color:Red;display:none;"><a href='#' class='tooltipCons'><img src='/App_Themes/Default/img/exclamation.png' alt='' /><span>text
</span></a></span>
<span id="ctl00_ContentPlaceHolder1_rvTb1" style="color:Red;display:none;"><a href='#' class='tooltipCons'><img src='/App_Themes/Default/img/exclamation.png' alt='' /><span>text</span></a></span>
</div>
</div>
<div class="popup_Buttons">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="left">
<a onclick="if (!window.event) {this.disabled=true; this.style.color='grey'; var but = document.getElementById('btnCancel'); but.disabled=true; but.style.color='grey';};" id="ctl00_ContentPlaceHolder1_btnOkay" class="cool-button width80 blue" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ContentPlaceHolder1$btnOkay", "", true, "cons", "", false, true))">text</a>
</td>
<td align="right">
<a id="ctl00_ContentPlaceHolder1_btnCancel" class="cool-button width80 black" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$btnCancel','')">Atcelt</a>
</td>
</tr>
</table>
</div>
</div>
Updated: Attached part where it looks like be this popup which appear after I press on button. Inspect code shows on this code. I didn't wrote this code, but trying to build automated tests on them :)
<div id="popupparent" style="display: block; height: 1e+06px; left: 0px; top: 0px;">
<script language="javascript" type="text/javascript">
function resizeIframe(obj) {
var h = "innerHeight" in window ? window.innerHeight : document.documentElement.offsetHeight;
var w = "innerWidth" in window ? window.innerWidth : document.documentElement.offsetWidth;
var doc;
if (obj.contentDocument) {
doc = obj.contentDocument;
} else if (obj.contentWindow) {
doc = obj.contentWindow.document;
} else {
return;
}
var childHeight = doc.body.scrollHeight;
var childWidth = doc.body.scrollWidth;
obj.style.height = childHeight + 'px';
var ver = getInternetExplorerVersion();
var parent = document.getElementById('termsofuse');
if (ver > -1) {
parent.style.marginTop = ((h - childHeight) / 2) - 64 + 'px';
} else {
parent.style.top = ((h - childHeight) / 2) - 64 + 'px';
}
parent.style.left = ((w - childWidth) / 2) + 'px';
parent.style.display = "block";
parent.style.visibility = "visible";
}
<div id="termsofuse" style="left: 1081px; top: 246.5px; display: block; visibility: visible;">
<iframe id="frameeditexpanse" frameborder="0" src="PopDailyCons.aspx?e=c0lEPUMwMDEwMDA3MDQmYUlEPUEwMDQ3ODIxNzAmZGF5PTImY3VyRGF0ZT0yMDE4LjA0JmN1clVzZXI9MTYwJnNvdXJjZT1hdGs=" scrolling="no" marginheight="0" marginwidth="0" class="termsOfUseFrame" style="height: 185px;"> </iframe>
<div class="popup_Buttons" style="display: none">
<input id="btnOk" value="Done" type="button">
<input id="btnCancel" value="Cancel" type="button" onclick="hideusertermwindow()">

Your within call doesn't work because you need to pass it a CSS selector. You're passing modal183 which would look for a <modal183> element, when what you really want is .modal183 to look for an element with the class modal183.
From your description it's tough to tell exactly what you're doing but it sounds like maybe you're talking about a popup window (new browser window) opened on top of the current tab. If that is the case then you'd need to swap windows in order to interact with it, along the lines of
new_win = window_opened_by do
# whatever action in the original window causes the popup window to open
click_button 'blah'
end
within_window(new_win) do
fill_in 'ctl00_ContentPlaceHolder1_tbId', :with => '10'
end

Related

When I click back and forward Buton of browser all of my dynamically created PartialView get disappered

I can create multiple Partialviews in my view
Create.cshtml
#model Opto.Models.Recipe
....
<div id="Far" class="tab-pane active card-body">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="row form-group">
<div class="col">
<div class="text-left" dir="ltr">
<partial name="RecipeFarSighted.cshtml" Model="Model.FarSighted" />
</div>
<hr />
<div class="text-center" dir="rtl">
<div id="FarSightedBillsSection">
#*FarSightedBills will be created here by javascript*#
</div>
<a onclick="AddBill('far')" style="cursor:pointer"><img src="~/images/Plus.png" width="25" height="25" /></a>
<br /><br />
#if (!Model.FarSighted.Share)
{
<div class="form-group row form-check">
<label class="form-check-label col">
<input asp-for="FarSighted.Share" type="checkbox"> عدم دریافت عینک
</label>
</div>
}
</div>
</div>
</div>
<br />
</div>
...
#section Scripts{
<script>
var farIndex = 0;
var farRealCount = 0;
function AddBill(type)
{
tag = "<div id='farSightedBill" + farIndex + "' ><a onclick=\"RemovePartial('far'," + farIndex + ")\" style='cursor: pointer'><img src='/images/Minus.png' width='25' height='25' /></a><h4><a onclick=\"$('#farSighted" + farIndex + "').collapse('toggle')\" style='cursor: pointer' data-target='#farSighted" + farIndex + "' id='farBillTitle" + farIndex + "' > عینک دائمی " + (farRealCount + 1) + "</a ></h4 > <br /><div class='collapse' id='farSighted" + farIndex + "'></div></div > ";
$.get('/Glasses/DisplayFarBill?index=' + farIndex +
'&packFactor=' + '#(Model.FarSighted?.PackFactor)' + '&lenseCover=' + '#(Model.FarSighted?.LenseCover)',
function (partial) {
$('#FarSightedBillsSection').append(tag);
$('#farSighted' + farIndex).append(partial);
$('#farSighted' + farIndex).collapse('show');
//aaa(farIndex);
farIndex++;
farRealCount++
});
}
</script>
}
when I click the Back button of the browser and then forward, all my partial views will be disappeared. how can I solve this?
and when I go to submit action method in the controller for validation and there are some errors in it, when it goes back to view there are no parts in it, what should I do?
Firstly,the Back and Forward button is used for navigating through visited Web pages.And it may uses BFCache,it will cache the whole page including jquery(Source code).And the partial view you added will not shown after Back,Forward click is because it is added by jquery.jquery will change the html code,but will not change the source code.When you click Back,Forward button,browser will reload the Source code,and render it to html code.If you want to show the partial view,you can only add them directly like this in your code:
<div class="text-left" dir="ltr">
<partial name="RecipeFarSighted.cshtml" Model="Model.FarSighted" />
</div>

AJAX POST Send Multiple DIV value

I would like to send my dynamically generated DIV items, but I do not understand how to do it
I create my div dynamically:
$("#clt_forf_ti").click(function(e){
e.preventDefault();
if(x < max_input){
x++;
var addDIV = $('<div id="clt_forf_div'+x+'" class="w-100 form-control" ><label for="clt_forf_num'+x+'">Ligne :</label> <input type="text" class="font-weight-bold border-top-0 border-right-0 border-left-0 mR-10" id="clt_forf_num'+x+'" autocomplete="off" style="width: 100px" > Forfait : <i id="clt_forf_forf_select'+x+'" style="display:none;"></i><input type="text" class="border-top-0 border-right-0 border-left-0" id="clt_forf_forf'+x+'" autocomplete="off" style="width: 180px"><i class="ti-calendar mL-20 mR-5"></i><input type="text" class="border-top-0 border-right-0 border-left-0" id="clt_forf_date_start'+x+'" name="datepicker" placeholder="Début" style="width: 80px"><i class="ti-calendar mL-20 mR-5"></i><input type="text" class="border-top-0 border-right-0 border-left-0" id="clt_forf_date_end'+x+'" name="datepicker" placeholder="Fin" style="width: 80px"><i id="clt_forf_ti_remove" class="ti-close c-red-500 mL-5" style="cursor: pointer;"></i></div>');
$('#clt_forf_bloc').append(addDIV);
$('#clt_forf_forf'+x, addDIV).autocomplete(autocomp_opt);
$("input[id^='clt_forf_date']", addDIV).datepicker(datepick);
//$('#clt_forf_date_end'+x, addDIV).datepicker(datepick);
}
});
I want to send each value by ajax:
var data = {
op_clt_forf_ref: $("[id^='clt_forf_forf_select']").html(),
op_clt_forf_num: $("[id^='clt_forf_num']").val(),
op_clt_forf_start: $("[id^='clt_forf_date_start']").val(),
op_clt_forf_end: $("[id^='clt_forf_date_end']").val(),
op_clt_forf_nom: $("[id^='clt_forf_forf']").val(),
};
however he only takes me the first div and not the others,
can you help me ?
The Elements that you have created were not present in the DOM earlier. So can't target them directly.
Try doing this:
var data = {
op_clt_forf_ref: $('body #clt_forf_forf_select').html(),
op_clt_forf_num: $('body #clt_forf_num').val(),
op_clt_forf_start: $('body #clt_forf_date_start').val(),
op_clt_forf_end: $('body #clt_forf_date_end').val(),
op_clt_forf_nom: $('body #clt_forf_forf').val(),
};

web scraping data to dashing dashboard

I am having trouble scraping a site to get some information. I am very green with ruby but trying to learn as much as I can. The data will be sent to a Dashing.io dashboard using a google maps widget ( https://github.com/andmcgregor/dashing-map ) to get the current location of our delivery driver. This will be on a screen in our sale office so the staff know exactly where our driver is at a glance.
I have the following code:
require 'mechanize'
require 'open-uri'
require 'restclient'
agent = Mechanize.new
agent.get('http://ilogix.capitaltransport.com.au/iLogixLogin.asp')
agent.page.forms[0]["txtUsername"] = "someusername"
agent.page.forms[0]["txtPassword"] = "somepassword"
agent.page.forms[0].submit
page = agent.get('http://ilogix.capitaltransport.com.au/Home/iLogix_AlertInformation.asp?DisplayType=Comms')
url = puts page.parser.xpath('//table/tr/td/script')
which gets me here
irb(main):014:0* url = puts page.parser.xpath('//table/tr/td/script')
<script type="text/javascript">
//Set Table Headers
var myHeaders = ["Fleet","ID", "Name","Type","Status
","Last Update","Location","Information"];
//Set Grid Object
var obj = new AW.UI.Grid;
obj.setId("myGrid"); // necessary for CSS rules
//Virtual mode
obj.setVirtualMode(true); // Enable virtual mode
//Set Header Text
obj.setHeaderText(myHeaders);
//Set Number of Columns and their width
//Set the Number of columns
obj.setColumnCount(8);
//Set Column Widths
obj.setColumnWidth(130, 0); // set width of the column-1 Vehicle Details
obj.setColumnWidth(45, 1); // set width of the column-3 Fleet
obj.setColumnWidth(100, 2); // set width of the column-4 ID
obj.setColumnWidth(100, 3); // set width of the column-5 DisplayName
obj.setColumnWidth(100, 4); // set width of the column-6 Status
obj.setColumnWidth(130, 5); // set width of the column-7 Last Comms
obj.setColumnWidth(220, 6); // set width of the column-8 Location
obj.setColumnWidth(250, 7); // set width of the column-9 Bad GPS
obj.setCellText("Clutch and Brake (WA)",0,0);
obj.setCellText("7518",1 ,0);
obj.setCellText("7518",2 ,0);
obj.setCellText("Tray (1 T)",3,0);
obj.setCellText("",4,0);
obj.setCellText("26 Jun 2015 15:51:46",5,0);
obj.setCellText("414-424 Beechboro Rd, Morley 6102",6,0);
obj.setCellText(":: since last Communication Activity",7,0);
//number of rows and Height
obj.setRowCount(1);
obj.setRowHeight(20);
//Write Object to the Screen
document.write(obj);
</script>
=> nil
irb(main):015:0>
What I would like is to pull just the location string. I just cant figure it out. After that I think I will need to find a way to convert that to lat and long for use with the widget but I think this http://www.rubygeocoder.com/ will do it.
Here is the html. If someone can help or point me in the right direction I would really appreciate it because I am truly stuck. Thanks.
<html class=" aw-all aw-quirks aw-vista aw-png2" xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body class="Body">
<table cellspacing="0" cellpadding="0" border="0" align="center" style="width:100%;height:100%">
<tbody>
<tr style="height:100%">
<td>
<script type="text/javascript"></script>
<span id="myGrid" class="aw-system-control aw-grid-control aw-selectors-hidden " onselectstart="return false" oncontextmenu="AW(this,event)" hidefocus="true" tabindex="-1">
<span id="myGrid-box" class="aw-grid-box " onactivate="AW(this,event)">
<textarea id="myGrid-box-focus" class="aw-control-focus " onpaste="AW(this,event)" oncopy="AW(this,event)" oncut="AW(this,event)" onbeforecopy="AW(this,event)" onselectstart="AW(this,event)" onbeforedeactivate="AW(this,event)" tabindex="0"></textarea>
<span id="myGrid-scroll" class="aw-scroll-bars aw-scrollbars-none " onmousewheel="AW(this,event)" onresize="AW(this,event)" style="visibility: inherit;">
<span id="myGrid-scroll-box" class="aw-bars-box " onscroll="AW(this,event)"></span>
<span id="myGrid-scroll-content" class="aw-bars-content " style="right: 17px; bottom: 17px;">
<span id="myGrid-view" class="aw-hpanel-template ">
<span id="myGrid-view-box" class="aw-hpanel-box ">
<span id="myGrid-view-box-top" class="aw-hpanel-top " style="height:20px;visibility:inherit;"></span>
<span id="myGrid-view-box-middle" class="aw-hpanel-middle " style="top:20px;bottom:0px;">
<span id="myGrid-rows" class="aw-templates-list aw-grid-view ">
<span id="myGrid-rows-start" class="aw-view-top " style="height:0px;"></span>
<span id="myGrid-row-0" class="aw-templates-list aw-text-normal aw-grid-row aw-row-0 aw-rows-normal aw-alternate-even ">
<span id="myGrid-row-0-start" class="aw-row-start " style="width:0px;"></span>
<span id="myGrid-cell-0-0" class="aw-item-template aw-templates-cell aw-grid-cell aw-column-0 aw-cells-normal " title=""></span>
<span id="myGrid-cell-1-0" class="aw-item-template aw-templates-cell aw-grid-cell aw-column-1 aw-cells-normal " title=""></span>
<span id="myGrid-cell-2-0" class="aw-item-template aw-templates-cell aw-grid-cell aw-column-2 aw-cells-normal " title=""></span>
<span id="myGrid-cell-3-0" class="aw-item-template aw-templates-cell aw-grid-cell aw-column-3 aw-cells-normal " title=""></span>
<span id="myGrid-cell-4-0" class="aw-item-template aw-templates-cell aw-grid-cell aw-column-4 aw-cells-selected " title=""></span>
<span id="myGrid-cell-5-0" class="aw-item-template aw-templates-cell aw-grid-cell aw-column-5 aw-cells-normal " title=""></span>
<span id="myGrid-cell-6-0" class="aw-item-template aw-templates-cell aw-grid-cell aw-column-6 aw-cells-normal " title=""></span>
<span id="myGrid-cell-7-0" class="aw-item-template aw-templates-cell aw-grid-cell aw-column-7 aw-cells-normal " title=""></span>
<span id="myGrid-cell-8-0" class="aw-item-template aw-templates-cell aw-grid-cell aw-column-8 aw-cells-normal " title=""></span>
<span id="myGrid-row-0-end" class="aw-item-template aw-grid-cell aw-column-space "></span>
</span>
<span id="myGrid-rows-end" class="aw-item-template aw-row-selector aw-selector-space "></span>
</span>
</span>
<span id="myGrid-view-box-bottom" class="aw-hpanel-bottom " style="height:0px;display:none;"></span>
</span>
</span>
</span>
</span>
<span id="myGrid-box-sample" class="aw-row-sample aw-grid-row "></span>
</span>
</span>
</td>
</tr>
</tbody>
</table>
</body>
</html>

Request.IsAuthenticated not working in IE repeated partial view MVC3

I have strange problem with an MVC3 application. The application works as expected on Firefox and Chrome but not IE.
I have a partial view that is repeated in a loop on a page. At the bottom of the partial view I am doing a check on Request.IsAuthenticated. If authenticated an ajax form should display. This works on all browsers but IE. In IE if there is more than one instance of the partial view rendered the form does not appear (even when the user is logged in).
I have attached the partial view code below. The form is right at the bottom.
Has anyone else had a similar problem? Any help or thoughts would be great. Thank you
#using HERE_MVC.Extenstions
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
#model HERE_MVC.Models.TilesModel
#{
Layout = String.Empty;
}
#foreach (var t in Model.NearestTiles)
{
<div id="div_tile #t.TileId" style="float: left; height: 235px; width: 300px; border: 0px solid black; margin: 5px;
background-color: #DDDDDD;">
<div id="tile #t.TileId" style="background-image: url(#Url.Action("GetTileBgImageWithCache", "Account", new { userName = #t.aspnet_Users.UserName })); background-repeat:no-repeat; height: 200px; width: 300px; border: 0px solid black;">
#t.aspnet_Users.UserName
<br />
#t.Title
<br />
#t.Quote1
<br />
#t.UpdatedDateTime
</div>
<div id="collectbtn #t.TileId" style="padding-top: 5px;">
#Html.ActionImage("Profile", "Home", new { userName = #t.aspnet_Users.UserName }, "~/content/img/Profile-Icon.jpg", "View Profile", "25")
#if (Request.IsAuthenticated)
{
if (#User.Identity.Name == #t.aspnet_Users.UserName)
{
}
else if (!Model.CheckAlreadyFollowing(#User.Identity.Name, #t.aspnet_Users.UserName))
{
#Ajax.ActionImageLink("../content/img/add.jpg", "Collect Tile", "25", "imgOn1" + #t.aspnet_Users.UserName, "AddFollowedUser", "Home", new { userToFollowName = #t.aspnet_Users.UserName }, new AjaxOptions { HttpMethod = "POST", OnSuccess = " $(#imgOn1" + #t.aspnet_Users.UserName + ").hide();" })
}
else
{
#Ajax.ActionImageLink("../content/img/minus.jpg", "Drop Tile", "25", "imgOff2" + #t.aspnet_Users.UserName, "UnFollow", "Home", new { userToUnFollowName = #t.aspnet_Users.UserName }, new AjaxOptions { HttpMethod = "POST" })
}
}
<a href="http://www.facebook.com/sharer.php?u=#t.ShareLink&t=%22+encodeURIComponent(document.title)" onclick="PopupCenter('http://www.facebook.com/sharer.php?u=#t.ShareLink&t=%22+encodeURIComponent(document.title)', 'NearRoar - Facebook',680,400); return false;" class="pin-it-button" count-layout="none">
<img src="../../Content/img/Facebook-share.jpg" width="25px" alt="Facebook share" /></a>
<a href="http://pinterest.com/pin/create/button/?url=#t.ShareLink&media=#t.PintrestShareImage&description=#t.PinterestShareText" onclick="PopupCenter('http://pinterest.com/pin/create/button/?url=#t.ShareLink&media=#t.PintrestShareImage&description=#t.PinterestShareText', 'NearRoar - Pin It',600,400); return false;" class="pin-it-button" count-layout="none" target="_blank">
<img border="0" src="../../Content/img/pintrest.jpg" title="Pin It" width="25px" /></a>
<a href="https://twitter.com/share" class="twitter-share-button" data-hashtags="#t.TwitterHashTag,NearRoar" data-url="#t.ShareLink" data-count="none" data-lang="en">
</a>
<script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
</div>
<div class="msg_list">
<p class="msg_head #t.TileId">
Comments (<span id='commentCount '#t.TileId>#t.CommentLines.Count</span>)</p>
<div class="msg_body">
#foreach (var c in t.CommentLines)
{
<div class="clearfix">
<div class="msg_body_comment_item_image">
<img width="50" src="#Url.Action("GetImageWithCache", "Home", new { userName = #c.aspnet_Users.UserName })"/>
</div>
<div class="msg_body_comment_item_text">
#c.CommentLine1
<br />
#c.CommentTime
</div>
</div>
}
<div id="newComment #t.TileId">
</div>
<div id="formdiv #t.TileId">
#if (Request.IsAuthenticated)
{
<div>
#using (Ajax.BeginForm("Comment" + #t.TileId, "Home", new { tileId = #t.TileId }, new AjaxOptions { }, new { id = "form" + #t.TileId, name = "form" + #t.TileId }))
{
<fieldset>
#Html.TextBox("commentText " + #t.TileId, null, new { #class = "msg_body_comment_text_input", maxlength = 200 })
<input type="hidden" name="date" id="cmTimeHidden #t.TileId" value="" />
<input type="hidden" name="commentText" id="commentTextHidden #t.TileId" value="" />
<input type="submit" value="Post Comment" onClick="setHiddens(#t.TileId);setNewComment(#t.TileId);"/>
</fieldset>
}
</div>
}
</div>
</div>
</div>
</div>
}
I would double check that rendered markup is valid. It's possible that one of your IDs is duplicated and that IE is just kicking it away.
UPDATE: You have 4 elements with ID t.titleId. As I said above, you can have only one specific ID on the HTML page. Move this to class instead and all should be fine.

jquery ajax form submit plugin not posting file input

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?

Resources