Why do I continue to get a error for prevent default - forms

I continue to get an error message for the prevent default. This is my form
<form id="myform" >
<input type="text" id="name">
<input type="submit" value="Submit">
</form>
This my code
document.getElementById("myform").addEventListener("submit", function(e){
e.preventDefault();
alert('Data is collected')
});
and this is the message I keep receiving
Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
Please help!
I've tried to change 'sumbit' to 'click'. I tried to make myform into a variable and adding the event listener to the variable. Nothing changed.

Related

Validating form with unpoly

I' trying to validate a form with unpoly. Here my HTML:
<div>
<label for="salesrepresentative-name">Name: </label><br/>
<input id="salesrepresentative-name" name="name" up-validate=".name-errors"/>
<span class="name-errors"></span>
</div>
And the error message what i get is:
Could not find failure target in response (tried [".name-errors", "body"])
Any ideas, what i'm doing wrong?
up-validate will submit the form and replace your current <span class="name-errors"> with the same span from the response.
The error message you received means that the form submission did not include a <span class="name-errors">. If even did not include a <body> tag (which Unpoly uses as a fallback when selectors are missing).
Try to submit the form without Unpoly and see if the response is what you're expecting.

Testing form input text value

I am testing the following form in my html :
<form wicket:id="form">
<input type="text" wicket:id="input"/>
<input type="submit"/>
</form>
The default value is null and in my test I tried to change it to something and check if I have really changed the field as following:
formTester.setValue(formTester.getForm().get("input"), "randomText");
Assert.assertEquals("randomText", formTester.getTextComponentValue("input"));
However the test is not successful because the field expected <[randomText]> but was: [].Any ideas why the setting doesn't work?
You need to call FormTester#submit() after setting the values of the FormComponents.

Input field with validation crashes when typing fast in Angular 2

I have a form in Angular 2 with a simple validation using Validators.
The html template
<form role="form" #myForm="ngForm" (ngSubmit)="submit()" novalidate [ngFormModel]="form">
<div class="form-group list-element">
<label for="name">name*</label>
<input type="text" name="name" class="form-control" ngControl="name"
#name="ngForm" placeholder="Enter name" [(ngModel)]=user.name >
</div>
</form>
The ControlGroup for the validation
ngOnInit() {
this.form = new ControlGroup({
name: new Control('', Validators.compose([
Validators.required,
Validators.pattern("(([a-zA-Z ]|[0-9])+)*"),
Validators.minLength(5),
Validators.maxLength(80)
]))
});
}
When I type fast (random) characters in the input field (e.g #1KZBZKBjkndedjk###kjbzdzdékj!) the application crashes. This does not happen always so I have not really noticed a pattern.
This is the place where the crash happens, so I think the crash has something to do with the pattern.
That's a known issue https://github.com/angular/angular/issues/7822 and will probably be fixed when this pull request lands https://github.com/angular/angular/pull/7421
First of all this is not related to AngularJS.
This happens because your pattern is wrong I don't know what pattern you need but for example if you need to validate email then use this pattern
& Just Check for your pattern nothing else.
your code....
Validators.pattern("^[_a-z0-9]+(.[_a-z0-9]+)#[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,4})$"),
your code ends...
That it!

Why is the CSRF Token value empty/blank in a form when a new session is created?

I am using Laravel 4 ("laravel/framework": "4.0.*").
I have my login form that uses the Form::open() helper method. The input tag for "_token" gets automatically added to the form as expected, except when a new session is created (let's say I clear my cookies), the "value" attribute is missing and there is no token value in the rendered output (View Source).
<input name="_token" type="hidden">
Only on refreshing the page, the _token value is displayed...
<input name="_token" type="hidden" value="FWFOGHSz6oUgYHFblHZ1HrPOZHQC1koF8S52sOL9">
If I however manually enter the following in the .blade file...
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
... I get the following in the rendered output..
<input name="_token" type="hidden"> <input type="hidden" name="_token" value="FWFOGHSz6oUgYHFblHZ1HrPOZHQC1koF8S52sOL9">
Any clues on why is the Form::open() method auto-magic stuff not working as expected?
EDIT
My Form::open() code in the .blade file is as follows:
{{
Form::open(array(
'url' => URL::full(),
'class' => 'form-vertical login-form'
))
}}

if isset doesn't work in php

I'm having a form with only one submit button. I don't know why, but when I use this code and I click on the submit button, nothing is happening. If I use a ! before the isset you'll see the echo in the page. I don't know what's wrong with it.
<form>
<input type="submit" value="Toevoegen" name="addImg" />
</form>
<?
if (isset($_POST['addImg'])) {echo "haaallloooo";}
?>
Maybe, form by default is sending variables by get, try using method="POST" attribute in form tag
You have to set the method to POST.
Otherwise you can use:
$_REQUEST['addImg']
The variable $_REQUEST can access both GET and POST parameters.
Form needs an action and a method.
<form action="" method="post">
<input type="submit" value="Toevoegen" name="addImg" />
</form>
<?
if (isset($_POST['addImg'])) {echo "haaallloooo";}
?>
Regarding "isset", if $_POST['addImg'] is not set, it doesn't echo "haaallloooo".
isset — Determine if a variable is set and is not NULL
Check http://hk.php.net/manual/en/function.isset.php