Local Storage not saving - html

I have a form and I am trying to save the fields to local storage but the alert comes up as null And the local storage isn't saving
<form method="post">
<input type="text" name="name" id="name" class="stored form-control" value="" />
<button onclick="store()" type="button" id="myButton" class="btn />
</form>
function store(){
var inputName= document.getElementById("name");
window.localStorage.setItem("name", inputName.value);
alert(name);
}

You have to call window.localStorage.getItem("name") to get the name from local storage in alert. Also your button tag is not closed properly. I have just formatted your code:
<!DOCTYPE html>
<html>
<head>
<title>Local Storage Demo</title>
<script>
function store(){
var inputName= document.getElementById("name");
window.localStorage.setItem("name", inputName.value);
alert(window.localStorage.getItem("name"));
}
</script>
</head>
<body>
<form>
<input type="text" name="name" id="name" />
<button onclick="store()" type="button" id="myButton">Save</button>
</form>
</body>
</html>

Related

Page not redirected when button is pressed

<html>
<head>
</head>
<body>
<script>
function isVisible(){
if(document.getElementById("nt").checked==true){
document.getElementById("opt").style.visibility="hidden";
}
else{
document.getElementById("opt").style.visibility="visible";
}
}
</script>
<form align="center" method="post">
<input type="radio" name="teaching" id="t" value="teaching" onchange="isVisible()"> Teaching<br>
<input type="radio" name="teaching" id="nt" value="non-teaching" onchange="isVisible()"> Non-teaching<br>
Post Code <input type="text" name="pcode" id="pcode"><br>
Post Name <input type="text" name="pname" id="pname"><br>
<div id="opt">
Department <input type="text" name="dept" id="dept">
</div>
<input type="button" name="addv" id="addv" value="Add Vacancy" onclick="javascript: form.action='hello.php';">
</form>
</body>
</html>
Above is addvacancy.php. On clicking button Add Vacancy, it is not directing to hello.php. It remains on the same page with values in the text boxes retained. Below is the code for hello.php.
hello.php
<html>
<head>
</head>
<body>
<?php
echo "Hello!";
?>
</body>
</html>
You need to add an action to your form, and you should remove the onclick event from your input so it looks like this:
<input type="submit" name="addv" id="addv" value="Add Vacancy">
And add an action to your form that points to the desired URL in which your form submission will be directed to, so it looks like this:
<form action="hello.php" align="center" method="post">
That should do the trick. The action attribute tells the browser to send the form data to a form-handling PHP page.

Input validation for text boxes in HTML5

I have a form like this,
<form>
Username: <input type="text" name="usrname" required>
<input type="button" value="Post">
</form>
I want a tooltip to pop-up if the user clicks on the button without entering anything in the textbox.
I know it will work if I change the button to like this - <input type="submit" value="Post">. But I don't want to do that.
Did you mean formaction instead of action?
<input type="text" name="usrname" required formaction="action_page.php">
action is a form tag attribute, not inputs's.
Use HTML5
Try like this it will work :
action attribute specifically for form tag. not input tag
<form action="action_page.php">
Username: <input type="text" name="usrname" required >
<input type="submit" value="Post">
</form>
Use method "POST" in tag, It will work
<form action="action_page.php" method="POST">
Username: <input type="text" name="usrname" required >
<input type="submit" value="Post">
</form>
I figured out how to do this with some jQuery based on an answer from this post.
The following code does exactly what I was looking for,
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<form id="myForm">
Username: <input type="text" name="usrname" required id="input">
<input type="button" value="Post" id="button">
</form>
<script>
$('#button').click(function(e) {
if ($('#input').val() === "") {
$('<input type="submit">').hide().appendTo('#myForm').click().remove();
}
});
</script>
</body>
</html>
See here for DEMO

create a link using input and button

