PHP Get values from Multiple form arrays - php

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.

Related

PHP can't get value from select option HTML [duplicate]

This question already has answers here:
Browsers does not reflects updates in the code [closed]
(12 answers)
Closed 2 years ago.
I have a simple form where users can register, and after submiting I want to work with the values to check everything is fine, the problem is that I cannot get some values.
This is the form in index.php:
<form class="form-detail" action="correcto.php" method="post" id="myform">
<select name="personaEmpresa" id="personaEmpresa">
<option class="option" value="empresa">Soy una empresa</option>
<option class="option" value="persona">Soy una persona</option>
</select>
<input type="text" name="first_name" id="first_name" class="input-text" placeholder="Nombre" required>
<input type="text" name="last_name" id="last_name" class="input-text" placeholder="Apellidos" required>
</form>
I have reduced the size of the form to be more concrete. Once I submit the form and go to correcto.php, I can perfectly work with "first_name" and "second_name" but for some reason I am not able to get the value from "personaEmpresa"
This is the code on correcto.php:
<?php
//I cannot get the value from this one
echo $personaEmpresa = $_POST["personaEmpresa"];
echo "<br>";
echo $nombre = $_POST["first_name"];
echo "<br>";
echo $apellidos = $_POST["last_name"];
Thank you all very much!
Did you put a submit type input into the form?
<form class="form-detail" action="correcto.php" method="post" id="myform">
<select name="personaEmpresa" id="personaEmpresa">
<option class="option" value="empresa">Soy una empresa</option>
<option class="option" value="persona">Soy una persona</option>
</select>
<input type="text" name="first_name" id="first_name" class="input-text" placeholder="Nombre" required>
<input type="text" name="last_name" id="last_name" class="input-text" placeholder="Apellidos" required>
<input type="submit" value="">
Finally the error was just that the cache of the browser needed to be updated. You can use classic cache killer in Chrome or Firefox to solve this.

Display html inputted data with PHP

