How to get the post vars from a form via user function - forms

On a Typo3 website a form is integrated. The action should be routed to a typoscript user function.
This is what I tried so far:
The fluid form code (excerpt):
<form action="{f:cObject(typoscriptObjectPath: 'lib.mynlreg')}" method="post">
<input type="text" name="email" placeholder="Ihre E-Mail-Adresse">
<input type="submit" name="send" value="Jetzt registrieren" class="submit" />
</form>
The typoscript lib:
lib.mynlreg = USER_INT
lib.mynlreg {
userFunc = Vendor\Extension\myClass->myFunction
}
And the class:
class myClass {
public function myFunction($content, $conf) {
$arguments = $this->request->getArguments();
$formEmail = $arguments['email'];
return '<div>' . $formEmail . '</div>';
}
}
I expect to get the content of the form field "email", but after submitting the page throws an error. The question is, how do I get the post vars into the user function? Thank you for any help!

$this->request is not available in a userFunc. As gautamsinh mori says, you should use \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('email');, however I'm not sure you understand what the f:cObject ViewHelper does.
With this code, your HTML before submitting the form will be:
<form action="<div></div>" method="post">
<input type="text" name="email" placeholder="Ihre E-Mail-Adresse">
<input type="submit" name="send" value="Jetzt registrieren" class="submit" />
</form>
Your HTML after submitting will be:
<form action="<div>filledInEmail</div>" method="post">
<input type="text" name="email" placeholder="Ihre E-Mail-Adresse">
<input type="submit" name="send" value="Jetzt registrieren" class="submit" />
</form>
I'd recommend making an extension for this, but if you really want/need to do it like this, I think what you're looking for is something like:
<f:cObject typoscriptObjectPath="lib.mynlreg" />
<form action="{uri.page(addQueryString: 1)}" method="post">
<input type="text" name="email" placeholder="Ihre E-Mail-Adresse">
<input type="submit" name="send" value="Jetzt registrieren" class="submit" />
</form>
This will create the form with action to the current page (including any query string). You then have to change the userFunc to return an empty string if the form hasn't been submitted. Something like:
class myClass {
public function myFunction($content, $conf) {
$formEmail = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('email');
if (empty($formEmail)) {
return '';
}
return '<div>' . $formEmail . '</div>';
}
}

Related

TYPO3 Extbase form referrer wrong casing

