MultiDimentional array in $_POST, checklist form - php

I am trying to make a form that mocks a checklist of sorts. Let me explain:
I have a number of jars. Inside each jar are a number of jelly beans. Each bean is different. I can have any number of jars and beans. I want to create a form as so:
public static function getForm($oData) {
<form action="<?= esc_url($_SERVER["REQUEST_URI"]); ?>" method="post" id="CountForm">
<fieldset>
<?php foreach ($aJars as $oJar) {
$aJellyBeans = JellyBeans::getJellyBeansBase($oJar->Jar_ID); ?>
<p><?= $oJar->JarName; ?></p>
<?php foreach ($aJellyBeans as $oJellyBean) { ?>
<input type="checkbox" name="?" id="CountedFor" value="1">
<input type="hidden" name="?" id="AccountedFor_ID" value="<?= $oData->AccountedFor_ID; ?>">
<input type="hidden" name="?" id="Jar_ID" value="<?= (($oData->Jar_ID) ? $oData->Jar_ID : $oJellyBean->Jar_ID); ?>">
<input type="hidden" name="?" id="JellyBean_ID" value="<?= (($oData->JellyBean_ID) ? $oData->JellyBean_ID : $oJellyBean->JellyBean_ID); ?>">
<label for="Notes">Notes</label>
<textarea name="?" id="Notes" cols="30" rows="5" wrap="soft" placeholder=" "><?= $oData->Notes; ?></textarea>
<?php } ?>
<?php } ?>
<input type="submit" name="Submit" id="Submit" value="<?= (($oData->AccountedFor_ID) ? 'Update' : 'Add' ) ?>">
</fieldset>
</form> <?php
}
When $_POST gets sent it goes to a class function that handles writing to the DB. The function will look at the name field and use that as the column name.
protected function CreateAccountedFor($aData) {
//Process provided data
foreach ($aData as $sKey => $eValue) {
$aSQL[$sKey] = $eValue;
}
}
I want to create a list of each jelly bean, sorted by jar, and be able to check each off as "accounted for" and submitted in one go.
I know the name field is what $_POST looks at, but is there a way to manipulate that property to form an array like:
["AccountedFor"]=>
[0]=>
["AccountedFor_ID"]=> "..."
["Jar_ID"]=> "..."
["JellyBean_ID"]=> "..."
[1]=>
["AccountedFor_ID"]=> "..."
["Jar_ID"]=> "..."
["JellyBean_ID"]=> "..."

You can assign the array values in the input name, use a for loop to count which iteration you're currently on.
If you're generating multiple of these inputs you're going to have invalid code, the id for each input will be repeated, maybe use classes instead or ditch them altogether.
<?php for ($i = 0; $i < count($aJellyBeans); $i++) { ?>
<input type="checkbox" name="AccountedFor[$i][CountedFor]" id="CountedFor" value="1">
<input type="hidden" name="AccountedFor[$i][AccountedFor_ID]" id="AccountedFor_ID" value="<?= $oData->AccountedFor_ID; ?>">
<input type="hidden" name="AccountedFor[$i][Jar_ID]" id="Jar_ID" value="<?= (($oData->Jar_ID) ? $oData->Jar_ID : $aJellyBeans[$i]->Jar_ID); ?>">
<input type="hidden" name="AccountedFor[$i][JellyBean_ID]" id="JellyBean_ID" value="<?= (($oData->JellyBean_ID) ? $oData->JellyBean_ID : $aJellyBeans[$i]->JellyBean_ID); ?>">
<label for="Notes">Notes</label>
<textarea name="?" id="Notes" cols="30" rows="5" wrap="soft" placeholder=" "><?= $oData->Notes; ?></textarea>
<?php } ?>

Related

How to Keep the data that user entered in textbox even after submit button is clicked and even if page is reloaded

I want to keep the data in a text box after submit and inserted into db.
even if i refresh the page data should not change or empty
here is my code
<tr class="form-field1">
<th valign="top" scope="row">
<label for="approved_admin"><?php _e('Collaboration Admin', 'custom_table_example')?></label>
</th>
<td>
<input id="approved_admin" name="approved_admin" type="email" value="<?php echo esc_attr($item['approved_admin'])?>"size="50" class="code" placeholder="<?php _e('Admin Email', 'custom_table_example')?>" required>
</td>
</tr>
if i do this data will be there if i reload empty text box will be shown.I need to make this readonly and onload same text should be there.
Form
<form id="form" method="POST">
<input type="hidden" name="nonce" value="<?php echo wp_create_nonce(basename(__FILE__))?>"/>
<?php /* NOTICE: here we storing id to determine will be item added or updated */ ?>
<input type="hidden" name="sid" value="<?php echo $item['sid'] ?>"/>
<div class="metabox-holder" id="poststuff">
<div id="post-body">
<div id="post-body-content">
<?php /* And here we call our custom meta box */ ?>
<?php do_meta_boxes('Settings', 'normal', $item); ?>
<input type="submit" value="<?php _e('Save', 'custom_table_example')?>" id="submit" class="button-primary" name="submit" / >
<input type="submit" value="<?php _e('Edit', '')?>" id="edit" class="button-primary" name="edit" / >
</div>
</div>
</div>
</form>
I tried doing
<input type="email" name="approved_admin" value="<?php if (isset($_POST['approved_admin'])) { echo $_POST['approved_admin']; } ?>">
and also
<input type="text" name="country" value="Norway" <?php isset($approved_admin) ? echo 'readonly' : ''; ?> >
You can save data after page reload a lot of ways like... Session , cookie, get , post. After this value get by isset and echo .
In your case Post method used..
Try this working fine all thing
<input type="text" name="country" value=" <?php echo isset($_POST['country']) ? $_POST['country'] : ''; ?>" <?php echo isset($_POST['country']) ? 'readonly' : ''; ?> >
You just put field name in isset();
You can use this syntax. Have a try for this.
<input type="text" id="to_date" name="to_date" class="form-control" placeholder="Enter End Date" value="<?php echo isset($_POST['to_date']) ? $_POST['to_date'] : '' ?>" />
The name of the input type has to be mentioned in the isset() condition.

