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.
Related
I would like to store an user input from a textbox into an external php array by clicking a button. Also, there is a dependency from two radio buttons.
This is my current attempt:
At first, I include the external php array:
<?php
include ('../array.php');
?>
The input form:
<form action="" method="post">
<input type="text" name="textfield" id="txt1" value="">
<input type="radio" name="name" id="id" value="Type1">Type1<br>
<input type="radio" name="name" id="id" value="Type2">Type2<br>
<button id="submit" name="send" type="submit">Save</button>
</from>
Insert the value into the array:
<?php
if (isset($_POST['send'])){
if (isset ($_POST['name'])){
if ($_POST['name']=='Type1'){
$newword = $_POST['textfield'];
$array[] = $newword;
}
}
}
?>
But the values don't "stack", meaning that the array isn't growing with each button click.
Can anyone help please? :D
Thanks in advance!
If you want to stack values, you need to store them somewhere to keep values between each requests to server.
Values can be stored in session (for current user only) or in any database supported by PHP.
Here an example with session :
index.php :
<?php
include ('./array.php');
?>
<form action="" method="post">
<input type="text" name="textfield" value="">
<input type="radio" name="name" value="Type1">Type1<br>
<input type="radio" name="name" value="Type2">Type2<br>
<button id="submit" name="send" type="submit">Save</button>
</from>
array.php :
<?php
session_start();
if (!is_array($_SESSION['persistentValues'])) {
$_SESSION['persistentValues'] = array();
}
if (isset($_POST['send']) && isset($_POST['name']) && $_POST['name']=='Type1') {
$_SESSION['persistentValues'][] = $_POST['textfield'];
}
print_r($_SESSION['persistentValues']);
?>
<form action="" method="post">
<input type="text" name="textfield" id="txt1" value="">
<input type="radio" name="name" id="id" value="Type1">Type1<br>
<input type="radio" name="name" id="id" value="Type2">Type2<br>
<button id="submit" name="send" type="submit">Save</button>
</from>
in the file php put this
<?php
if (isset($_POST['send']) && $_POST['textfield'] && $_POST['name']){
if ($_POST['name']=='Type1'){
array = require('./array.php');
$newword = $_POST['textfield'];
$array[] = $newword;
}
}
?>
I have a form with 3 selections, and I want the form to replace the selected text with the name entered when submitted. So if the user enters their name as Josh, and picks 'King', I want the form to update itself and instead of having King it would say Josh which would no longer be a choice, and then have the other two characters below for someone else to pick from. This is a PHP document, but the data is in a HMTL form of course.
I was reading something about hidden forms, but was not sure how to implement them here. Does anyone have any suggestions?
<form method="get" action="">
Your Name: <input type="text" name="name" />
<br/>
<input type="radio" name="character" value="King"> King<br>
<input type="radio" name="character" value="Queen"> Queen<br>
<input type="radio" name="character" value="Prince"> Prince <br>
<input type="submit" value="Submit" />
Are you looking to do something like this?
You can do it by using php conditions and hidden fields.
Save the file with the name "form.php" then run it and tell me if it does the trick ;)
<form method="post" action="form.php">
Your Name: <input type="text" name="name" />
<br/>
<?php if((isset ($_POST['name-1']) || isset($_POST['name'])) && (isset ($_POST['character-1']) && $_POST['character-1']=='King'))
{
$name=isset($_POST['name-1'])?$_POST['name-1']:$_POST['name'];
echo $name.'<br><input type="hidden" name="name-1" value="'.$name.'"/><input type="hidden" name="character-1" value="'. $_POST['character-1'].'"/>';
}
else {?>
<input type="radio" name="character-1" value="King"> King<br>
<?php } ?>
<?php if((isset ($_POST['name-2']) || isset($_POST['name'])) && (isset ($_POST['character-2']) && $_POST['character-2']=='Queen'))
{
$name=isset($_POST['name-2'])?$_POST['name-2']:$_POST['name'];
echo $name.'<br><input type="hidden" name="name-2" value="'.$name.'"/><input type="hidden" name="character-2" value="'. $_POST['character-2'].'"/>';
}
else {?>
<input type="radio" name="character-2" value="Queen"> Queen<br>
<?php } ?>
<?php if((isset ($_POST['name-3']) || isset($_POST['name'])) && (isset ($_POST['character-3']) && $_POST['character-3']=='Prince'))
{
$name=isset($_POST['name-3'])?$_POST['name-3']:$_POST['name'];
echo $name.'<br><input type="hidden" name="name-3" value="'.$name.'"/><input type="hidden" name="character-3" value="'. $_POST['character-3'].'"/>';
}
else {?>
<input type="radio" name="character-3" value="Prince"> Prince <br>
<?php } ?>
<input type="submit" value="Submit" />
</form>
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
I am having some difficulty with inserting dynamic input fields into a new form html page. Essentially, I would like to get these dynamically created inputs into a new form field when the submit button is clicked on the previous page where the table dimensions are specified. Currently, when I view the page source, the input fields are outside of the html body. I would prefer not to have to use JavaScript just the keep things simple
//page where dimensions are specified e.g dimensions.html. clicking submit should bring you to a new page and display specified inputfields inside the html form.
<form action="script.php" method="post">
<label for="rows">Number of Rows (Min 1, Max 10)</label>
<input type="number" class="form-control" min="1" max="10" value="" name="rows" required>
<label for="columns">Number of Columns (Min 3, Max 10)</label>
<input type="number" class="form-control" min="3" max="10" value="" name="columns" required>
<input type="submit" name="submit2" value="Submit"/>
<input type="hidden" name="method" value="post" />
</form>
// loop for creating input fields from previous form input specifying table dimensions from seperate .php file
$row = (isset($_POST['rows']) ? $_POST['rows'] : null);
$col = (isset($_POST['columns']) ? $_POST['columns'] : null);
$x = $row * $col;
for ($i = 1; $i <= $x; $i++) {
echo "$i<input type='text' name='content[]' required>", "<br>";
}
// new html page form page e.g. content.html
<form action="script.php" method="post">
<input type="text" name="content[]">
// I would like to have the new fields input generated inside here
<input type="submit" name="submit3" value="Submit"/>
<input type="hidden" name="method" value="post"/>
</form>
You can do something like this,
page1.php
<form action="page2.php" method="post">
<label for="rows">Number of Rows (Min 1, Max 10)</label>
<input type="number" class="form-control" min="1" max="10" value="" name="rows" required>
<label for="columns">Number of Columns (Min 3, Max 10)</label>
<input type="number" class="form-control" min="3" max="10" value="" name="columns" required>
<input type="submit" name="submit2" value="Submit"/>
<input type="hidden" name="method" value="post" />
</form>
page2.php
<?php
if(isset($_POST['submit2'])){
$row = $_POST['rows'];
$col = $_POST['columns'];
$x = $row * $col;
}
?>
<form action="script.php" method="post">
<input type="text" name="content[]">
<?php
if(isset($x)){
for ($i = 1; $i <= $x; $i++) {
echo "<input type='text' name='content[]' required><br>";
}
}
?>
<input type="submit" name="submit3" value="Submit"/>
<input type="hidden" name="method" value="post"/>
</form>
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']); }?>" />