I made a plugin which allows frontend users to write news articles. This works fine on the Mac locally. However the New action gives a wrong casing in the __referrer of the form, resulting on the Linux server (which is sensitive for casing) in:
Class Pen\Pennews\Controller\newsController does not exist. Reflection failed.
My controller
class NewsController extends NewsBaseController
...
/**
* New action
*
* #return void
*/
public function newAction()
{
$feUserUid = $GLOBALS['TSFE']->fe_user->user['uid'];
/** #var User $user */
$user = $this->frontendUserRepository->findByUid($feUserUid);
$this->view->assignMultiple([
'user' => $user
]);
}
The form
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
data-namespace-typo3-fluid="true">
<f:layout name="Default"/>
<f:section name="content">
<f:form action="create" name="newNews" controller="News" object="{newNews}">
<label for="pennews-title" class="pennews-title">Titel</label>
<f:form.textfield property="title" id="pennews-title" placeholder="Onderwep" required="required"/>
<label for="pennews-teaser" class="pennews-teaser">Teaser</label>
<f:form.textarea id="pennews-teaser" class="pennews-teaser-newPost" name="teaser" rows="3" placeholder="Korte teaser - optioneel"/>
<label for="pennews-bodytext" class="pennews-bodytext">Content</label>
<f:form.textarea id="pennews-bodytext" class="pennews-bodytext-newPost pen-form-textarea" name="bodytext" placeholder="Bericht"/>
<div class="pen-button pennews-new-button text-align-right">
<button class="button pennews-submit-news icon-left" type="submit"><i class="fas fa-pen-square"></i>Maak nieuw bericht</button>
</div>
</f:form>
</f:section>
</html>
Part of the source of the form (taken from local dev) :
<form name="newNews"
action="/vlaggensite?tx_pennews_entry%5Baction%5D=create&tx_pennews_entry%5Bcontroller%5D=News&cHash=4cc2809be1ea9f2f2d3e07a8e396981f"
method="post">
<div>
<input type="hidden" name="tx_pennews_entry[__referrer][#extension]" value="Pennews"/>
<input type="hidden" name="tx_pennews_entry[__referrer][#vendor]" value="Pen"/>
<input type="hidden" name="tx_pennews_entry[__referrer][#controller]" value="news"/>
<input type="hidden" name="tx_pennews_entry[__referrer][#action]" value="new"/>
<input type="hidden" name="tx_pennews_entry[__referrer][arguments]"
value="YTowOnt9dd9a70668d7db0e58fe5097a80fa26ab79028541"/>
<input type="hidden" name="tx_pennews_entry[__referrer][#request]"
value="a:4:{s:10:"#extension";s:7:"Pennews";s:11:"#controller";s:4:"news";s:7:"#action";s:3:"new";s:7:"#vendor";s:3:"Pen";}316417f03bf11f3d860053d7f0c1286795c3db22"/>
<input type="hidden" name="tx_pennews_entry[__trustedProperties]"
value="a:3:{s:7:"newNews";a:1:{s:5:"title";i:1;}s:6:"teaser";i:1;s:8:"bodytext";i:1;}b6e10b48e7d3acab8926ba3318ce3ce1fed6ea2d"/>
</div>
As can be visible in the action, the controller is there News with a capital. However in the refererer the controller has the value "news" - lowercase. What could cause this or how can I fix it with a capital as first letter?
Solved. On ext_localconf.php I had the controllername lowercase.

submitting form to controller#store

