What is the difference between null and "" - html

I am currently trying to understand the jsp codes of another guy but I am having trouble with the following codes:
if( request.getParameter("username")!=null &&
!request.getParameter("username").equals(""))
The username is the field a user fills on an HTML form. The codes after these codes saves the data the user fills in into strings which will be later used for other purposes.
My question is what is the purpose of the !request.getParameter("username").equals("") code part in the above?
Has request.getParameter("username")!=null part of the code not already tested whether the user enters information into the input field of the HTML form or not?
Regards

If you reveive a request like
http://.../yourservlet
then the parameter value will be null, since no parameter named username is passed at all.
If you reveive a request like
http://.../yourservlet?username=
then the parameter value will be the empty string, since the parameter is passed, but its value is the empty string.
What you'll receive depends on the HTML, and on what the user actually does (he could type the URL manually in the address bar, for example). The code makes sure both cases are handled in the same way. Let's imagine some JavaScript in the page disabled the input field or removes it from the DOM when some checkbox is checked. In that case, the parameter won't be sent at all.

Has request.getParameter("username")!=null part of the code not already tested whether the user enters information into the input field of the HTML form or not?
No. A text input in a HTML form will always send a value. If nothing has been typed into it, then that value will be "".

Related

How to validate input in a field on webpage without reloading the webpage?

(I am a beginner in django and html)
How can I weed out spam entries in a particular field while taking input from a user?
Also, how to show a message to the user that the entered value is not accepted because it's a spam, without reloading the webpage.
For eg., in the field shown in image, if the user enters a number or something like test123 or something else that's not a proper name, my webpage should show a message to the user without loading the webpage that the entered value is not valid. I am also okay with putting a button that says validate to validate this name field value. How can I achieve this using Django+html+jquery?
Easiest way is to use the pattern attribute:
<input type="text" pattern="[A-zÀ-ž][A-zÀ-ž\s]+">
The browser will automatically check when you submit the form.
A-z stands for: latin characters.
À-ž stands for diacritics.
\s stands for space.
Note that the above patters requires the name to start with a letter and have at least 2 characters to be valid.
In Django you can define your form field like so:
full_name = forms.CharField(label='Full name', widget=forms.TextInput(attrs={"pattern":"[A-zÀ-ž][A-zÀ-ž\s]+"}))

handling the request parameters from id elements in server side

In the server side we fetch the form parameters by the form element name. Similarly how to fetch the form parameters in case the input element does not have the name attribute and has the id attribute?
I don't think that is possible. Unless an input element has name attribute it won't even be submitted in the GET/POST request while submitting the form.
So, my understanding is, to read value for any input field in html (on server side) we need to have name attribute defined on it.
Going through w3c specs at HTML specification, it says that the first step that happens during form submissions is Step one: Identify the successful controls. And successful control is the one that has Control name defined on that field. i.e, the name attribute .

Why cant I add a URL as a prefilled Wufoo field entry?

I am trying to write some code that will put the current tabs URL into a wufoo form as a default value in the form hash. The code works fine but the wufoo form doesn't want to cooperate. instead of filling the correct URL (Say https://wufoo.com for example) it drops one of the / characters. This also happens if you skip the code and simply type the URL hash as in the example below:
https://ownthistown.wufoo.com/forms/q1d68ngv1qovy1o/def/&field1=http://wufoo.com
The field would show https:/wufoo.com as the entry. ANyone have any ideas why this is happening?

Hide the text input field portion in a form's GET query url when the value is an empty string

e.g.: http://127.0.0.1:8000/database/?reference_doi=&submit=Submit
I know It appears to be an html standard, but is there a tag to switch it, so that the empty text input string does not appear in the query url?
Or alternatively, since I'm using Django, I tried doing the following in my view.
request_get_copy = request.GET.copy()
for key, value in request_get_copy.items():
if not value or key == 'submit':
request_get_copy.pop(key)
request.GET = request_get_copy
request.META['QUERY_STRING'] = request_get_copy.urlencode()
I displayed request.GET and request.META['QUERY_STRING'] in the actual page through my template, and several methods that request object has, and they all gave successfully "corrected" values, like http://127.0.0.1:8000/database/ But since the GET request first goes through the browser, the displayed url still contains empty string value portions. Is there anything I can do?
The easiest thing you could do is to issue a redirect to your fixed URL:
fixed_url = request_get_copy.urlencode()
return redirect(fixed_url)
Even better if you do that only if it actually changed, and before any DB access or heavy work.
This means an additional GET, but gives you the result you want, and I guess that's more valuable to you :)
If Javascript is an option, you could also do this changes before the submit actually happens, it's a tad more convoluted but will avoid the extra request.
Edit: Just to be clear, there's no way to "turn it off", you could say this is how HTTP and browsers work :)

My fields complete itselves

good afternoon,
i'm, at the moment, programming a web site with Symfony2.
I use a form and into this form there are some input type="text"
and two input type="password".
I'm encountering a problem using this form, indeed the password and the neighboor input complete itselves, by using my own name and my password.
The thing is that those inputs shouldn't be allowed to do this, a thing even stranger is that the input in which my name is put is not even a login input, it is called "inpCity".
I'm not sure if what i'm saying is clear for you, but this is really embarassing. I don't want it to autocomplete, i put the autocomplete attribute to off, but it just hides the value of the field.
Any idea about this ?
This is the first time it happens, while i used to create 3 others website (without Symfony)
PS : sorry for my english, this is not my main language
I think it's built-in feature of browser but suggestions are based on the name of input field.
How did you override getName() of your form type? That "name" is prefixed to you field names, for example:
getName() returns "my_form"
input name is email
your field will be named my_form_email
So, inspect you HTML and find how input name was generated...