I've successfully logged into a website and loaded a table that can then be downloaded as an .xls file. The issue I'm having is that an image has to be clicked to call the download. I haven't figured out how to post the correct data to fetch the .xls file.
Here is the website's HTML:
<table border="0" width ="95%" align ="center">
<tr>
<form name="downloadXLSForm" method="POST" action="/control/filedownload">
<input type="hidden" name="fileType" value="mlInventory">
<td align="left" valign = "top">
<font face="Verdana, Arial, Helvetica, sans-serif" size="-2">
<input type="image" src="../images/excel_download.jpg" onClick="document.downloadXLSForm.submit()" value="Excel Download" Alt="Download the results above into MS Excel">
</font>
</td>
</form>
<td align="right" valign = "top">
<form name="mlInventorysearch" method="post" action="/control/mlinventorysearch">
<input type="hidden" name="refreshSearch" value="true">
<input type="submit" value="New Search">
</form>
</td>
</tr>
</table>
There is a link embedded in the website that points to https://www.emlco.com/control/filedownload but when I try and get it, the page is empty.
Here is my code:
with requests.Session() as z:
z.get("https://www.emlco.com/control/j_security_check", stream=True)
p3 = z.post("https://www.emlco.com/control/j_security_check",data=payload3)
z.get(link)
inventory_start = z.post(link, data={'newSearch':'true'})
inventory_excel = z.post(link,data={'fileType':'mlInventory'})
#inventory_excel = z.post('https://www.emlco.com/control/filedownload',data={'fileType':'mlInventory'})
inventory_excel = z.get('https://www.emlco.com/control/filedownload', stream = True)
#output = open('MLCStock.xls', 'wb')
#output.write(inventory_excel.content)
#output.close()
print(inventory_excel.url)
print(inventory_start.url)
print("Done")
I apologize for the redundant comments. I was trying various combinations to see what would take.
Thanks in advance. I am quite new at Python.
Related
A web site is developped in asp.net and let people choose pictures to download.
The website groups them togheter and people can then push a download button per picture.
As we never know how many pictures that eventually will be downloaded the button are dynamically generated.
I'm not permitted to alter the asp.net code on the server only in the piwik part...
The problem is that also the id's are dynamically generated..
I have tried with this:
$("h2").on("click", "a.button button-dl", function(){
alert($(this).text());
});
But got no result, In this case I would like to retrieve "276173" in a variable.
I hope that somebody can help me, thanks for reading
Guy
<table class="checkout-basket" cellspacing="0" rules="all" border="1" id="ctl00_ctl00_cphContent_cphContent_gvBasketitem" style="border-collapse:collapse;">
<tr>
<th scope="col"> </th><th scope="col"> </th><th scope="col">Asset</th><th scope="col"> </th>
</tr><tr>
<td style="width:30px;">
<input id="ctl00_ctl00_cphContent_cphContent_gvBasketitem_ctl02_CbFlag" type="checkbox" name="ctl00$ctl00$cphContent$cphContent$gvBasketitem$ctl02$CbFlag" />
</td><td style="width:80px;">
<a onclick="return confirm('Bent u zeker dat u deze asset wil verwijderen?');" id="ctl00_ctl00_cphContent_cphContent_gvBasketitem_ctl02_lbDelete" class="button" href="javascript:__doPostBack('ctl00$ctl00$cphContent$cphContent$gvBasketitem$ctl02$lbDelete','')">verwijder</a>
</td><td style="width:100px;">
<img id="ctl00_ctl00_cphContent_cphContent_gvBasketitem_ctl02_Image1" src="../../cache/images/thumb/3AF267A169AAED70770F0EEE7E74FBB61A526EBB_156.jpg" style="width:80px;border-width:0px;" />
</td>
<td>
<h2>
<a href='/nl-BE/asset/276173/'>
276173
</a>
</h2>
<h2>
<a href='/nl-BE/asset/276173/'>
20151109_113939_0040
</a>
</h2>
<a class="button button-dl" href="javascript:__doPostBack('ctl00$ctl00$cphContent$cphContent$gvBasketitem$ctl02$ctl00','')" style="margin-top: 4px;"><img style="position: relative; top: 4px; right: 5px" src="/images/picto/download.png" />Download</a>
</td><td>
<div id="ctl00_ctl00_cphContent_cphContent_gvBasketitem_ctl02_pnl">
<input type="hidden" name="ctl00$ctl00$cphContent$cphContent$gvBasketitem$ctl02$hfAsset" id="ctl00_ctl00_cphContent_cphContent_gvBasketitem_ctl02_hfAsset" value="276173" />
<span id="ctl00_ctl00_cphContent_cphContent_gvBasketitem_ctl02_rblPrint" class="rbl"><input id="ctl00_ctl00_cphContent_cphContent_gvBasketitem_ctl02_rblPrint_0" type="radio" name="ctl00$ctl00$cphContent$cphContent$gvBasketitem$ctl02$rblPrint" value="False" checked="checked" /><label for="ctl00_ctl00_cphContent_cphContent_gvBasketitem_ctl02_rblPrint_0">Download</label><br /><input id="ctl00_ctl00_cphContent_cphContent_gvBasketitem_ctl02_rblPrint_1" type="radio" name="ctl00$ctl00$cphContent$cphContent$gvBasketitem$ctl02$rblPrint" value="True" /><label for="ctl00_ctl00_cphContent_cphContent_gvBasketitem_ctl02_rblPrint_1">Print</label></span>
<div class="printFormat" style="display: none; padding: 3px 0 3px 20px;">
<select name="ctl00$ctl00$cphContent$cphContent$gvBasketitem$ctl02$ddlPrint" id="ctl00_ctl00_cphContent_cphContent_gvBasketitem_ctl02_ddlPrint">
<option selected="selected" value="">--</option>
<option value="9cmx13cm">9cmx13cm</option>
<option value="10cmx15cm">10cmx15cm</option>
</select>
</div>
</div>
</td>
</tr><tr>
I eventually found a solution thanks to the answer below..
This is what I used
var newstr = "1";
var addressValue = "1";
$(".button.button-dl").click(function () {
var addressValue = $(this).attr('href');
console.log("tweede ",addressValue );
var re = /'(.*?)'/;
var re2 = /\$/gi;
var m = addressValue.match(re);
if (m != null)
idVal = (m[0].replace(re, '$1'));
console.log("idVal ",idVal);
var newstr = idVal.replace(/\$/gi, "_").slice(0,-6)+"_hfAsset";
console.log("newstr ",newstr);
console.log(document.getElementById(newstr).value);
});
You have a hierarchy problem:
First, a CSS problem: "a.button button-dl" should be "a.button.button-dl" (both classes are on the anchor tag).
Second, that anchor tag is not contained within an h2, so your event is never triggering. At best, you can use the td tag.
So your JS might look like:
$("td").on("click", "a.button.button-dl", function () {
alert($(this).text());
});
I was following some tutorials about ecommerce site in PHP and MySQL.
But when i hit submit button I get OBJECT NOT FOUND ERROR.
Below is the code,please guide me. Directories etc. are fine. I've also tried enclosing the button in seprate form tag but nothing worked.
<?php
include("includes/db.php");
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Add Products</title>
</head>
<body bgcolor="#3B6AD8">
<form method="post" action="insert_product" enctype="multipart/form-data">
<table width="700" height="650" align="center" border="2" bgcolor="#F3F3F3">
<tr>
<td colspan="2">
<h2 align="center">Insert Product Here</h2>
</td>
</tr>
<tr>
<td><b>Product Title</b></td>
<td><input type="text" name="product_title" /></td>
</tr>
<tr>
<td><b>Product Category</b></td>
<td>
<select name="product_cat">
<option>Select a Category</option>
<?php
$get_categories = "SELECT * FROM categories";
$run_categories = mysqli_query($con,$get_categories);
while($row_categories =mysqli_fetch_array($run_categories) ) {
$cat_id = $row_categories['cat_ID'];
$cat_title = $row_categories ['cat_title'];
echo "<option value='$cat_id'>$cat_title</option>";
}
?>
</td>
</select>
<tr>
<td><b>Product Brand</b></td>
<td><select name="product_brand">
<option>Select a Category</option>
<?php
$get_brands = "SELECT * FROM brands";
$run_brands = mysqli_query($con,$get_brands);
while($row_brands =mysqli_fetch_array($run_brands) ) {
$brand_id = $row_brands['brand_ID'];
$brand_title = $row_brands['brand_title'];
echo "<option value='$brand_id'>$brand_title</option>";
}
?>
</td>
</select>
</td>
</tr>
<tr>
<td><b>Product Image 1</b></td>
<td>
<input type="file" name="product_img1" />
</td>
</tr>
<tr>
<td><b>Product Image 2</b></td>
<td>
<input type="file" name="product_img2" />
</td>
</tr>
<tr>
<td><b>Product Image 3</b></td>
<td>
<input type="file" name="product_img3" />
</td>
</tr>
<tr>
<td><b>Product Price</b></td>
<td>
<input type="text" name="product_price" />
</td>
</tr>
<tr>
<td><b>Product Description</b></td>
<td>
<textarea name="product_desc" cols="30" rows="5"></textarea>
</td>
</tr>
<tr>
<td>Product Keywords</td>
<td>
<input type="text" name="product_keywords" />
</td>
</tr>
<tr align="center">
<td colspan="2">
<form method="post">
<input type="submit" name="add_product" value="Add Product"/>
</form>
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['add_product'])){
//text data variables
$product_title = $_POST['product_title'];
$product_cat = $_POST['product_cat'];
$product_brand = $_POST['product_brand'];
$product_price = $_POST['product_price'];
$product_desc = $_POST['product_desc'];
$product_keywords = $_POST['product_keywords'];
$product_status = 'on';
//Produt Images
$product_img1 = $_FILES['product_img1']['name'];
$product_img2 = $_FILES['product_img2']['name'];
$product_img3 = $_FILES['product_img3']['name'];
//Image Temp Names
$temp_name1 = $_FILES['product_img1']['tmp_name'];
$temp_name2 = $_FILES['product_img2']['tmp_name'];
$temp_name3 = $_FILES['product_img3']['tmp_name'];
if($product_title=="" OR $product_cat=="" OR $product_brand=="" OR $product_price=="" OR $product_desc=="" OR $product_keywords=="" OR $product_img1==""){
echo "<script>alert('Please Fill All The Fields')</script>";
exit();
}
else {
move_uploaded_file($temp_name1,"product_images/product_img1");
move_uploaded_file($temp_name2,"product_images/product_img2");
move_uploaded_file($temp_name3,"product_images/product_img3");
$insert_product_query = "INSERT INTO products(category_ID,brand_ID,date,product_title,product_img1,
product_img2,product_img3,product_price,product_desc,product_status)
values ('$product_cat','$brand_id',
NOW,'$product_title','$product_img1','$product_img2','$product_img3','$product_price','$product_desc',
'$product_status')";
$run_products = mysqli_query($con,$insert_product_query);
if ($run_products){
/*echo "<script>alert('Product inserted successfully')</script>";*/
}
}
}
?>
try to put an extension to your form action="insert_product" ... like .php or .asp .. Also, reform your title to something more like "Object not found on form submit."
Try this:
<form method="post" enctype="multipart/form-data">
Ther are two problems:-
The problem is you write form contains only submit button.Try to remove <form method="post">before button
You write all your code in same page but in form your action is insert_product which have no extension like .php.It's totally wrong
so try to do any one of these two.
a) either define it action= insert_product.php and make a file with that name and put your last form processing code in that file.
b) Or remove form action attribute and write like <form method="post" enctype="multipart/form-data">.
Try 1 point and 2 nd b) point at-once. And check once.
I am trying to create an automatic file download when a visitor submits a validated opt-in form. I would like to initiate the download using a Javascript function as an onsubmit() event. Here is the code I am working with I am already using the form action= to submit the form data to a database and there is already Javascript in place to validate the form. I just need to add the download function.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css" xml:space="preserve">
BODY, P,TD{ font-family: Arial,Verdana,Helvetica, sans-serif; font-size: 10pt }
A{font-family: Arial,Verdana,Helvetica, sans-serif;}
B { font-family : Arial, Helvetica, sans-serif; font-size : 12px;font-
weight
: bold;}
.error_strings{ font-family:Verdana; font-size:14px; color:#660000;}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js">
</script>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script language="JavaScript" src="gen_validatorv4.js"
type="text/javascript" xml:space="preserve"></script>
<script type="text/javascipt">
var downloadURL = function downloadURL(url) {
var iframe;
var hiddenIFrameID = 'hiddenDownloader';
iframe = document.getElementById(hiddenIFrameID);
if (iframe === null) {
iframe = document.createElement('iframe');
iframe.id = hiddenIFrameID;
iframe.style.display = 'none';
document.body.appendChild(iframe);
}
iframe.src = 'http://healthyweightnaturally.com/file-download/download.php?
download_file=integrative_nutrition_ebook.pdf';
}
</script>
</head>
<body>
<form name="myform" id="myform" method="post"
action="https://www.example.com/FormHTML.aspx" onSubmit="return downloadURL(url);">
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td align="right">
First Name
</td>
<td>
<input type="text" name="FirstName" />
</td>
</tr>
<tr>
<td align="right">
Last Name
</td>
<td>
<input type="text" name="LastName" />
</td>
</tr>
<tr>
<td align="right">
Email *
</td>
<td>
<input type="text" name="Email" />
</td>
</tr>
<tr>
<td align="right"></td>
<td>
<div id="myform_errorloc" class="error_strings">
</div>
</td>
</tr>
<tr>
<td align="right"></td>
<td>
<input style="padding-left:10px;" src="http://healthyweightnaturally.com/file-
download/download-button.png" type="image" value="submit" value="Submit" />
</td>
</tr>
</table>
<div style="visibility:hidden">
<iframe name="ifr1" width="20" height="20" id="hiddenIFrameID"></iframe>
</div>
</form>
<script language="JavaScript" type="text/javascript"
xml:space="preserve">//<![CDATA[
//You should create the validator only after the definition of the HTML form
var frmvalidator = new Validator("myform");
frmvalidator.EnableOnPageErrorDisplaySingleBox();
frmvalidator.EnableMsgsTogether();
frmvalidator.addValidation("FirstName","req","Please enter your First Name");
frmvalidator.addValidation("FirstName","maxlen=20", "Max length for FirstName
is 20");
frmvalidator.addValidation("FirstName","alpha_s","Name can contain alphabetic chars
only");
frmvalidator.addValidation("LastName","req","Please enter your Last Name");
frmvalidator.addValidation("LastName","maxlen=20","For LastName, Max length is 20");
frmvalidator.addValidation("Email","maxlen=50");
frmvalidator.addValidation("Email","req");
frmvalidator.addValidation("Email","email");
//]]>
</script>
</body>
</html>
I want to use onsubmit instead of onclick because I need to make sure that the form has been validated before the file is able to be downloaded.
I am somewhat new to Javascript and have been struggling with this. I would greatly appreciate someone's assistance. The closest thing I found to an answer can be seen at HTML OnSubmit: Download OR HTML, but since they never gave any code examples it does not help me see the solution.
You can use jQuery plugin http://jqueryfiledownload.apphb.com/, which I found here Download File Using Javascript/jQuery.
I'm trying to write my own webmail application using python cgi with smtplib and imaplib. When I hit the send button on the "send" form the email is sent, however, for every refresh after that the message is sent again. The page (cgi script) is refreshed automatically every two minutes via javascript:
<script language="javascript">
window.setInterval('refresh()', 180000);
function refresh()
{
// this checks to see which tab is selected, page is refreshed only if compose tab is not selected
elList = document.getElementsByTagName("a");
for (i = 0; i < elList.length; i++)
{
if (elList[i].className === 'tab active')
{
var activeTab = elList[i].innerHTML;
}
}
if (activeTab.indexOf("Unread") >= 0)
{
window.location.reload(true);
}
}
</script>
I can't really tell whether the form data is sent multiple times or just read multiple times. However, I see no reason why the form should be submitted again on refresh so I have to believe that it is just being read multiple times. Here are the components, which enable the reading of the form data:
# Create instance of FieldStorage
form = cgi.FieldStorage()
if form.keys() != []:
for x in form.keys():
if x == 'send':
send_email()
I tried to reset the form using javascript in the page head tag, but this did not work:
<script language="javascript">
Form.reset('send'); // reset send form so email is not sent again
</script>
For completeness, here is the form definition:
<form name="send" action="/cgi-bin/myemail.py" method="post" >
<table id="example" class="compose" >
<tr>
<td><input type="submit" value="To:" style="height: 1.95em; width: 4em"></td><td><input type="text" name="send_to" size="90"></td><td><input type="submit" name="send" value="SEND" style="height: 1.95em; width: 12em"></td>
</tr>
<tr>
<td><input type="submit" value="Cc:" style="height: 1.95em; width: 4em"></td><td><input type="text" name="send_cc" size="90"></td> <td><input type="submit" name="send" value="SAVE" style="height: 1.95em; width: 12em"></td>
</tr>
<tr>
<td><input type="submit" value="Bcc:" style="height: 1.95em; width: 4em"></td><td><input type="text" name="send_bcc" size="90"></td> <td><input type="submit" name="send" value="DELETE" style="height: 1.95em; width: 12em"></td>
</tr>
<tr>
<td><input type="submit" value="Subj:" style="height: 1.95em; width: 4em"></td><td><input type="text" name="send_subj" size="90"></td> <td><input type="submit" name="send" size="200" value="ATTACH" style="height: 1.95em; width: 12em"></td>
</tr>
<tr>
<td colspan="3">
<!-- <td colspan="3"> <div id="result" class="body" style="background-color:#000000"></div></td> -->
<textarea name="send_body" cols="118" rows="23"> </textarea>
</td>
</tr>
</table>
</form>
How can I keep this form data from being read multiple times? Thanks.
This problem seems to be odd to me and I can't seem to fix it. I do have a simple HTML form; inside, I do have a textbox and a button as shown down here:
<form id="form1" method="get"> <!-- Note: No Action property -->
<div>
<h1>
Simple Test Form</h1>
<table border="0" width="400">
<tr>
<td align="right">
All of these words
</td>
<td>
<input name="txtAll" id="txtAll" type="text" value="testing keyword" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="Submit" value="Submit" onclick="myJS(this);" />
</td>
</tr>
</table>
</div>
</form>
MyJS file is as:
<script type="text/javascript">
function myJS(which) {
var CONSTANT_SITE_TARGET = "http://servername/redirect/target.aspx?text=";
//capture all values from the form
var allWords = document.getElementById("txtAll").value;
var morestring = ""; //More data manipulation here.....
var url = CONSTANT_SITE_TARGET + allWords + morestring;
window.open(url);
//window.location = url; //Doesn't work
//window.location.href = url; //Doesn't work
//self.location = url; //Doesn't work
//top.location = url; //Doesn't work
}
</script>
As you can see, it doesn't redirect to the designated URL in the javascript. When I use the window.open then it works. Note that in the < form... > tag, I don't put the action property in it. I don't want to open a new browser, just redirect to the new url within the same browser.
Is there a way to redirect it?
Don't use the form tags. Or, set the "type" attribute of your button to be "button", not "submit". The form submits when you click the button, but you don't want that to happen. Either removing the form or changing the button should fix improper redirection. When you don't specify an action, I'm pretty sure the default is the current URL.
Ok, just an idea. As you haven't set the action parameter, the default behaviour of a submit button is to reload the same page. You alter that behaviour by handling its onclick. Maybe there is a conflict that can be resolved by having return false; at the end of the click handler, which prevents the default action for that event.
Here
function myJS(which) {
var CONSTANT_SITE_TARGET = "http://servername/redirect/target.aspx?text=";
//capture all values from the form
var allWords = which.txtAll.value;
var morestring = ""; //More data manipulation here.....
return CONSTANT_SITE_TARGET + allWords + morestring;
}
<form id="form1" method="get" onsubmit="this.action=myJs(this)">
<div>
<h1>
Simple Test Form</h1>
<table border="0" width="400">
<tr>
<td align="right">
All of these words
</td>
<td>
<input name="txtAll" id="txtAll" type="text" value="testing keyword" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="Submit" value="Submit" />
</td>
</tr>
</table>
</div>
</form>
to elaborate on a comment:
<script>
window.onload=function() {
document.getElementById("Submit").onclick=function() {
var CONSTANT_SITE_TARGET = "http://servername/redirect/target.aspx?text=";
//capture all values from the form
var allWords = document.getElementById("txtAll".value;
var morestring = ""; //More data manipulation here.....
location = CONSTANT_SITE_TARGET + allWords + morestring;
}
}
</script>
<div>
<h1>
Simple Test Form</h1>
<table border="0" width="400">
<tr>
<td align="right">
All of these words
</td>
<td>
<input name="txtAll" id="txtAll" type="text" value="testing keyword" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" id="Submit" value="Submit" />
</td>
</tr>
</table>
</div>
Try one the options for redirection below. I commented out three of them so that the code stays valid, but you may play around with either and see if it suits your needs.
var CONSTANT_SITE_TARGET = "http://servername/redirect/target.aspx?text=";
//capture all values from the form
var allWords = document.getElementById("txtAll").value;
var morestring = ""; //More data manipulation here.....
var url = CONSTANT_SITE_TARGET + allWords + morestring;
window.location.href= url;
//window.navigate(url);
//self.location(url);
//top.location(url);