fill 2 forms one time php - php

I have one form like this
Arrivo:
<select name="arrivo">
<option value="2014-06-15">domenica 15 Giugno</option>
<option value="2014-06-16">lunedì 16 Giugno</option>
<option value="2014-06-17">martedì 17 Giugno</option>
Partenza: <select name="partenza">
<option value="2014-06-15">domenica 15 Giugno</option>
<option value="2014-06-16">lunedì 16 Giugno</option>
<option value="2014-06-17">martedì 17 Giugno</option>
<option value="2014-06-18">mercoledì 18 Giugno</option>
Adulti:
<select name="PER">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<INPUT TYPE = "Submit" VALUE = "Calcola Preventivo">
</form>
I need that some data (like ARRIVO E PARTENZA) will auto-filled in another form (that will be created after a user click on submit), that will be added a new field that user must complete. See this example (where name and email are the new field that user must fill in the second form)
Arrivo:
<input type="text" name="arrivo2" value='$_POST['arrivo']><br>
Partenza
<input type="text" name="partenza2" value='$_POST['partenza']><br>
email
<input type="text" name="email"><br>
Name
<input type="text" name="name"><br>
<INPUT TYPE = "Submit" VALUE = "emailme">

form 1
<form method="post">
<input type="text" name="name" />
<input type="hidden" name="hidname"/>
</form>
after submitting this form (i assume you know process for submitting a form) get the post values in session variable like this
session_start();// this starts a session
$_SESSION['name']=$_POST['name'];
$_SESSION['hidname']=$_POST['hidname'];
from 2
<?php session_start(); ?>
<form method="post">
<input type="text" name="name2" value="<?php if(isset($_SESSION['name'])) echo $_SESSION['name'];?>"/>
<input type="text" name="hidname2" value="<?php if(isset($_SESSION['hidname'])) echo $_SESSION['hidname'];?>" />
</form>

Related

My html form automatically passing all field data in url

I have created a simple html form but I have encountered a weird problem which I have never seen before. When I click on submit then all of the data is directly passing to the URL bar. I don't know what is wrong with this can anyone help?
Form:
<form id="form-data">
Name<br><input type="text" name="name" id="name" value=""><br><br>
Age<br><input type="number" name="number" id="age" value=""><br><br>
Gender<br>
<input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Fe-male<br><br>
<select name="country">
<option value="Kashmir">Kashmir</option>
<option value="Egypt">Egypt</option>
<option value="Norway">Norway</option>
<option value="Iceland">Iceland</option>
</select><br>
<br><input type="submit" id="submit" value="Save">
</form>
the form uses get method therefore it displays the form values in the url. change the method to post to avoid this
method="post"
<!DOCTYPE html>
<html>
<head>
<title></title>
<body>
<form id="form-data" method="post">
Name<br><input type="text" name="name" id="name" value=""><br><br>
Age<br><input type="number" name="number" id="age" value=""><br><br>
Gender<br>
<input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Fe-male<br><br>
<select name="country">
<option value="Kashmir">Kashmir</option>
<option value="Egypt">Egypt</option>
<option value="Norway">Norway</option>
<option value="Iceland">Iceland</option>
</select><br>
<br><input type="submit" id="submit" value="Save">
</form>
</body>
</html>
Add method attribute to your form tag like this:
<form action="welcome.php" method="post">
https://www.w3schools.com/tags/att_form_method.asp
If you don't send the data somewhere, it gets passed as URL parameters to the current page:
From MDN Web Docs:
The action attribute defines where the data gets sent. Its value must
be a valid relative or absolute URL. If this attribute isn't provided,
the data will be sent to the URL of the page containing the form — the
current page.

PHP Get values from Multiple form arrays

I have the following form, how can i get the data from multiple[] arrays in the form and combine them into 3 data sets eg name[], quantity[], unit[]
<input type="text" name="name[]" class="form-control" placeholder="Name" />
<input type="text" name="name[]" class="form-control" placeholder="Name" />
<input type="text" name="name[]" class="form-control" placeholder="Name" />
<input type="text" name="quantity[]" class="form-control" />
<input type="text" name="quantity[]" class="form-control" />
<input type="text" name="quantity[]" class="form-control" />
<select name="unit[]" >
<option value="">--Unit--</option>
<option value="g">g</option>
</select>
<select name="unit[]" >
<option value="">--Unit--</option>
<option value="g">g</option>
</select>
<select name="unit[]" >
<option value="">--Unit--</option>
<option value="g">g</option>
</select>
The PHP Manual at http://php.net/manual/en/tutorial.forms.php answers your question.
Example #1 A simple HTML form
>
>
>
> <form action="action.php" method="post">
> <p>Your name: <input type="text" name="name" /></p>
> <p>Your age: <input type="text" name="age" /></p>
> <p><input type="submit" /></p>
> </form>
>
>
>
There is nothing special about this form. It is a straight HTML form
with no special tags of any kind. When the user fills in this form and
hits the submit button, the action.php page is called. In this file
you would write something like this:
Example #2 Printing data from our form
>
> Hi <?php echo htmlspecialchars($_POST['name']); ?>.
> You are <?php echo (int)$_POST['age']; ?> years old.
>
>
A sample output of this script may be:
Hi Joe. You are 22 years old.
Apart from the htmlspecialchars() and (int) parts, it should be
obvious what this does.
Do var_dump($_POST) to view all results.
in $_POST['name'] (or quantity or unit) u have an array with 3 results.

