Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Submit Form programmatically from PHP using JavaScript

It´s something quite easy but may help a newby (as I´m now) in the future.
This is a simple function in PHP to insert a JavaScript sentence which will do the same action as if the user would be pressing the Submit Button.
It can be used to use any control as a Submit Button or in case you make a form you need to send without interaction from the user.
the $FormName must be the name specified in the Form tag (i.e.: <form action="MyActionURL" method="post" enctype="application/x-www-form-urlencoded" name="MyFormName">), if you didn´t specified any, you must now :)
Lastly, just a tip regarding a trouble I had and may be you case too... if you have a form with all hidden fields, it won´t work, you have to have at least something echoed inside the form, just a letter can help, anyway, as it will redirect to the action URL, it should not be a problem as the form will disappear.

function SubmitForm($FormName)
//JavaScript function to submit a form programmatically
//$FormName must be form´s name (as specified in the opening form tag)
{
	echo '<script language="javascript">document.'.$FormName.'.submit()</script>';
}

Browser automation using perl LWP

// This is a sample code used to measure reports response times on a OAS application.
#!/usr/bin/perl
#
# LWP connection to the Datamart Portal
# Timing of the main reports
#

use strict;
require LWP::UserAgent;

my $ua = LWP::UserAgent->new;

sub isodate() {
        my ($day, $mon, $year, $hour, $min, $sec) = (localtime)[3, 4, 5, 2, 1, 0];
        $mon++; # 0-based index
        $year = $year + 1900;
        my $date = sprintf ("%04i-%02i-%02i %02i\:%02i\:%02i", $year, $mon, $day, $hour, $min, $sec);
        return $date;
}

sub datamart_login {
        my ( $user, $pass ) = @_;
        my $time_begin=time();
        my $url='http://daprd:7782/portal/page?_pageid=37,134413,37_134422&_dad=portal&_schema=PORTAL';
        my $req = HTTP::Request->new( GET => $url );
        my $resp = $ua->request($req);
        my $loginform = $resp->content ;
        if ( $loginform !~ /Entrez votre nom utilisateur/ ) {
                die isodate()." Failed to get the logon page of the Web Site\n";
        } else {
                my $locale="";
                my ($v) = $loginform =~ /NAME=\"v\" value=\"(.+)\"/;
                my ($site2pstoretoken) = $loginform =~ /NAME=\"site2pstoretoken\" value=\"(.+)\"/;
                my ($submiturl) = $loginform =~ /form method=\"POST\" action=\"(.*?)\"/;
                $resp = $ua->post( $submiturl,
                   [
                     ssousername => $user,
                     password => $pass,
                     v => $v,
                     locale => $locale,
                     site2pstoretoken => $site2pstoretoken
                   ],
                );
                $resp = $ua->get($url);
                $resp = $ua->get($url);
                if ( $resp->content !~ /Crit..res de recherche/ ) {
                        die isodate()." Failed to get the main page of Portal\n";
                }
        }
        print join(";",isodate(),"Time to log on the Portal",time()-$time_begin,$url)."\n";
}

sub datamart_testurl {
        my ($label,$url,$expected)=@_;
        my $time_begin=time();
        my $resp;
        $resp = $ua->get($url);
        $resp = $ua->get($url) if $resp->content !~ /$expected/;        # We try two times !
        if ( $resp->content !~ /$expected/ ) {
                print STDERR isodate()." Test Failed on $label. $expected not found in response.\n";
                print STDERR $resp->as_string;
        } else {
                print join(";",isodate(),$label,time()-$time_begin,$url)."\n";
        }
}

$ua->timeout(1200);
$ua->cookie_jar({});
$ua->agent( 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)' );
#push @{ $ua->requests_redirectable }, 'POST';

# >>>> Main code here

datamart_login("xxxx","xxxx");

open FH,"/exploit/scripts/appli/check_datamart.ini" or die "Unable to open check_datamart.ini";
while (<FH>) {
        chomp();
        my ($report,$expected,$url) = split /;/;
        datamart_testurl($report,$url,$expected);
}
close FH;

# <<<< Main code here

JavaScript: Programmatically Click the Form Submit Button

// Programmatically Click the Form Submit Button
// by using the 'click()' method

  submitTags : function()
  {
    var btnSubmitTags = document.getElementById( TagsHelperConfig.FORM_TAGS_ENTRY_SUBMIT_BUTTON_ID );

    // Programmatically click the submit button
    btnSubmitTags.click();
  }

Mass conversion from word to HTML

I got 1000 word files. Each contains 1 main image and some decorations.
(This is actually a big book scanned into 1-file-per-page format)
I need to extract all the images. What do I do?

Python can do some automation using COM. (or something like that)
import pythoncom, win32com.client

app = win32com.client.gencache.EnsureDispatch("Word.Application")

doc = 'C:\\lang\\try\\bdham\\p1'
app.Documents.Open(doc + '.doc')
app.ActiveDocument.SaveAs(doc + '.html', FileFormat=win32com.client.constants.wdFormatHTML)
app.ActiveDocument.Close()
# now repeat with p2, p3, etc.

Actually, I should put it in a loop. But this non-loop version
is easier to read and remember.

IE Automation in Python

I try to dump my data out of a website. But it block my python's urllib bot. I tried other pure python libray but without success. So, I try a quicker route using IE automation.
For general automation you should try 'watsup'. For IE, there is PAMIE.
>>> from cPAMIE import PAMIE
>>> ie = PAMIE()
>>> ie.Navigate('www.google.com')   # go to google
>>> ie.SetTextBox('Python','q',0)       # search for python
>>> ie.ClickButton('btnG')               # Go!

>>> src = ie.pageText()                 # here it is
>>> cookie = ie.GetCookie()          # Ok, cookie is yummy


PAMIE is actually aimed for more of the testing work.
But in this case it serves me fine.
http://pamie.sourceforge.net/
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS