I have a 2-step registration process which consists of 2 different HTML pages.
The second (and final) step collects all the data and sends it to the server for evaluation. For simplicity, let's say I collect user's name in the first form and user's age in the second:
formA.html:
<form action="formb.html" method="get">
Name: <input id="age" type="text" name="age">
<input id="submit_button" type="submit" value="CONTINUE">
</form>
formB.html:
<form action="serverscript.py" method="post">
Age: <input id="name" type="text" name="name">
<input id="submit_button" type="submit" value="SUBMIT">
</form>
How can I "propagate" the "name" that user has entered in formA.html to formB.html so that I can send name,age to the server?
p.s. The only way I can thinkg is doing it with and then parsing the URL in formB , but that seems very ugly...
formA.html:
<form action="formb.html" method="get">
Name: <input id="Name" type="text" name="Name">
<input id="submit_button" type="submit" value="CONTINUE">
</form>
In form B make This Changes...
formB.html:
<form action="serverscript.py" method="post">
Age: <input id="age" type="text" name="age">
<input id="Name" type="hidden" value="">
<input id="submit_button" type="submit" value="SUBMIT">
</form>
<script type="text/javascript">
var query = window.location.search.substring(1);
var Field=query.split("=");
document.getElementById("Name").value = Field[1];
</script>
Hope it will help ;)
I can think of 3 ways :
1) cookie. store the information in a cookie and read it in the second form
2) Query string.You can chain the the data to query string and read it on form B
3) You can use the action attribute of a form which can take a url. (recommended)
You can build the data you get from form A as html hidden input fields within form B on load, since you are sending your data through 'GET' method, then when you submit form B, the data will be sent to the server.
Edit
Assuming am using PHP as server side :
forma.php
<form action="formb.php" method="get">
Name: <input id="name" type="text" name="name">
<input id="submit_button" type="submit" value="CONTINUE">
</form>
formb.php
<form action="postPage.php" method="post">
Age: <input id="age" type="text" name="age">
<?php
if(isset($_GET['name'])
{
?>
<input id="name" type="hidden" name="name" value="<?php echo $_GET['name']?>"/>
<?php } ?>
<input id="submit_button" type="submit" value="SUBMIT">
</form>
Try using HTML5 sessionStorage to store data in browser session. During the session, a user could visit other pages of the domain, or other sites entirely, then return to the original domain. Any data saved in sessionStorage during that session will remain available, but only to pages in the original domain, until the tab or window is closed.
Related
for example
this is our form
<form action="test1" method="GET" name="frm">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit" >
</form>
and here I want to put the content of the values from the form and put them into global variables I've tried this code. But it doesn't display the content of "a"
<script>
var a=document.frm.lname.value;
console.log(a);
</script>
you access the form value and it returns the correct value.
your input fields are empty and your get an empty result.
assign an initial value to your form fields and you will get them.
<form action="test1" method="GET" name="frm">
First name: <input type="text" name="fname" value="sdfsd"><br>
Last name: <input type="text" name="lname" value="sdfsd"><br>
<input type="submit" value="Submit" >
</form>
if you want to get the value your user entered, run your
var a=document.frm.lname.value;
after an event like onsubmit event of your form
Look at the example below which logs the value of last name after a user clicks on submit button
var a=document.frm.lname;
function aFunction(){
console.log(a.value);
}
<form action="#" method="GET" name="frm" onsubmit="aFunction()" >
First name: <input type="text" name="fname" ><br>
Last name: <input type="text" name="lname" ><br>
<input type="submit" value="Submit" >
</form>
I have made a form in my site, which will allow me to get suggestions about Rubik's cube algorithms, but how to know what input the user has? For better understanding, I've given the code below:
<form method="POST">
<label>Your name: </label><br>
<input type="text" name="name" placeholder="Your Name" required><br><br>
<label>Your E-mail: </label><br>
<input type="email" name="email" placeholder="email#domain.com" required><br><br>
<label>Select puzzle: </label><br>
<input type="radio" name="2x2" value="2x2">2x2<br>
<input type="radio" name="3x3" value="3x3">3x3<br><br>
<label>Select set/subset: </label><br>
<input list="set"><br><br>
<datalist id="set">
<option>Ortega OLL</option>
<option>Ortega PBLL</option>
<option>CLL</option>
<option>EG-1</option>
<option>EG-2</option>
<option>F2L</option>
<option>OLL</option>
<option>PLL</option>
<option>COLL</option>
<option>WV</option>
</datalist>
<label>Your Alg: </label><br>
<input type="text" name="alg"><br><br>
<input type="submit" name="submit" class="w3-black w3-button w3-hover-white w3-hover-text-blue w3-text-white">
</form>
Please add action attribute to your form tag and on submit here is the example
<form action="getvalue.php" method="post">
</form>
Note: Every form element must have unique name .
after adding action attribute then create getvalue.php file and add following code in to it
<?php
print_r($_POST);
?>
Above code will give all the form field values
do let me know if it was helpfull...
I'm not sure exactly what you want to do, but here is an example of a form that submits to itself. This will allow you to remain on the same page after the form has been submitted. You can change what the user sees to indicate that the form was done successfully/etc. I have tested this code and it works.
<main>
<?php
// When the form is submitted
if (isset($_POST["submitButton"])){
//Do something with the data from the form - you don't have to print it out.
//This is all done on the server.
//Connect to DATABASE, send an EMAIL, VALIDATE the form, etc.
print("<pre>");
print_r($_POST); // for all GET variables
print("</pre>")
?>
<!-- This HTML will be visible to the user after the form has been submitted. -->
<h1>The form has been submitted successfully!</h1>
<?php
}else{ //If the form has not been submitted
?>
<form method = "post" >
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" id = "submitButton" name = "submitButton" value="Submit">
</form>
<?php
} //End else
?>
</main>
As the user fills out the form, I have some buttons that dynamically add new questions. However, I can't get the value of these buttons to Post to the subsequent PHP page (all the other information posts fine).
In the example below, I'm not able to get the value of "add_email" in "process-form.php" via $_POST['add_email'];
Thanks in advance for your help. In reduced form, it looks like this:
HTML form
<form id="form" class="form" action="process-form.php" method="POST">
<input id="name" name="name" type="text" placeholder="Name">
<!-- Yes/No buttons asking if the user wants to enter their email-->
<div id="form_email">
<h4>Do you want to enter your email?</h4>
<input type="button" class="btn" id="add_email" name="add_email" value="Yes" onclick="showEmailQ(this.value)"></input>
<input type="button" class="btn" id="add_email" name="add_email" value="No" onclick="showEmailQ(this.value)"></input>
</div>
<input type="submit" class="btn" value="Submit ยป">
</form>
Javascript
<script>
//Function to process whether user wants to enter email and then display value
function showEmailQ(value){
var table_row = document.getElementById("form_email");
if(value == "Yes"){
table_row.innerHTML = '<input id="email" name="email" type="text" placeholder="Email">';
}
else{
table_row.innerHTML = '<p>You have chosen not to enter your email</p>';
}
}
</script>
PHP
//process-form.php
session_start();
$enteredEmail = $_POST['add_email'];
echo $enteredEmail; // Nothing prints to screen
You're destroying your form by overwriting the inputs with
table_row.innerHTML = '<input id="email" name="email" type="text" placeholder="Email">';
So maybe change the input name to match
table_row.innerHTML = '<input id="email" name="add_email" type="text" placeholder="Email">';
I have a form with ,
In the form after user clicks on Submit , he will be taken to another page which would show all the data entered, Which would allow the user to verify the content.(User will have a look at the content , to see what he entered is correct).
Once user verifies he would submit the data and Insert to DB should be done.
I want to know a method in which i could carry on the approach, to do this.
How can i implement this
EDIT MORE EXPLAIN
addemp.php
The Main Div With Form
<div class="panel-body">
<form>
Employee : <input Type="text" id="name">
<input type="submit" value="check">
</div>
The Second Div in the same form should show once submit is clicked
<div class="submit panel-body">
<form>
Employee : <Employee Name asin main div>
<input type="submit" > <--! this submit would post data
</form>
</div>
how to pass the value from 1st div to the second , and from the second INSERT to db.how can i do without page refresh ?
Use following script on location file
$action='';
if(isset($_POST['submit'])){
//create form here
//Change the action of form
$action = 'save.php';
}
echo '<form method="POST" action="'.$action.'">
<input type="text" name="nric" value="'.isset($_POST['nric'])?$_POST['nric'].'" />
<input type="text" name="empName" value="'.isset($_POST['empName'])?$_POST['empName'].'" />
<input type="text" name="location" value="'.isset($_POST['location'])?$_POST['location'].'" />
<input type="submit" value="Submit"/>
</form>';
EDIT: You don't need to use a form in this case. You can simply use JQuery to show the data from text boxes in a DIV and a button that will POST the data for you on the server.
<input type="text" name="nric" id="nric" />
<input type="text" name="empName" id="empName" />
<input type="text" name="location" id="location" />
<input id="sndData" type="button" value="Submit" />
<div id="showData"></div>
JQuery:
$('#sndData').click(function(){
var makeData = "<p>NRIC: "+$('#nric').val()+"</p><p>Employee Name: "+$('#empName').val()+"</p><p>Location: "+$('#location').val()+"</p>";
$('#showData').html(makeData);
});
When you've done that, just create/show a HTML button that will POST the data for you.
If this answers your question, please mark it as an answer.
I have a form as follows:
<form name="chat" id="chat" action="" onsubmit="sendMessage()">
<a name="chat"> </a>
<input type="text" name="chat" id="chat" size="38" maxlength="50" vertical-align="middle">
<input type="hidden" name="action" id="action" value="checkresponse">
<input type="hidden" name="response_Array[sessionid]" id="response_Array[sessionid]" value="keu88lh88ini89dgnccob54f27"><br>
<input type="text" name="response_Array[top]" id="response_Array[top]" value="a">
<input type="text" name="response_Array[second]" id="response_Array[second]" value="b"><br>
<input type="text" name="response_Array[third]" id="response_Array[third]" value="c"><br>
<input type="text" name="response_Array[fourth]" id="response_Array[fourth]" value="d"><br>
<input type="text" name="response_Array[input][0]" id="response_Array[input][0]" value="input 0"><br>
<input type="text" name="response_Array[that][0]" id="response_Array[that][0]" value="that 0"><br>
<input type="submit" name="submit" id ="submit" value="SEND" style="width: 45px">
</form>
When the user clicks Submit, I need to send all the input fields to the server.
But, the page should not get refreshed. So, I have created sendMessage(),in which I have used XMLHttpRequest object to send the form submit request.
To read input fields, I have used in sendMessage():
var infoStr = "chat="+document.forms[0]['chat'].value;
infoStr += "&action="+document.forms[0]['action'].value;
I want to know how should I read the 2D array :response_Array and parse it into a JSON string so that it can be passed to the php server code.
It is so simple
var infoStr=""
for(var i=0;i<document.forms[0].elements.length;i++)
{
infoStr+=document.forms[0].elements[i].name+"="+document.forms[0].elements[i].value+"&";
}
alert(infoStr); // You will have the full name value pairs.
Hope this solves your problem.