I want to create a form like popup that has a input and a button. When i type in an input Ex.: "good" and click on button it go to the url "good.mydomain.com"
i did this but it doest work
<html>
<form action="firstname.mydomain.com">
Go to URL:<br>
<input type="text" name="firstname" value="">.mydomain.com
<br>
<p>
<input type="submit" value="go">
</p>
</form>
</html>
You could achieve this with jQuery.
The HTML:
<input type="text" id="goTo" name="firstname">.mydomain.com
<input type="submit" id="myButton" value="Go">
Then the jQuery:
$('#myButton').click(function() {
var goTo = $("#goTo").val();
window.location.href = 'http://'+goTo+'.mydomain.com/';
return false;
});
please try below code using javascript.
<html>
<head>
<title>Submit</title>
<script>
function Submit() {
var url = "http://www." + document.getElementById("firstname").value + ".mydomain.com";
window.location.assign(url);
}
</script>
</head>
<body>
Go to URL:<br>
<input type="text" name="firstname" id="firstname" value="">.mydomain.com
<br>
<p>
<input type="button" value="go" onclick="Submit()">
</p>
</body>
</html>​​

HTML Form submit data

I have a form on a remote server I'm trying to submit data to, example below
<html>
<head>
</head>
<form action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
<input type="submit" name="send" value="Submit" />
</form>
</body>
</html>
I'm trying to auto submit the data like this
<html>
<head>
</head>
<form action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
<script type="text/javascript">
document.forms[0].submit();
</script>
</form>
</body>
</html>
It works fine if the first example didn't have a (name="send") name but when it does have a name nothing submits. My question is how would I go about sending the data with a input button that has a name.
Thank you
Note: The answer turned out to be type="hidden" instead of type="submit", in which the second does not allow DOM submission while also submitting that input's value in the GET/POST data. type="hidden" does, and it works here since the button does not need to be physically clicked.
Pick one:
<form name="formname" id="formid" action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
<input type="submit" name="send" value="Submit" />
</form>
<script type="text/javascript">
if (confirm('Ok for "formname", Cancel for "getElementById"')) {
console.log('document.formname.submit()');
document.formname.submit();
} else {
console.log('document.getElementById("formid").submit()');
document.getElementById('formid').submit();
}
</script>
http://jsfiddle.net/userdude/EAmwj/3
Your JavaScript code will get executed as soon as the page is loaded. That is why it gets submitted immediately.
You need to wrap the JS code in a function and let an event call it from the page.
You can use a regular button and set the function for the onclick event of a button.
<html>
<head>
<body>
<form action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
<input type="button" name="send" value="test2" onclick="doSubmit()" />
<script type="text/javascript">
function doSubmit() {
document.forms[0].submit();
}
</script>
</form>
</body>
</html>

html form submit button

I have a form and when the form is loaded through a browser, I want it to submit the data automatically.
The problem is I wrote a PHP script that will submit it all fine on a test form. My problem is the server I'm trying to submit the data to named the button "submit".
Here is the example form:
<html>
<head>
</head>
<form action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
</form>
<script type="text/javascript">
document.forms[0].action="submit"
</script>
</body>
</html>
The person that created the form on the other server named it "submit". Is there a workaround until they fix it?
Here is a example of the person's form
<html>
<head>
</head>
<form action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
<input type="submit" name="submit" class="submit" value="Send Data" />
</form>
</body>
</html>
You want to submit the form? Then simply use its submit() method:
document.forms[0].submit();
If the server expects submit to be POSTed, i.e. the button being clicked, you can simply trigger the click() event of the button. Since you cannot access it using its name, I'd use jQuery for it:
$('input[name="submit"]').click();
Instead of:
<html>
<head>
</head>
<form action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
</form>
<script type="text/javascript">
document.forms[0].action="submit"
</script>
</body>
</html>
Try this:
<html>
<head>
</head>
<form name="MyForm" action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
</form>
<script type="text/javascript">
document.MyForm.submit();
</script>
</body>
</html>
If I understand your question correctly, the input element has name="submit" and type="submit". They are different things.
So you can also create the same behavior easily.