html form input - multiple answers for each input field - html

I'm not sure exactly how to word this question, but I'll do my best.
I have a form which collects data about employment history from users. At the moment my form looks like this:
<form name="EmploymentHistory" action="Form E.php" method="post">
<h2>Employment History</h2>
<label>Last/Current employer</label>
<input type='text' name='LastCurrentemployer'>
<hr> <label>Position</label>
<input type='text' name='Position'>
<hr> <label>Date Started</label>
<input type='text' name='DateStarted'>
<hr> <label>Date Finished</label>
<input type='text' name ='DateFinished'>
<hr> <label>Supervised by</label>
<input type='text' name = 'Supervisedby'>
<hr> <label>Contact Details for Boss</label>
<input type='text' name='ContactDetailsForBoss'>
<hr> <button type='button'>Add another job</button> <br>
<hr> <input type="submit" value="Next">
</form>
My problem is that the "Add another job" button does nothing.
How can I enable a user to enter multiple jobs into their employment history so that each job is entered as a new record in my database, but the submit button (next) will only need to be hit once?
I was thinking about trying to set an array to then using arraypush to add each entry, eg:
$pastemployers = array ();
arraypush $pastemployers(POST_['LastCurrentemployer']);
then a loop to add each one as a new record, eg:
for ($x = 0; $x < count(pastemployers); $x++){
echo "<input name='pastemployers" . $x . "' value='" . $pastemployers[$x] . "'> </input>";
I'm sure there's a better way to do this, but I can't find the answer online or work it out. Most of my searches are returning results about checkboxes or multiple submit buttons (not helpful to me). Please help

First of all alter your form as below and use addAnotherJob() to create clone of first span (using jQuery) and change id of each form field.On submitting this form you will get array of employer details.
<form name="EmploymentHistory" action="Form.php" method="post">
<h2>Employment History</h2>
<span class="span_clone">
<label>Last/Current employer</label>
<input type='text' id='LastCurrentemployer_0' name='LastCurrentemployer[]'>
<hr> <label>Position</label>
<input type='text' id='Position_0' name='Position[]'>
<hr> <label>Date Started</label>
<input type='text' id="DateStarted_0" name='DateStarted[]'>
<hr> <label>Date Finished</label>
<input type='text' id='DateFinished_0' name ='DateFinished[]'>
<hr> <label>Supervised by</label>
<input type='text' id='Supervisedby_0' name = 'Supervisedby[]'>
<hr> <label>Contact Details for Boss</label>
<input type='text' id='ContactDetailsForBoss_0' name='ContactDetailsForBoss[]'>
<hr> <button id='add-another-job-0' type='button' onclick= 'addAnotherJob()'>Add another job</button> <br>
</span>
<hr> <input type="submit" value="Next">
</form>
Please refer this link : http://jsfiddle.net/ambiguous/LAECx/

Related

Linking custom HTML Form to Google Form (How to handle Date input?)

I am trying to link a custom HTMl form to an existing Google Form. I have matched the action and name attributes to the form (placeholders in html snippet below for privacy).
My custom Form
<form action="https://docs.google.com/forms/PLACEHOLDERurl" method="post">
<div class="form-group">
<label class="mb-0" for="name">First & Last Name</label>
<input class="form-control" name="entry.PLACEHOLDER" type="text" id="name" placeholder="Enter name" required>
</div>
<div class="form-group">
<label for="date" class="mb-0">Pick Up Date</label>
<input id="pick-up-date" type="date" class="form-control">
</div>
<button id="testFormSubmit" type="submit" value="submit" class="btn btn-main-color">Submit</button>
</form>
However, there are 3 "name" attributes for the date input on the google form.
Main date input from Google Form
<input type="date" class="quantumWizTextinputPaperinputInput exportInput" jsname="YPqjbf" autocomplete="off" tabindex="0" value="" data-initial-value="2020-04-14" badinput="false" dir="ltr">
3 hidden inputs for each name value (year, day, month) from Google Form
<input type="hidden" name="entry.2099319546_year" jsname="L9xHkb_year" value="2020">
<input type="hidden" name="entry.2099319546_month" jsname="L9xHkb_month" value="4">
<input type="hidden" name="entry.2099319546_day" jsname="L9xHkb_day" value="14">
Can someone please let me know how I can alter my custom date form input to match this format?
I was dealing with same problem but turns out the solution is pretty easy.
You just need to delete the underscore value ('_year') and use only the entry id.
entry.2099319546
Doesn't matter that they have three different inputs and you only have one, Google will recognize it no matter way.
Check this tutorial from Codepen

Why does required attribute in html doesn't work?

I wanted to try and verify input before being submitted therefore I used the required attribute at the end of input, but it's not working and I've already tried some of the recommended solution like wrapping the input in form tag or trying to close the tag of input () but when i submit my form with an empty input it stills submited normally and doesn't declare a required field .
I would appreciate any help, thank you!!
this is a part of my code
<form id="form" style="background-color:honeydew ;" class="container text-center">
<div><br><h2> Contact Us </h2></div>
<div id="contact">
<div>
<label> Name</label>
<input type="text" placeholder="Your name " name="name" required/>
</div>
<br>
<div>
<label> Email</label>
<input type="email" placeholder="name#gmail.com" name="email" name="email">
</div>
<br>
<div>
<label> Message</label>
<input type="text" style="height:50px;" name="message">
</div>
<br>
<div><input type="button" value="submit" name="submit"><br></div>
<br>
</div>
<br>
</form>
and this is the javascript file linked to it :
//we take informations subbmitted by user from the form and we replace the form with a reply
//containing these pieces of information on the click on the submit button
var form=document.getElementById('form'),
contactForm=document.getElementById('contact'),
submitButton=contactForm.children[6].children[0];
var processForm= function(){
name=document.getElementsByName('name')[0].value,
email=document.getElementsByName('email')[0].value,
sitereplyText=document.createTextNode('this is a initialiazing value'),
sitereplyEl=document.createElement('p');
mytext= 'Hey '+name+'! Thanks for your message :) We will email you back at '+email;
sitereplyText.nodeValue=mytext;
sitereplyEl.appendChild(sitereplyText);
form.replaceChild(sitereplyEl,contactForm);
}
submitButton.addEventListener('click',processForm);
So i firstly corrected the input type into submit
<input type="submit" value="submit" name="submit">
then in the javascript file i changed
submitButton.addEventListener('click',processForm);
to
submitButton.addEventListener('form.submit()',processForm);
and it seems to work :)

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.

