Can you use <input> without a server? - html

I want to use ‹input› without PHP because my code editor (Glitch.com) doesn't work well with PHP. Is there a way to use ‹input› without server-side code? Example: Somehow stores the input as a variable. This is incorrect, I know but can I do something like this?
<form>
<label for="FirstName">First name:</label>
var input = <input type="text" id="fname" name="fname">
</form>
Then like use a getElementById and replace the text with the input variable.
I know I can use prompt(), but I don't want a window popping up, but something like this:
<form>
<label for="Username">Enter New Name:</label>
<input type="text" id="fname" name="fname"><br><br>
<input type="Submit" value="Submit">
</form>
If you can help,
Thanks!
Update:
What I am trying to accomplish is I am making a scorekeeper, and I want the user to be able to rename themselves. Sorry if I wasn't clear.

You can definetely achieve this using Javascript:
const firstName = document.querySelector('#fname');
const lastName = document.querySelector('#lname');
const submit_button = document.querySelector('input[type="button"]');
const heading = document.querySelector('h1')
submit_button.addEventListener('click', () => {
heading.textContent = "Last Name: " + lastName.value;
})
<h1 class="display-first-name"></h1>
<form>
<label for="FirstName">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="LastName">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="button" value="Submit">
</form>

Related

html form input method get prevent name in url if value is not filled

Let's say this is my html in reactjs/jsx:
<form action="/search-list" method="GET">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
</form>
and when i click on the submit buttoin without inputing anything, it make the url like this fname=&lname=
and when i fill anly fname and submit, the url become like this fname=JhonDoe&lname=
Notice here, i didn't fill the lname, yet the lname is here in url which i dont want.
I want, if a field has value, that field should be in url as query params, if not field, that field should not be in url query params.
is it possible to do? how can I do it? All the fields should be optional.
You can use this plain JavaScript to remove the empty inputs from submitting.
<form method="GET" id="myForm">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
</form>
<script>
Form = document.getElementById("myForm");
Form.addEventListener('submit',()=>{
if(Form.fname.value == ""){
Form.fname.name ="";
}
if(Form.lname.value == ""){
Form.lname.name ="";
}
Form.submit();
});
</script>

Input pattern - Mobile number - Start with a specific numbers