Below is my HTML and PHP code. I'm trying to display my the inputted data, however, I'm getting nothing. What is wrong with my code?
HTML
<form action="welcome.php" method="POST">
<fieldset>
<legend> Personal Details: </legend>
<label for="fname>"></label>
<input type="text" name="Name" id="fname" required autofocus placeholder="First Name" pattern="[a-zA-Z]{3,}" title="Please enter more than three letters">
<label for="lname>"></label>
<input type="text" name="Last Name" id="lname" required autofocus placeholder="Last Name" pattern="[a-zA-Z]{3,}" title="Please enter more than three letters">
<label for="email">Email: </label>
<input type="text" name="email" id="email" required placeholder="Your school email" pattern="[a-zA-Z]{3,}#[a-zA-Z]{3,}[.]{1}[a-zA-Z{2} title="Please enter a valid email address>
<label for="phone">Phone: </label>
<input type="tel" name="phone" id="phone" required placeholder="Please enter in your phone number" pattern="[0-9]{4} [0-9]{3} [0-9]{3}" title="Please enter in a phone number in this format: #### ### ###">
<select name="country" required>
<option value=""> </option>
<option value="US">US</option>
<option value="UK">UK</option>
<option value="AUS">AUS</option>
</select>
</fieldset>
<br>
<fieldset>
<legend> Booking Details: </legend>
<input type="date" name="date" min="2018-10-07" max="2018-10-31">
<input type=time min=9:00 max=17:00 step=900>
<br>
<br>
<label for="dorm">Dormitory: </label>
<br>
<select name="dorm" required>Dormitory
<option value="Cypress">Cypress Hall</option>
</select>
<br>
<br>
<label for="floor">Floor: </label>
<br>
<select name="floor" required>Floor:
<option value=""></option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
</select>
<br>
<br>
<label for="roomnumber">Room Number: </label>
<br>
<select name="roomnumber">Room Number:
<option value=""></option>
<option value="22">22</option>
</select>
<br>
<br>
<label for="roomletter">Room Letter: </label>
<br>
<select name="roomletter" required="">Room Letter
<option value=""></option>
<option value="A">A</option>
<option value="B"> B</option>
</select>
<br>
<br>
<label for="bedroomcleaning">Bedroom: </label><br>
<input type="checkbox" id="box-4" onclick="checkPrice()" value="4">Dust all ceiling fans/light/fixtures within reach.<br>
<input type="checkbox" id="box-5" onclick="checkPrice()" value="5"> Change sheets and/or fold clothes if requested by client.<br>
<input type="checkbox" id="box-6" onclick="checkPrice()" value="6"> Straighten up, put toys away, make beds, fold clothes and put on bed. Straighten papers and put in a pile. DO NOT THROW AWAY ANY PERSONAL ITEMS!<br><br>
<label for="bathroomcleaning">Bathroom: </label><br>
<input type="checkbox" id="box-1" onclick="checkPrice()" value="1"> Clean bowl and wipe down toilet cover, seat, under seat, base and behind the base.<br>
<input type="checkbox" id="box-2" onclick="checkPrice()" value="2"> Clean all mirrors.<br>
<input type="checkbox" id="box-3" onclick="checkPrice()" value="3"> Clean countertops and backsplashes.<br>
<label for="bathroomprice" id="price">Total Price: </label>
<br>
<br>
<input type="submit">
</fieldset>
</form>
PHP
Welcome <?php echo $_POST["Name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
Your phone number is: <?php echo $_POST["phone"]; ?>
Check your pattern for the email input:
[a-zA-Z]{3,}#[a-zA-Z]{3,}[.]{1}[a-zA-Z{2}
[a-zA-Z]{3,}#[a-zA-Z]{3,}[.]{1}[a-zA-Z]{2}
Also:
<input type=time min=9:00 max=17:00 step=900>
<input type="time" min="9:00" max="17:00" step="900">
As said by Jon in the comments, you need a <form> div.
<form action="yourPhpPage.php" method="POST">
<label ...>...</label>
<input ... />
...
<input type="submit" value="Send" />
</form>
The action attribute tells which page to call when submitting form.
The method tells how to send data (GET or POST mainly)
The <input type="submit" /> is the submit button. When you click it the page indicated in action will be called with the values of the different inputs. The value of the submit input is what is shown on it.
Eventually you can use a submit <button> :
<button type="submit">What you want to be displayed on it.</button>
I prefer this one because it's easier to modify its style and behavior respectively with CSS and JS. But it's not the point.
Hope it helps you !Jean-Marc.

why am i getting undefined indexs when i use enctype="multipart/form-data"

why am I getting undefined index's with my form is it because of the encoding type I am using, if so what can I do to fix this to properly post my variables
<form enctype="multipart/form-data" name="pmForm" id="pmForm" method="post" action="personalspage.php"><br>
<b>Age</b> <input type="text" name="age" id="age" cols="4"><br><br>
<b>University</b> <select name="university" id="university" onfocus="emptyElement('status')">
<option disabled selected>select one...</option>
<option value="Algoma">Algoma University</option>
<option value="york">York University</option>
</select><br><br>
<b>Headline</b> <input type="text" name="headline" id="headline"><br><br>
<b>Message</b> <textarea name="message" id="message" rows="6" cols="50"></textarea><br><br>
<b>Add a picture</b> <input type="file" name="photo" id="photo" accept="image/*"><br><br>
<input type="hidden" name="mysex" id="mysex" value="<?php echo $_POST["mysex"]; ?>">
<input type="hidden" name="lookingfor" id="lookingfor" value="<?php echo $_POST["lookingfor"]; ?>">
<center><input type="submit" name="adSubmit" id="adSubmit" value="Post It"></center>
</form>
I know that the variables being posted from say page1 to this form are coming through because I have an if statement with an isset() for the variables making it header to another page if there not set. this form code is from page2
im using this code on page3 to recieve the form data
$mysex = $_POST['mysex'];
$lookingfor = $_POST['lookingfor'];
$uni = $_POST['university'];
So when I post all the variable from this form to another page I get
Notice: Undefined index: mysex in C:\xampp\htdocs\Website\personalspage.php on line 4
Notice: Undefined index: lookingfor in C:\xampp\htdocs\Website\personalspage.php on line 5
Notice: Undefined index: university in C:\xampp\htdocs\Website\personalspage.php on line 6
I double checked and made sure that all my methods are using post, the only thing I can think of why this isnt working is because of some sort of combination of echoing input values and the enctype. If anyone could help me out it would be greatly appreciated.
Undefined variable university means you didnt select any options to fix that you can set any of the ptions to selected and for hidden variables problem is value is not there. fixed code is here
<form enctype="multipart/form-data" name="pmForm" id="pmForm" method="post" action="personalspage.php"><br>
<b>Age</b> <input type="text" name="age" id="age" cols="4"><br><br>
<b>University</b> <select name="university" id="university" onfocus="emptyElement('status')">
<option disabled selected value="" selected>select one...</option>
<option value="Algoma">Algoma University</option>
<option value="york">York University</option>
</select><br><br>
<b>Headline</b> <input type="text" name="headline" id="headline"><br><br>
<b>Message</b> <textarea name="message" id="message" rows="6" cols="50"></textarea><br><br>
<b>Add a picture</b> <input type="file" name="photo" id="photo" accept="image/*"><br><br>
<input type="hidden" name="mysex" id="mysex" value="<?php if(isset($_POST["mysex"])) echo $_POST["mysex"];else echo ""; ?>">
<input type="hidden" name="lookingfor" id="lookingfor" value="<?php if(isset($_POST["lookingfor"]))echo $_POST["lookingfor"];else echo ""; ?>">
<center><input type="submit" name="adSubmit" id="adSubmit" value="Post It"></center>
</form>
I Have Any Two option
First:
error_reporting(0);
Second:
<input type="hidden" name="mysex" id="mysex"
value="<?php if(isset($_POST["mysex"])){ echo $_POST["mysex"]; }?>">
<input type="hidden" name="lookingfor" id="lookingfor"
value="<?php if(isset($_POST["lookingfor"])){ echo $_POST["lookingfor"]; }?>">

PHP action / salesForce

I have been asked to update a contact form, i always leave action blank(action="") in any forms i have worked on before. I have been sent the following form as seen below. When the form is submitted, it is sent to the url, which is a blank screen. Has anyone implemented a salesForce contact form before ?
<form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">
<input type=hidden name="oid" value="00D200000005wgb">
<input type=hidden name="retURL" value="http://"> <!-- add URL of completed page here
-->
<label for="first_name">First Name</label><input id="first_name" maxlength="40" name="first_name" size="20" type="text" /><br>
<label for="last_name">Last Name</label><input id="last_name" maxlength="80" name="last_name" size="20" type="text" /><br>
<label for="email">Email Address</label><input id="email" maxlength="80" name="email" size="20" type="text" /><br>
<label for="phone">Phone</label><input id="phone" maxlength="40" name="phone" size="20" type="text" /><br>
<select id="country" name="country" title="country">
<option value="London">London</option>
<option value="Midlands">Midlands</option>
<option value="North England">North England</option>
<option value="South England">South England</option>
<option value="Wales">Wales</option>
<option value="Scotland">Scotland</option>
<option value="Northern Ireland">Northern Ireland</option>
</select><br>
<textarea id="00N200000016Kk5" name="00N200000016Kk5" rows="4" type="text" wrap="soft"></textarea><br>
<!-- Below 3 fields are hidden -->
<select id="recordType" name="recordType" style="display:none;">
<option value="012w0000000QCbe">Prospect UK</option></select>
<select id="00N20000001L8Pi" name="00N20000001L8Pi" title="Web Source" style="display:none;"><option value="Download Whitepapers UK">Download Whitepapers UK</option> </select>
<select id="lead_source" name="lead_source" style="display:none;">
<option value="Download Whitepapers UK">Download Whitepapers UK</option></select>
<input type="submit" name="submit">
</form>
The action attribute of a <form> is basically a redirect for the form that sends the POST or GET data along with it.
If action is left blank, it redirects to the current URL, thus "refreshes".
If action is a link beginning with http:// or https://, the form sends the $_POST / $_GET request to the full page link.
If action is set to a link not beginning with http:// nor https://, the link set in the action is added to the current sub-directory in the same way a php include works.
Therefore, this in your code:
<form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">
Will send a POST request to https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8

form and file upload to two different controllers

I have a form that I want to submit with one submit button.
However my action to upload a file is a different controller then the database posts. it looks like this...
<form method="post" action="/admin/upholstery_product/add/">
<h3>Add a Product</h3>
Frame: <input type="text" name="productname" /><br>
Parent Category:
<select name="category">
<?php
foreach($cats AS $cat){
$sel = ($curCat->category_id == $cat->category_id) ? 'selected' : '';
echo "<option value='".$cat->category_id."' ".$sel.">" . $cat->category_name . "</option>\n";
}
?>
</select><br />
Family: <input type="text" name="family" /><br>
Width: <input type="text" name="width" size="6" /><br>
Height: <input type="text" name="height" size="6" /><br>
Depth: <input type="text" name="depth" size="6" /><br>
Seat Width: <input type="text" name="seat_width" size="6" /><br>
Seat Height: <input type="text" name="seat_height" size="6" /><br>
Seat Depth: <input type="text" name="seat_depth" size="6" /><br>
Arm Height: <input type ="text" name="arm_height" size="6" /><br>
Features: <textarea cols="40" rows="20" name="features"></textarea><br>
<form method="post" action="/upload/product_image_upload">
Image File: <input type="file" name="image" id="image"/><br>
</form>
<form method="post" action="/upload/product_image_upload">
Thumbnail File: <input type="file" name="thumbnail" id="thumbnail"/><br>
</form>
<form method="post" action="/upload/pdf_upload">
PDF File: <input type="file" name="pdf" id="pdf"/><br>
</form>
Status: <select name="status">
<option value="active">Active</option>
<option value="inactive">Inactive</option>
</select> <br>
<input type="submit" name="submit" value="Add Product" /><br>
</form>
is there a way to post the file actions to one controller and the submit to my other controller with a single submit button? right now the code doesnt work.
What you want to do is, strictly speaking, not possible: You have two separate forms on one page, andf you want to submit both simultanously.
The main problem here is this: The result of a submitted form is a new page. Different actions = different pages. Which one do you want to display?
Workaround: Submit both the file and the data to the same script; if the handlers need to be separate, make the first handler post the data to the second handler "behind the scenes".

Categories