laravel 5.5
voyager 1.0
i'm trying to submit a form to a controller with :
<form class="" action="FeedController#store" method="post">
<div class="mui-textfield mui-textfield--float-label">
<input id="title" type="text" name="title" value="" required autofocus>
<label for="title" class="col-md-4 control-label">title</label>
</div>
<div class="mui-textfield mui-textfield--float-label">
<textarea id="description" type="textarea" name="description" value="" required autofocus></textarea>
<label for="description" class="col-md-4 control-label">description</label>
</div>
<input class="mui-btn btn-primary pull-right " type="submit" name="" value="submit">
</form>
within FeedController:
/**
*#param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request){
$data = ReportDatum::create([
'owner_id' => auth()->id(),
'title' => request('title'),
'description' => request('description')
]);
return test;
}
and in the routes:
Route::resource('feed', 'FeedController');
(and/or depending on what I'm trying)
Route::post('store', 'FeedController#store');
what I'm i missing to correctly submit the form?
I'm not using laravelCollective forms
You're missing the csrf_field(). Try adding it after the <form> tag.
Ref: https://laravel.com/docs/5.5/csrf
You should define route at first then it will works:
Resource controller routing
Route::resource('feed', 'FeedController');
or specific simple routing
Route::post('feed', 'FeedController#store');
Same error here. I've commented the line \Greazy\Http\Middleware\VerifyCsrfToken::class which stays in app/Http/Kernel.php and the post method worked in Postman, however it does not work when invoked on the form.

How to pass data from a form in a view to another controller with C# MVC?

If I have a form that needs to use a textbox input like as below:
#{
if(IsPost){
username = Request.Form["username"]
}
}
<form action="Home/Index" method="post">
<input type="text" name="username" />
<input type="submit" value="submit" />
</form>
The controller is something like below,
public class HomeController : Controller
{
public ActionResult Index (string username) {
if (string username != string.Empty)
{
Console.WriteLine("Your username is " + username);
}
return View();
}
}
Seems like the data is not being passed from the post method. When I hit submit the URL that it requests is Home/Home/Index, which is not were the controller(HomeController) action is located, it should be Home/Index, and use the HomeController right?
What if I need to pass this data to a different controller that has an Index for the action, like UserController?
In this instance, you're missing a slash!
<form action="/Home/Index" method="post">
<input type="text" name="username" />
<input type="submit" value="submit" />
</form>
But when using asp.net mvc, you should use Url.Content with the home directory character to ensure that if your site is deployed in a sub-directory of the site, the correct root will be found. So use:
<form action="#Url.Content("~/Home/Index")" method="post">
<input type="text" name="username" />
<input type="submit" value="submit" />
</form>

Passing Text into URL from a Form

I'm trying to insert a variable collected from a form into a URL, but I don't want the "?variable=value" part of the URL.
<form action="http://www.example.com/<?php echo htmlspecialchars($_GET['entry']);?>/" method="GET">
<input type="text" value="" name="entry" id="entry">
<input type='submit'>
</form>
Is there any easy way to do this? I want the browser to go to the following URL when the user types "whatever"
http://www.example.com/whatever/
Edit:
I've changed the code to the following, which seems to work, but have I now introduced a script vulnerability?
<form onSubmit=" location.href = 'https://www.example.com/' + document.getElementById('entry').value + '/' ; return false; ">
<input type="text" value="" name="entry" id="entry" placeholder="Your Promo Code">
<input name="promoSubmit" type="submit" value="Buy Now">
</form>
you could use javascript for this kind of tasks, i don't see why would you involve server side for such thing
but the easiest answer will be like:
<script>
function go(){
window.location='http://www.example.com/'+document.getElementById('url').value;
}
</script>
<input type='text' id='url'>
<button id='btn_go' onclick='javascript:go();'>Go</button>

Disable submit button when form invalid with AngularJS

I have my form like this:
<form name="myForm">
<input name="myText" type="text" ng-model="mytext" required />
<button disabled="{{ myForm.$invalid }}">Save</button>
</form>
As you may see, the button is disabled if the input is empty but it doesn't change back to enabled when it contains text. How can I make it work?
You need to use the name of your form, as well as ng-disabled: Here's a demo on Plunker
<form name="myForm">
<input name="myText" type="text" ng-model="mytext" required />
<button ng-disabled="myForm.$invalid">Save</button>
</form>
To add to this answer. I just found out that it will also break down if you use a hyphen in your form name (Angular 1.3):
So this will not work:
<form name="my-form">
<input name="myText" type="text" ng-model="mytext" required />
<button ng-disabled="my-form.$invalid">Save</button>
</form>
Selected response is correct, but someone like me, may have issues with async validation with sending request to the server-side - button will be not disabled during given request processing, so button will blink, which looks pretty strange for the users.
To void this, you just need to handle $pending state of the form:
<form name="myForm">
<input name="myText" type="text" ng-model="mytext" required />
<button ng-disabled="myForm.$invalid || myForm.$pending">Save</button>
</form>
If you are using Reactive Forms you can use this:
<button [disabled]="!contactForm.valid" type="submit" class="btn btn-lg btn primary" (click)="printSomething()">Submit</button>
We can create a simple directive and disable the button until all the mandatory fields are filled.
angular.module('sampleapp').directive('disableBtn',
function() {
return {
restrict : 'A',
link : function(scope, element, attrs) {
var $el = $(element);
var submitBtn = $el.find('button[type="submit"]');
var _name = attrs.name;
scope.$watch(_name + '.$valid', function(val) {
if (val) {
submitBtn.removeAttr('disabled');
} else {
submitBtn.attr('disabled', 'disabled');
}
});
}
};
}
);
For More Info click here
<form name="myForm">
<input name="myText" type="text" ng-model="mytext" required/>
<button ng-disabled="myForm.$pristine|| myForm.$invalid">Save</button>
</form>
If you want to be a bit more strict