<input type="text" name="prod_id" value="<?php echo $product['table']['id']; ?>" />
But it doesn't show up anything although the $product variable is sure to have been properly initialized, what might be the problem ?
Here is the print_r produce
Array (
[0] => Array (
[product_table] => Array (
[id] => 5
[quantity] => 20
[name] => something
)
)
)
You have to access the variable as $product[0]['product_table']['id'] and not $product['table']['id']
You're trying to output contents of a non-existing key. You have 'product_table', not 'table'.
Edit:
And it also needs to be $product[0]['product_table']['id'].
<input type="text" name="prod_id" value="<?php echo $product[0]['product_table']['id']; ?>" />
Please add index [0] to ensure that if the provided array have multiple values then only first one will be picked as well.
Use this
<?php
extract($product[0]);
<input type="text" name="prod_id" value="<?php echo $product_table['id']; ?>" />
Related
I've designed one HTML form as follows :
<form action="sample_test.php" method="post">
<input type="text" name="fileName" value="8.png" id="fileName[]">
<input type="text" name="fileLink" value="https://www.filepicker.io/api/file/zZ993JyCT9KafUtXAzYd" id="fileLink[]">
<input type="text" name="fileName" value="2_OnClick_OK.jpg" id="fileName[]">
<input type="text" name="fileLink" value="https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ" id="fileLink[]">
<input type="submit" name="Submit" value="Submit File">
</form>
Then the code in sample_test.php is as follows :
<?php
print_r($_POST); die;
?>
The output I got is as follows :
Array ( [fileName] => 2_OnClick_OK.jpg [fileLink] => https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ [Submit] => Submit File )
But this is not the desired output. I want the desired output array to be printed in following manner:
Array
(
[8.png] => Array
(
[0] => https://www.filepicker.io/api/file/zZ993JyCT9KafUtXAzYd
)
[2_OnClick_OK.jpg]
(
[0] => https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ
)
)
For now I've just demonstrated with two elements only but in real situations hundreds of such elements could present on the form.
So what changes do I need to make in my HTML as well as PHP code? Please help me.
Thanks in advance.
What you ask is impossible by just modifying the HTML code, because you would like a value (of fileName) to become an index in the array you get. That's impossible, the index will always be the name of the input.
However, if you have a look here : POSTing Form Fields with same Name Attribute , you will be able to get arrays of fileName and fileLink, and I'm pretty sure you can do something from there.
A few things wrong, but you are close. Make the name field an array instead of the id - plus your ids need to be unique.
<input type="text" name="fileName[]" value="8.png" id="fileName1">
<input type="text" name="fileLink[]" value="https://www.filepicker.io/api/file/zZ993JyCT9KafUtXAzYd" id="fileLink1">
<input type="text" name="fileName[]" value="2_OnClick_OK.jpg" id="fileName2">
<input type="text" name="fileLink[]" value="https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ" id="fileLink2">
Not tested, but should do the trick.
I am trying to figure out how to loop through multiple POST form data that is dynamically pulled from a database and re-submit the modified data to a different table. For some reason (probably old age) I can't seem to come up with a solution that works.
I am already looping out all the records from one table (call it roster) and need to submit it to another table (call it roster2). The form is something similar to this:
<form name="name" action="form.php" method="post">
<input type="text" name="name1" value="15">
<input type="text" name="attended" value="1">
<input type="checkbox" name="nameid_12" value="12">
<input type="text" name="name2" value="8">
<input type="text" name="attended" value="1">
<input type="checkbox" name="nameid_6" value="6">
</form>
The 'name' and 'nameid' fields will always change and the number of records displayed will always be different (one day it could be 5 and the next 100).
What is the best way to loop through the POST data to submit it to the database keeping all the associations intact?
I am relatively new to working with PHP and I can't seem to figure out a good way to do this.
If you're going to generate them dynamically, I would recommend using PHP to place the ID of each form in the inputs on that form. So your original form would end up with these names:
<input type="text" name="name[12]" value="15">
<input type="text" name="attended[12]" value="1">
<input type="checkbox" name="nameid[12]" value="12">
<input type="text" name="name[6]" value="8">
<input type="text" name="attended[6]" value="1">
<input type="checkbox" name="nameid[6]" value="6">
Then your arrays will have keys corresponding to their form's ID. The array structure looks like this.
Array (
[name] => Array ( [12] => 15, [6] => 8 )
[attended] => Array ( [12] => 1, [6] => 1 )
[nameid] => Array ( [12] => 12 [6] => 6 )
)
Now we need to figure out which ids are actually present today. The array_keys() function generates an array of keys from any source array. Keys will be the same for each of the three elements, so I arbitrarily take the keys from [name].
$id_array = array_keys($_POST['name']);
Then, to access each element of the POST array, we'll use a foreach.
foreach ($id_array as $id) {
//assign variables
$name = $_POST['name'][$id];
$attended = $_POST['attended'][$id];
$nameid = $_POST['nameid'][$id];
//store
//Using whichever database style you like. I prefer PDO.
}
You can loop through the fields and retain their keys like so:
foreach ($_POST as $field_name => $field_value) {
// Storage
}
I'm trying to get a multidimensional array to store the info from three separate elements (caption, image, and subfolder). The three elements are descriptions from photos that individuals may upload onto my site. I've built an initial page that asks the visitor how many photos they intend to upload and from which country(an input type=select). Based upon their response on that initial form, the second page uses a loop to generate the appropriate number of , , and elements. Once the second form is submitted the intention is to upload the info to the server which it does. The second objective is to store that information inside the multidimensional array such as below in order to upload into a MySQL table. My dilemma is in getting the image names presumably from ($_FILES['image']['name']) to insert themselves into my multidimensional array. The captions '' insert themselves into the multidimensional array as does the country name ('name=subfolder']) but not the image names. I'd greatly appreciate anyone willing to help with this.
Thank you.
Array ( [caption] => Array ( [0] => Nice Simple Picture [1] Another Nice Simple Picture=> )
[image] => [subfolder] => Array ( [0] => Italy ) [upload] => UPLOAD )
Here's a bit of how I've attempted to go about this..
<?php
$file=$_FILES['image']['name'];
$expected = array('image','subfolder','caption');
foreach ($_POST as $key => $value) {
if (in_array ($key, $expected)) {
${$key} = mysql_real_escape_string($value); }}
$sql = "INSERT INTO images (file_name, country, caption)
VALUES ('$image', '$subfolder', '$caption')";
$result = mysql_query($sql) or die (my_sql_error()); }
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" id="form1" name="form1" method="POST"
enctype="multipart/form-data">
<?php
for ($i=0; $i<$imgnumb; $i++) {
echo "<input type=\"file\" name=\"image[".$i."]\" /> <input type=\"text\"
name=\"caption[".$i."]\" /><br />"; }
?>
<br />
<input value="<?php echo $file; ?>" name="image" type="hidden" />
<input value="<?php echo $location; ?>" name="subfolder" type="hidden" />
<input value="<?php echo $_POST['caption']; ?>" name="caption" type="hidden" />
<input type="submit" name="upload" id="next" value="UPLOAD" />
</form>
Issue 1: I have a form that contains a list of events pulled from a database, alongside which are checkboxes for customers to register their attendance. There is an additional checkbox to indicate any special requirements, as well as a text field to elaborate ('If yes, please provide additional details...').
I would like to store every event a customer is attending in a multi-dimensional array containing the event ID, 'yes/no' for special requirements and details of any such requirements. I thought I was on track with:
<form id="bookingform" method="post" action="booking-details.php">
<fieldset>
<legend>Personal information</legend>
<div class="bookingitem">
<label for="firstname">First name</label> <input type="text" name="firstname" id=
"firstname" />*<br />
</div>
<div class="bookingitem">
<label for="surname">Surname</label> <input type="text" name="surname" id=
"surname" />*<br />
</div>
<div class="bookingitem">
<label for="company">Company</label> <input type="text" name="company" id=
"company" />*<br />
</div>
</fieldset>
<fieldset>
<legend>Which exhibition(s)?</legend>
<div class="bookingitem">
<?php
$venues_query="SELECT exhibitions.exhib_id AS exhib_id, venues.venue_name AS venue, DATE_FORMAT(exhibitions.exhib_date, '%d/%m/%y') AS date FROM venues, exhibitions WHERE exhibitions.venue_id = venues.venue_id AND (exhibitions.exhib_date>=CURDATE())ORDER BY exhibitions.exhib_date";
$venues_result=mysql_query($venues_query);
while($venues=mysql_fetch_array($venues_result, MYSQL_ASSOC)){
echo '<input type="checkbox" name="registrations['.$venues['exhib_id'].'][id]" /> '.$venues['venue'].' - '.$venues['date'].'<br/>';
echo '<input type="checkbox" name="registrations['.$venues['exhib_id'].'][requirements]" />Special requirements?<br/>';
echo 'If yes, please give more details... <input type="text" name="registrations['.$venues['exhib_id'].'][requirements_details]" />';
}
mysql_close();
?>
</div>
</fieldset>
<fieldset>
<legend>Terms and conditions:</legend>
<p>T&Cs here</p>
<div>
<input type="submit" class="buttons" name="submit" value="Submit" />
</div>
</fieldset>
</form>
... but when I do var_dump($_POST['registrations']);, I'm only getting:
array(4) { [0]=> string(5) "00132" [1]=> string(5) "00140" [2]=> string(5) "00135" [3]=> string(5) "00136" }
The five-digit numbers are the event IDs, pulled from the database (I've registered for four events in this example), but the other info seemingly isn't being stored. I expect it's blindingly obvious, but can anyone spot where I'm going wrong?
I want to be able to loop through through the values (as per this example here) using the following code:
foreach ( $_POST['registrations'] as $registration )
{
echo '$registration[id]';
echo '$registration[requirements]';
echo '$registration[requirements_details]';
// etc
}
Issue 2: I would like to make sure that when the special requirements box is ticked, the details box is also filled out. Previously, I had three separate arrays for each part of the form, and did a count() on the number of ticks and the number of input boxes completed. If they didn't match, the form wouldn't process. However, I'm sure there's a much easier way of achieving this and would be grateful for any advice!
When I try what I believe is your resultant HTML, I get the following from print_r:
Array
(
[registrations] => Array
(
[11111] => Array
(
[id] => on
[requirements] => on
[requirements_details] => 1
)
[22222] => Array
(
[id] => on
[requirements] => on
[requirements_details] => 2
)
[33333] => Array
(
[id] => on
[requirements] => on
[requirements_details] => 3
)
[44444] => Array
(
[id] => on
[requirements] => on
[requirements_details] => 4
)
[55555] => Array
(
[id] => on
[requirements] => on
[requirements_details] => 5
)
)
)
Can you update your question with the HTML produced for the complete form?
I have a form that includes the first name and last name of a person. The user can add multiple people using a link, that creates new input fields via JS. Here's an example of a form that includes 2 people:
<form action="" method="post">
<input type="text" class="required" name="people[first][]" />
<input type="text" class="required" name="people[last][]" />
<input type="text" class="required" name="people[first][]" />
<input type="text" class="required" name="people[last][]" />
<input type="submit" name="submit">
</form>
I'm trying to figure out a way to insert this data into the database. I've tried using:
foreach ($_POST['people'] as $person) {
foreach ($person as $value) {
echo $value . '<br/>';
}
}
.. which results in
first name 1
first name 2
last name 1
last name 2
I'm trying to group the results somehow so I can insert a new row for each first name x + last name x combination.
Create the input elements like this:
<input type="text" name="people[0][first]" />
<input type="text" name="people[0][last]" />
<input type="text" name="people[1][first]" />
<input type="text" name="people[1][last]" />
In your PHP:
foreach ($_POST['people'] as $person) {
echo $person['first'].' '.$person['last'].'<br />';
}
$_POST['people']['first'] is an array of first names.
$_POST['people']['last'] is an array of last names.
You can merge them into an array of arrays like this:
$people = $_POST['people'];
$length = count($people['first']);
for($i = 0; $i < $length; $i++)
$temp[] = array('first' => $people['first'][$i], 'last' => $people['last'][$i]);
$people = $temp;
The resulting array in $people will be an array of associative arrays, and might look like:
Array
(
[0] => Array
(
[first] => Jim
[last] => Smith
)
[1] => Array
(
[first] => Jenny
[last] => Johnson
)
)
which is equivalent to the array you would get by modifying your HTML as bsdnoobz has shown you can do as well. Iterating through it would be the same too:
foreach ($people as $person) {
echo $person['first'] . ' ' . $person['last'] . '<br />';
}