How to use values in the URL in PHP

I am currently making a report error form that has 4 fields:
Job ID $jobid
Part ID part_id
Machine
Note
The user clicks on a table corresponding the their work and are brought to a new page with a url that has variable. At the moment all the fields are empty however I want the fields to be populated automatically except for notes.
Current Model
Link to report error form:
$EM_html = ''.$tick.'
Report error form:
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Example URL
http://sra-pstest/report_error_form.php?JobID=KANBAN16-09-04-01&Machine=EM&PartID=124047
How do "extract" the information out of the url (JobID, Machine, PartID) and automatically fill out the form?
You can use $_GET
<?php
if(isset($_GET))
{
foreach($_GET as $key=>$value)
{
$$key=$value;
}
echo $JobID."<br>".$Machine."<br>".$PartID;
}
?>
Please try this
<?php
$jobid = #$_REQUEST['JobID'];
$part_id = #$_REQUEST['PartID'];
$machCode = #$_REQUEST['Machine'];
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
You use $_GET Method like this code
<?php
$jobid=$part_id=$machine="";
if(isset($_GET['JobID']))
{
$jobid= $_GET['JobID'];
}
if(isset($_GET['Machine']))
{
$machine= $_GET['Machine'];
}
if(isset($_GET['PartID']))
{
$part_id= $_GET['PartID'];
}
?>
<form action="" method="post">
<?php $jobNumber = isset($_GET['JobID']) ? $_GET['JobID'] : '' ?>
Job Number: <input type="text" value="<?php echo jobNumber; ?>" name="jobNum"><br>
<input type="submit" name="submit" value="Submit">
</form>
Try using isset and post method to check if variable are declared and get the variable data on submit of form
<?php
if(isset($_POST['submit'])){
$jobid = $_POST['JobID'];
$part_id = $_POST['PartID'];
$machCode = $_POST['Machine'];
}
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php echo $jobid; ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php echo $part_id; ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode" value="<?php echo $machCode; ?>"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Hope this help

Keep form data inside the field after submission using php

I am using below code for a html form.(it has two forms) I am able to keep the textarea field after the first and second form submission. but issue I am facing here is the dropdown menu selection.
Code:
<html>
<body>
<div class="emcsaninfo-symcli-main">
<form id="form1" name="form1" action=" " method="post" >
<div class="input">Your Name</div>
<div class="response"><span><input class="textbox" id="myname" name="myname" type="text" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>" /></span> </div>
<div class="input">Your Place</div>
<div class="response"><span><input class="textbox" id="myplace" name="myplace" type="text" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" /></span> </div>
<div class="input-quest">Graduation Status</div>
<div class="input-resp"><select id="graduation" name="graduation" OnChange="CMT();"><option class="dropdown-options">Graduate</option><option class="dropdown-options">Non Graduate</option></select></div>
<div class="submit">
<input id="first_submit" type="submit" name="first_submit" value="first_submit" />
</div>
</form>
<?php
if(!empty($_POST['myname']) && !empty($_POST['myplace']) || !empty($_POST['output_textarea'] ) )
{
$myname = $_POST['myname'];
$myplace = $_POST['myplace'];
$graduation = $_POST['graduation'];
?>
<form id="form2" name="form2" action=" " method="post" >
<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly value="<?php if(isset($_POST['output_textarea'])) { echo htmlentities ($_POST['output_textarea']); }?>">
<?php
echo "My name is $myname and I am from $myplace, and I am $graduation";
?>
</textarea>
<input id="submit1" type="submit" name="name_field" value="submit1" />
<input id="submit2" type="submit" name="place_field" value="submit2" />
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />
</form>
<?php
function name()
{
echo $_POST["output_textarea"];
}
if(isset($_POST['name_field']))
{
name();
}
function place()
{
echo $_POST["output_textarea"];
}
if(isset($_POST['place_field']))
{
place();
}
}
?>
</div>
</html>
</body>
For example if I put name = John, place : UK and selecting graduation status as graduate, it will will give me first form output as in my output textarea
My name is John and I am from UK, and I am Graduate
I have two seperate submit button for second form, using that I am doing some other function with help of the output textarea
If I press any of the second button,I m able to keep entries my name and place area, but it not keeping the dropdown selection. so it will only display like after submitting submit1 or submit2
My name is John and I am from UK, and I am
Here,
How can I keep the the dropdown selection also with the output text area
Will I able to show only the output_textarea content after second form submission without keeping the first form data ?
PHP FIDDLE
You have an error in logic in the hidden input for the "graduate" element.
This is what you have at lines 53-55. Line 55 doesn't have an echo instead it has an $graduation = $_POST['graduation']; which won't help you:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />
instead of that, this code should work:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { echo htmlentities($_POST['graduation']); }?>" />