I trying to configure the pattern to be able to submit a mobile number that should start with 09 followed by a '0-9' digits. Total max characters are 11.
Sample: 09493432199 or 09267778595
<form action="/action_page.php">
<label for="username">Username:</label>
<input type="text" id="username" name="username" pattern="[09]+[0-9]" maxlength="11"><br><br>
<input type="submit" value="Submit">
</form>
Your html seems nice
<form action="/action_page.php">
<label for="username">Username:</label>
<input type="text" id="username" name="username" pattern="[0-9]" maxlength="11"><br><br>
<input type="submit" value="Submit">
</form>
You need also have JS like follow :
var username_prefix = $('#username').subsubstr(0,2);
$('#validate').click(function(){
if (username_prefix == 09) {
alert('correct');
}
else {
alert('incorrect');
}
});
Use an expression with fixed digits plus a specific amount of digits.
If you need the exact 11 characters, then use 09[0-9]{9}
In this example 09[0-9]{8,9} are valid 0912345678 and 09123456789
<form action="/action_page.php">
<label for="username">Username:</label>
<input type="text" id="username" name="username" pattern="09[0-9]{8,9}" maxlength="11"><br><br>
<input type="submit" value="Submit">
</form>
As a side note, even if using input pattern is supported by the most common browsers, you should not rely only on the client side validation. Use a similar validation from the PHP backend to verify the information (in this case additional ^ (start of expression) and $ (end of expression) were added.
if (!preg_match("/^09[0-9]{9}$/",$_GET["username"])){
// not valid
}
You can use this RegExp
It will match
when the number starts with 09 AND
when the number has exactly 11 digits
pattern = /09[0-9]{9}/
Try it out here
https://regex101.com/r/Nl2c2y/3
<form action="/action_page.php">
<label for="username">Username:</label>
<input type="text" id="username" name="username" pattern="09[0-9]{9}" maxlength="11"><br><br>
<input type="submit" value="Submit">
</form>
<form action="/action_page.php">
<label for="username">Username:</label>
<input type="text" id="username" name="username" pattern="[0-9]" title="Zero is required" maxlength="11"><br><br>
<input type="submit" value="Submit">
</form>

How can I add text to a entered input value

How can I add a string to the text written by the user of my form?
For example, if he wants to search for "test", my form should submit "site:mysite.de test".
(The name where I'm trying to send the string to is q.)
I tried with
<form action="https://searx.me" method="get" accept-charset="UTF-8">
<input type="text" name="q" placeholder="Search with Searxes" required>
<input type="hidden" name="q" value="site:mysite.de">
</form>
How expected only the first value is submitted https://searx.me/?q=test
I would prefer a solution with pure html without javascript.
Here is an ugly way to do that, looks like binding ;)
var hidden = document.getElementsByClassName('hidden')[0];
var hidden_attr = hidden.getAttribute('value');
var show = document.getElementsByClassName('show')[0];
function magic(){
hidden.setAttribute('value', hidden_attr + show.value);
console.log(hidden.getAttribute('value'));
}
<form action="https://searx.me" method="get" accept-charset="UTF-8">
<input class="show" onkeyup="magic()" type="text" name="q" placeholder="Search with Searxes" required>
<input class="hidden" type="hidden" name="q" value="site:mysite.de ">
</form>

HTML label control doesn't pass form data to controller

This format of html form control doesn't pass form data
<form action="index.php?action=createNewCustomer"
method="POST">
<fieldset>
<label class="field" for="firstName">First Name:</label>
<input type="text">
</fieldset>
<input type="submit">
</form>
but this one does
<form action="index.php?action=createNewCustomer"
method="POST">
<fieldset>
<label>First Name:</label>
<input type="text" name="firstName">
</fieldset>
<input type="submit">
</form>
Unless I'm missing something obvious, the difference is in the label control..Are both valid and if so, can anyone explain to me why only the second one passes form data to my PHP controller method.
<input type ="submit"> is not part of the fieldset in the first bit of code, but I don't think that's really the issue.
I believe changing the code on the first bit to:
<form action="index.php?action=createNewCustomer"
method="POST">
<fieldset>
<label for = "firstName">First Name: </label>
<input type="text" name = "firstName">
<input type="submit">
</fieldset>
</form>
will work. You need to make sure the name of the input is the same as the property you are trying to set.

Formatting output from mailto:

I'm making a form which collect some data from a person and then use a simple mailto: to put it in their e-mail client like so (example):
<form method="post" action="mailto:email#address.com?subject=Stuff">
<p>Name: <input type="text" name="Name" placeholder="Number"></p>
<p>Address: <input type="text" name="Address" placeholder="Number"></p>
<p>City: <input type="text" name="City" placeholder="Number"></p>
<p>Comment: <input type="text" name="Comment" placeholder="Number"></p>
<p><input type="submit" value="SEND"></p>
</form>
It then comes out like this:
Name=Bob&Address=something&City=more&Comment=elsemore
but what I want is:
Name=Bob
Address=something
City=more
Comment=elsemore
or even better:
Name = Bob
Address = something
City = more
Comment = elsemore
Is it at all possible currently to do this just within HTML? HTML5 and Python is all I know (mostly only the basic parts too). Maybe someone could help me out with the proper code/script on this.
I did find some other posts on this but they are old and don't answer the question.
You need to set the Encryption Type (enctype="text/plain"):
<form method="post" action="mailto:email#address.com?subject=Stuff" enctype="text/plain">
<p>Name: <input type="text" name="Name" placeholder="Number"></p>
<p>Address: <input type="text" name="Address" placeholder="Number"></p>
<p>City: <input type="text" name="City" placeholder="Number"></p>
<p>Comment: <input type="text" name="Comment" placeholder="Number"></p>
<p><input type="submit" value="SEND"></p>
</form>
You could also write some stuff in Javascript and then work with the body parameter. But i wouldn't recommend this.
In most times, it is better to let the webserver handle the email communication.
Asp: http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp
Php: http://tangledindesign.com/how-to-create-a-contact-form-using-html5-css3-and-php/