how put put dynamically populated number into input value field

I have dynamically populated value coming from an external js file. The value is working just fine when I call it in the HTML page but I'm stuck on how to have an input field display this value.
Here is my HTML:
<form action="../mail.php" method="POST" class="location-form">
<div class="device-details">
<h2>Device: iPhone5</h2>
<h2>Repair Cost: <span id="result">0</span></h2>
</div>
<input name="device" type="hidden" id="device" value="iPhone5" maxlength="20">
<input name="cost" type="hidden" id="cost" value="" maxlength="20">
<div class="submit-btn">
<input type="submit" value="Submit Request" />
</div>
</form>
<script>
document.getElementById("result").innerHTML = localStorage.getItem("sum");
</script>
at the top, in the div "device-details", the h2 "repair cost" is populating the value just fine, no issues. But I cannot seem to populate the last input with this value.
<input name="cost" type="hidden" id="cost" value="(need the value here)" maxlength="20">
I need the value of the input field to mimic the value of the repair cost at the top in order to send that value to an email address.
I've tried inserting the following into the value and it didn't work
value="<span id='result'>0</span>"
I've tried creating a variable in php and then putting the variable into the value field, and it did not work
<?php
$my_var = '<span id="result">0</span>';
?>
value="<?php echo '$my_var'); ?>"
I've also tried:
value="<?php echo htmlspecialchars($my_var); ?>"
these last two just returned the actual HTML code
<span id="result"></span>
instead of the populated value.
Try this one.
<!DOCTYPE html>
<html>
<body onload="abc()">
<form action="../mail.php" method="POST" class="location-form">
<div class="device-details">
<h2>Device: iPhone5</h2>
<h2>Repair Cost: <span id="result">0</span></h2>
</div>
<input name="device" type="hidden" id="device" value="iPhone5" maxlength="20">
<input name="cost" type="hidden" id="cost" value="" maxlength="20">
<div class="submit-btn">
<input type="submit" value="Submit Request" />
</div>
</form>
<script>
function abc(){
document.getElementById("result").innerHTML=localStorage.getItem("sum");
document.getElementById("cost").value = localStorage.getItem("sum");
}
</script>
</body>
</html>

wrong (Please fill out this field ) message in html

I have several separated forms in my HTML page and each form has a several required inputs
when I fill all inpus in the first form and I want to submit the first form the page alarm me Please fill out this field in second forms inpust, although I just want to submit the first form ,and I don't want to fill the second form !
this is a simple example :
if you want to just fill the first form and you want to submit send1 , page alarm you you must fill the second form inputs too !
<form action='' method='POST'>
title1 : <input name='title1' type='text' required > <br>
content1 : <input name='content1' type='text' required ><br>
<input name='send1' type='submit' value='send1' >
<form>
<br>
<form action='' method='POST'>
title2: <input name='title2' type='text' required > <br>
content2 : <input name='content2 ' type='text' required ><br>
<input name='send2' type='submit' value='send2' >
<form>
this is fiddle
for solving this issue the first idea was that I use jQuery but I think there are some other simple ways
jquery :
$("otherinputs").prop('required', false);
I dont want to use jquery
close your form tags <form></form> to solve the problems:
<form action='' method='POST'>
title1 : <input name='title1' type='text' required > <br>
content1 : <input name='content1' type='text' required ><br>
<input name='send1' type='submit' value='send1' >
</form>
<br>
<form action='' method='POST'>
title2: <input name='title2' type='text' required > <br>
content2 : <input name='content2 ' type='text' required ><br>
<input name='send2' type='submit' value='send2' >
</form>
JSFIDDLE