WWW::Mechanize::Firefox put content into input by id - perl

I am using WWW::Mechanize::Firefox. How to put content into input by id and after that to press link witch, using JS, submit page?
<input id="my_input" type="text" class="some_class"/>
<a id="send_with_me" href="#">Press me</a>
How to do it?
Thanks
Answer to request of Borodin:
my $mech = WWW::Mechanize::Firefox->new(
activate => 1
);
my $content = $mech->get("http://some_url.com");
$mech->field(".my_select_element_by_class", 1); #select element from select by class
I want to put value into input by id. Something like this:
$mech->field("#my_input", 100);
$mech->some_method_witch_press_href("#send_with_me");

You need this to click the link (by CSS selector):
$mech->field("#my_input", 100);
$mech->click({ selector => '#some_id' });

Related

I am using MIME::Lite::TT to send mail with perl. How to save the mail locally before sending

Template
<html>
<body>
<strong>Hi [% first_name %]</strong>,
<p>
This is to confirm your purchase of $ [% amt_due %].
</p>
<p>
Thank you!
</p>
</body>
</html>
`$params{first_name} = 'Frank';
$params{last_name} = 'Wiles';
$params{amt_due} = '24.99';
my $msg = MIME::Lite::TT::HTML->new(
From => 'admin#example.com',
To => 'frank#example.com',
Subject => 'Your recent purchase',
Template => {
text => 'test.txt.tt',
html => 'test.html.tt',
},
TmplOptions => \%options,
TmplParams => \%params,
);
How to save the mail locally before sending. It is having template as html which is populated with params and a pdf attachment.
Is it possible to save the Template with populated values.
MIME::Lite::TT is just a preprocessor; calling MIME::Lite::TT->new returns a normal MIME::Lite object. Just save that object in whatever way you like.
For example, you can print it to a filehandle:
my $email = MIME::Lite::TT->new(...);
$email->print(\*STDOUT);
$email->send;
To print the populated template we can use
$$email{data}
As $email is a reference to a hash and data is a key to the contents of body of email.
To print the whole mail use the above solution.

Open FPDF in new tab

I have a pdf generated (fpdf) from a post form. I would like the pdf to open in a new tab and/or window prompting the user to save the pdf. I'm guessing I need to save the output to a string
$data=$pdf->Output("OfficeForm.pdf", "S");
but what exactly can I do with this string to get it to open in a new window. I've attempted something like this but it's not working. Am I on the right track or is window.open not what I need?
echo "<script type=\"text/javascript\">
window.open('$data', '_blank')
</script>";
If you use a form you can do it by specifying target='_blank' in the -tag (next to where you should have submit='something')
Example:
This will open a new Tab (showing whatever "makepdf.php" produces) on submit.
Hope it answers the question correctly
I simply added target="_blank" to my form opening tag and used $_SESSION[]; to pass my form to the FPDF code:
<?php session_start(); ?>
<form id ="buildPDFform" name="buildPDFform" target="_blank" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
...some code for my form
<input type="submit" name="buildPDf" id="buildPDf" class="buildPDFbutton" value="Build the PDF">
</form>
Then when the form is submitted I gather my form items, put them in an array, create a session the array goes into and use a header("Location: testcode.php") to redirect to where my FPDF code is.
if (isset($_POST['buildPDf'])) {
$pdfArray = array();
foreach ($_POST as $key => $value) {
...gather your form items into your array
}
$_SESSION['pdfArray'] = $pdfArray;
header("Location: testcode.php");
}
And don't forget in your FPDF code file (testcode.php in my case) to grab your session that has the array.
<?php
session_start();
$pdfArray = $_SESSION['pdfArray'];
... your FPDF code
$pdf->Output('I');
?>
source: https://www.thesitewizard.com/html-tutorial/open-links-in-new-window-or-tab.shtml
use target="_blank" in your a tag to open it to new tab
Try $pdf->Output("OfficeForm.pdf", "I");

cakephp edit form with file type not overidding http with put and post occurs twice with empty data

First off I'm new to cakephp.... I'm pulling survey questions from a database and building a form of type=file.
echo $this->Form->create('PersonalDetail', array('type' => 'file', 'id' => 'editProfileForm', 'class' => 'form-horizontal'));
echo $this->Form->hidden('id');
echo $this->Form->hidden('PersonalDetail.id');
echo $this->Form->input('PersonalDetail.field_name', array('label' => false, 'div' => false, 'readonly' => false));
echo $this->Form->submit('Update Profile', array('class' => 'btn btn-primary', 'id' => 'editProfileSubmitBtn'));
echo $this->Form->end();
According to cakephp docs "Since this is an edit form, a hidden input field is generated to override the default HTTP method." But I can't seem to figure out how to tell cake this is an edit form. It always inserts a hidden POST not PUT method:
<form action="/editForm" id="editProfileForm" class="form-horizontal" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<div style="display:none;">
<input type="hidden" name="_method" value="POST"/>
<input type="hidden" name="data[_Token][key]" value="ff8b198e82d800a35581" id="Token836"/></div>
<input type="hidden" name="data[id]" id="id"/>
<input type="hidden" name="data[PersonalDetail][id]" id="PersonalDetailsId"/>
<label class="control-label required">Username</label>
<input name="data[PersonalDetail][field_name]" maxlength="255" type="text" id="PersonalDetailsFieldName"/>
<input class="btn btn-primary" id="editProfileSubmitBtn" type="submit" value="Update Profile"/>
<div style="display:none;">
<input type="hidden" name="data[_Token][fields]" value="a2f722badf82c0d8991ab8%3APersonalDetail.id%7Cid" id="TokenField020"/> <input type="hidden" name="data[_Token][unlocked]" value="" id="TokenUnlocked1562820470"/> </div></form>
The problem is when I submit the form and watch with Firefox's Tamper Data the form posts the data fine, but then it posts again immediately again with all the data missing.
On a working form example, I see the same behaviour, except the hidden input field is "PUT" and when the form submits, it is first a PUT with data, then the immediate second submission is with the POST with data instead of begin blank.
I assume I'm missing something basic here, but I'm really confused.
Here's the controller where PersonalForm is a database of questions passed to an element that builds the forms. PersonalDetail is supposed contain the answers but for this first time this is run the user won't have any answers.
public function editForm() {
$userId = $this->UserAuth->getUserId();
if (!empty($userId)) {
$user_account_type = $this->UserDetail->read('account_type', $userId);
$user_account_type = $user_account_type['UserDetail']['account_type'];
$this->set('user_acct_type', $user_account_type);
$this->loadModel('Usermgmt.PersonalForm');
$forms = $this->PersonalForm->find('all');
$this->set('forms', $forms);
if ($this->request->isPut() || $this->request->isPost()) {
//put in ajax verification
//$this->PersonalDetail->saveAssociated($this->request->data);
$this->Session->setFlash(__('Your answers have been successfully updated'));
$this->redirect('/dashboard');
} else {
// read user's original responses and populate form
$this->loadModel('Usermgmt.PersonalDetail');
$answers = $this->PersonalDetail->read(null, $userId);
$this->request->data = null;
if (!empty($answers)) {
$this->request->data = $answers;
}
}
} else {
$this->redirect('/dashboard');
}
}
I'm using cakephp 2.3.7 and I'm running the debugKit plugin (maybe causing more than one submission? I don't know.) Edit: Also I'm using UserAuth and Security modules.
EDIT: I oversimplified the example when I removed the hidden id fields. Now I included the two hidden input elements. However the first time this form is loaded there is no edit data so it is a create instead of add case. So I don't understand why it is posting twice and losing the data on the second post. Perhaps that is the real problem and not that it should be PUT vs POST? I'm obviously missing something fundamental in how cake is processing the post data.
Perhaps I should mention this is form is part of a plugin. Could the routing have something to do with the loss of data and the second post?
You are missing the vital part of an edit form, the id:
echo $this->Form->input('id');
Without its presence cake assumes that this is not an update (edit), but a create (add).
Also mind your casing, its not $this->Form->Submit() but $this->Form->submit().
EDIT:
At second look: I also guess that you violated more than 5 other conventions, including the most important one: Models are singular, Controllers plural. Meaning:
$this->Form->create('PersonalDetail');
If your model is PersonalDetail (which from your controller code it looks like).
This would explain why the data doesnt end up where it is supposed to.
Again my recommendation: Bake your code to see how its done.
It appears this is related to a security module problem.
I was able to prevent the double empty data posts by adding the following to the beforeFilter:
if (isset($this->Security) && ($this->RequestHandler->isAjax() || $this->action == 'editForm')) {
$this->Security->csrfCheck = false;
$this->Security->validatePost = false;
}
Now I need to research the reasons for this security problem with my form to fix it.

How to send a POST image url variable through a form in Laravel 4

I am trying to upload an image url through a form in Laravel 4.
Without a framework, I would use something like this:
<div id="myResults" value="<php echo h($_POST['img'])"></div>
where the image url is sent to the id "myResults" by a javascript file.
In the javascript:
document.getElementById("myResults").innerHTML = "<img src='" + FPFile.url + "' width='200px' height='200px'>";
I would use the h($_POST['img'])" value to get the url and then store it in the database upon the submit click of the form.
My question is how can I do this same function in Laravel 4? Using the form:
{{ Form::open(array('route' => 'artists.store')) }}
{{Form::submit('Submit', null, array(
'class' => 'button',
));}}
Thank you for your help.
{{ Form::file('myImage') }}
Seriously, it's that easy! See: http://laravel.com/docs/html
Then, to retrieve the file: $file = Input::file('myImage');
See: http://laravel.com/docs/requests#files
I'm not really understanding your question.
But equivalent of
<div id="myResults" value="<php echo h($_POST['img'])"></div>
in Blade is...
<div id="myResults" value="{{{ Input::get("img") }}}"></div>
Is that what you were asking?

iFrame App. Permissions Request?

I want to request permissions when the user first clicks my iFrame Facebook application. The problem is the examples I have seen force the user to click a button to load the http://www.facebook.com/authorize.php URL.
Is there a way to iframe the authorize.php page in my application? I've seen it done before but can't find out how.
If I currently try it, it shows the "go to facebook box". The method I seen changes the href or something on the browser.
Any ideas?
I do something like this in one of my iframe applications (I've simplified the actual code for this example)
$fbSession = $facebook->getSession();
if ( $fbSession )
{
$url = $facebook->getLoginUrl( array(
'canvas' => 1
, 'fbconnect' => 0
, 'req_perms' => 'list,of,perms'
, 'display' => 'page'
) );
include( 'redirect.php' );
exit;
}
Then, redirect.php
<script type="text/javascript">
top.location.href = '<?php echo $url; ?>';
</script>
<p>
Not being redirected? Click Here.
</p>
no need to complicate: target="_top" is the answer!
Best!
m