Make datalist required

I would like the user not to be able to submit a form unless they have selected something from the dropdown datalist which appears as they type. If they just typed something random, I don't want the form to be submitted.
If this isn't possible, would a better option to be to check if the typed text appears in the datalist when the user submits?
<form method="post">
<input type="text" list="browsers">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']
// function to check if something from the datalist was clicked and NOT just typed
}else{
echo'Select something from the datalist!';
}
While I can set the datalist as required, this can easily be by-passed.
Using required in the input tag that has the same id as the datalist you are targeting will check force the user to input something.
<form method="post">
<input type="text" list="browsers" required>
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']
// function to check if something from the datalist was clicked and NOT just typed
}else{
echo'Select something from the datalist!';
}
However it will not block the user from giving an input that is not listed in the dropdown. That needs to be checked via Javascript just before submission.
<form method="post" onsubmit="return myCheckFunction(this)>
<!-- Your form items -->
<!--------------------->
</form>
<script>
myCheckFunction(form) {
// get the values that are currently under the datalist tag in option tags
// get the user input
// compare the options with the user input
// if one is equal with the user input submit the form with the method submit();
// else don't submit the form, the user will have to change his input
}
</script>
If you wish some help with the js i'll be happy to help.
If you want to go with server side validation,
<form method="post">
<input type="text" list="browsers" name="items" required>
<datalist id="browsers" >
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']
$check = array('Internet Explorer', 'Firefox', 'Chrome', 'Opera', 'Safari');
if(in_array($_POST['items'], $check)){
// Code to be execute.
} else{
echo'Select something from the datalist!';
}
}else{
echo'Select something from the datalist!';
}

Unable to post the form values without refreshing after changing option

I have a following form in my HTML code.
When I forget to fill in the surname,
the form will show me a "bubble message" You have to fill in the surname, then I
will fill in the surname, press the submit button and the values are submitted.
When I forget to change the default option from <select> the form will show
me a "bubble message" You have to choose something, then I will choose another option,
press the submit button, but the values are not submitted. I always have to refresh
the page. Do you know where is the problem and how to achieve it without refreshing?
<form action="add" method="post">
...
<select id="choice" name="choice"
required oninvalid="setCustomValidity('You have to choose something')"
oninput="setCustomValidity('')">
<option value="" selected="selected"></option>
<option value="first">first</option>
<option value="second">second</option>
</select>
...
<input type="text" name="surname" id="surname" value=""
required oninvalid="setCustomValidity('You have to fill in the surname')"
oninput="setCustomValidity('')">
...
</form>
There is no problem with the form data submitting as you will see from this jsfiddle example:
http://jsfiddle.net/vbnh1a4y/
<form action="/echo/html" method="post">
<select id="choice" name="choice">
<option value="" selected="selected"></option>
<option value="first">first</option>
<option value="second">second</option>
</select>
<input type="text" name="surname" id="surname" value="" />
<input type='submit' />
</form>

Passing two variables via GET to PHP using HTML form

I'm trying to pass two variables (a search query (String) and a column name (also a String)) via a HTML form. The first string is entered into a text box and the second is selected from a drop-down menu.
Obviously this method doesn't work:
<h3>Search for a Customer</h3>
<form action="search.php" method="get">
<input type="text" id="sString">
<select name="sField">
<option value="Name">Name</option>
<option value="HostID">Host ID</option>
<option value="OrderNumber">Order Number</option>
<option value="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
You need to give your <input> field a name in order for this to work. Unnamed inputs won't get passed through the form post.
You don't have a 'name' parameter on your sString input boxes. The name parameter is what gets sent back to the server, not the field's ID:
<input type="text" id="sString" name="sString">
and then in PHP
$search = $_GET['sString'];
You need to use attribute name instead of value.
<h3>Search for a Customer</h3>
<form action="search.php" method="get">
<input type="text" id="sString" name="sString"/>
<select name="sField">
<option name="Name">Name</option>
<option name="HostID">Host ID</option>
<option name="OrderNumber">Order Number</option>
<option name="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
Try:
<form action="search.php" method="get">
<input type="text" name="sString" id="sString" value="">
<select name="sField">
<option value="Name">Name</option>
<option value="HostID">Host ID</option>
<option value="OrderNumber">Order Number</option>
<option value="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
You should be able to access form entered variables by $_GET array in search.php. To see what is going on try var_dump($_GET);

Categories