Array not carrying data forward

I have a form with 10 similar rows of data required. The form collects product codes, descriptions and quantities. I loop through 10 rows and use arrays to collect the information.
$code = array();
$description = array();
$quantity = array();
<?php
for($i=0; $i<10; $i++){
?>
<div class="quote-row">
<div class="quote-id">
<?php echo $i+1; ?>
</div>
<div class="quote-code">
<input type="text" class="quotecode" name="<?php echo $code[$i]; ?>" />
</div>
<div class="quote-description">
<input type="text" class="quotedescription" name="<?php echo $description[$i]; ?>" />
</div>
<div class="quote-quantity">
<input type="text" class="quotequantity" name="<?php echo $quantity[$i]; ?>" />
</div>
</div>
<?php
}
?>
On the following page, I then use $_POST['code'], $_POST['description'], $_POST['quantity'] to carry the data forward and attempt to use it.
My issue is that the data doesn't appear to be arriving?
Using the for loop, will I still be able to submit the form and take all the data forward?
Hope this is as informative as possible, thanks!
You are giving value of array in name attribute. Your array is empty so your name is empty too.
Try this:
<?php
for($i=0; $i<10; $i++){
?>
<div class="quote-row">
<div class="quote-id">
<?php echo $i+1; ?>
</div>
<div class="quote-code">
<input type="text" class="quotecode" name="code[]" />
</div>
<div class="quote-description">
<input type="text" class="quotedescription" name="description[]" />
</div>
<div class="quote-quantity">
<input type="text" class="quotequantity" name="quantity[]" />
</div>
</div>
<?php
}
?>
name[ ] format automatically make your data an array.
The key to use with the $_POST array is whatever you put in the name="" attribute. Based on the code you provided the names aren't code, description, and quantity but whatever the actual codes, descriptions, and quantities of the items are. You probably want to do this instead:
$code = array();
$description = array();
$quantity = array();
<?php
for($i=0; $i<10; $i++){
?>
<div class="quote-row">
<div class="quote-id">
<?php echo $i+1; ?>
</div>
<div class="quote-code">
<input type="text" class="quotecode" name="code[]" value="<?php echo $code[$i]; ?>" />
</div>
<div class="quote-description">
<input type="text" class="quotedescription" name="description[]" value="<?php echo $description[$i]; ?>" />
</div>
<div class="quote-quantity">
<input type="text" class="quotequantity" name="quantity[]" value="<?php echo $quantity[$i]; ?>" />
</div>
</div>
<?php
}
?>
There are several places where code needs to be updated to work as you expect it.
The most important being that the inputs are using the wrong attributes to store the name and value.
For example, the input element needs to look something like this for each of your inputs:
<input type="text" class="quotecode" name="code[]" value="<?php echo $code[$i]; ?>" />
After, adding a submit button and the surrounding form tags you can then proceed to inspect the variables in next page using PHPs $_POST or $_GET variables.

Help with Array and Post

I have this form:
<form action="" method="post">
<label for="color">Color:</label>
<input type="text" style="width: 195px;" name="color" id="color" value="<?php echo $item; ?>">
<input value="Save" type="submit" />
</form>
And the following PHP to capture contents:
if (!empty($_POST['color']) && preg_match('/^#[a-fA-f0-9]{6}/', $_POST['color']) != 0 ) {
$thisarray[] = $_POST['color'];
SetProfileField('usercss', $USER->id, implode(',',$thisarray));
}
How can I add a new field to the Form and then add that to the array to be saved?
You can add new fields to the form if you alter the HTML code. There are many different form elements, see if there is something you need here: http://www.w3schools.com/html/html_forms.asp
You can access the value of each element with $_POST['key'], so to add it to the array you would have to write $thisarray[] = $_POST['key']. Note: Replace key with the actual name of the form element.
Whole Example:
HTML:
<form action="" method="post">
<label for="color">Color:</label>
<input type="text" style="width: 195px;" name="color" id="color" value="<?php echo $item; ?>">
<input name="the_new_element" />
<input value="Save" type="submit" />
</form>
PHP:
if (!empty($_POST['color']) && preg_match('/^#[a-fA-f0-9]{6}/', $_POST['color']) != 0) {
$thisarray[] = $_POST['color'];
$thisarray[] = $_POST['the_new_element'];
SetProfileField('usercss', $USER->id, implode(',',$thisarray));
}
I named the field the_new_element, change this to whatever you want. Of course you should also sanitzie the contents.

Categories