Generate multiple Form Inputs php - php

How do i get the values when submitted
I am generating the input via a loop based on the users selection but don't know how to retrieve the input values via post method
here is a sample of what i have
// string is based on database values it can be anything which i can't tell
Example code
$string = 'math,english,biology';
$exp = explode(',', $string);
foreach($exp as $value){
print '<input type="text" name="'.$value.'[]" value="" />
}

You don't have to use name array (name="blabla[]")
$string = 'math,english,biology';
$exp = explode(',', $string);
if ($_POST) {
foreach ($exp as $name) {
if (isset($_POST[$name])) {
echo 'input ' . $name . ' is ' . $_POST[$name] . '<br>';
}
}
exit();
}
echo '<form method="post">';
foreach($exp as $value){
print '<input type="text" name="'.$value.'" value="" />';
}
echo '<button type="submit">Submit</button></form>';
Enter a, b, c to each input and submit. Here is the result:
input math is a
input english is b
input biology is c

Put the value in value="", name the field and make it an array [].
$string = 'math,english,biology';
$exp = explode(',', $string);
foreach ($exp as $value) {
echo '<input type="text" name="fieldName[]" value="<?= htmlentities($value) ?>" />
}
Then it will be accessible in *$_POST['fieldName'] as an array.
*presuming you are using method="POST" on the form
If math,english,biology are form keys, then do:
$string = 'math,english,biology';
$exp = explode(',', $string);
foreach ($exp as $key) {
echo '<input type="text" name="fieldName[<?= htmlentities($key) ?>]" value=""/>
}
or
$string = 'math,english,biology';
$exp = explode(',', $string);
foreach ($exp as $key) {
echo '<input type="text" name="<?= htmlentities($key) ?>" value=""/>
}

Related

delete duplicate and original string php

i want to delete duplicate string and original duplicate!
for example:
my string = one two three one two
and i want = three
my code:
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<p>
<textarea name="keywords" rows="20" columns="120"></textarea>
</p>
<p>
<input type="submit" name="submit" />
</p>
</form>
<?php
if(!empty($_POST['keywords']))
{
$posted = $_POST['keywords'];
$posted = array_unique(explode(' ', str_replace("\r\n", ' ', $posted)));
echo print_r($posted, true);
}
?>
please Help me
Thanks
After you explode your string to words - count all values in array:
$posted = explode(' ', str_replace("\r\n", ' ', $posted));
$counted_values = array_count_values($posted);
// then filter by value, if value equals 1 - echo it, or do whatever you want
foreach ($counted_values as $k => $v) {
if ($v == 1) {
echo $k;
}
}
Similar to the other answer but using array_filter:
$posted = array_filter(array_count_values(str_replace("\r\n", ' ', $posted)),
function($v) { return $v === 1; });

Dynamic $row output from array

I am trying to achieve dynamic $row values from an array, and show them as output. It keeps showing nothing while the values are already there.
This is what I have so far, where:
$row["lg_".$val.""]; should return:
$lg_it
'it' is the $val from the array.
foreach($arrMapCookieToLang as $key => $val) {
$shrtKey = $row["lg_".$val.""];
<input type="text" name="lg_$val" value="$shrtKey">
}
Anyone an idea?
What you have should result in a syntax error. Try the following:
<?php
foreach($arrMapCookieToLang as $key => $val) {
$shrtKey = $row['lg_'.$val];
?>
<input type="text" name="lg_<?= $val ?>" value="<?= $shrtKey ?>">
<?php
}
You've missed to echo your input-field:
foreach($arrMapCookieToLang as $key => $val) {
$shrtKey = $row["lg_".$val.""];
echo '<input type="text" name="lg_' . $val .'" value="' . $shrtKey . '">';
}
Further, if you don't use the array key in the foreach-loop, you can omit the $key =>-part and just write
foreach($arrMapCookieToLang as $val) {
// ...
}
Instead, the
<input type="text" name="lg_$val" value="$shrtKey">
Maybe you should use the
echo "<input type=\"text\" name=\"lg_" . "$val\" value=\"$shrtKey\">";

How can I echo out content of if statement

i have 5 of these:
if(isset($_POST['checkbox'])) {
$name = "Alex";
}
How can I echo the name of every selected checkbox outside of the if statement?
From your example, build an array of $name:
if(isset($_POST['checkbox'])) {
$name[] = "Alex";
}
if(isset($_POST['checkbox1'])) {
$name[] = "Bob";
}
Then either:
echo implode(', ', $name);
Or:
foreach($name as $value) {
echo $value;
}
But actually I'm wondering why not just set the values in the form inputs (use an array):
<input type="checkbox" name="checkbox[]" value="Alex">
<input type="checkbox" name="checkbox[]" value="Bob">
Then:
foreach($_POST['checkbox'] as $value) {
echo $value;
}

Compute 1st value on multiple values on a checkbox

I was trying to make a COMPUTERIZED ORDERING SYSTEM. My problem is how can I compute the 1st value on my checkbox. The second value on the checkbox will be posted on the summary of orders.
Once I've check all those three, it will compute the total amount of the menu and to display the selected menu on the summary of orders. But I can't figure out how can I display the total amount.
Can anybody guide me how to compute the total on my 1st value on the checkbox?
<form method="post">
<input name='ts[]' type='checkbox' value='40 |Tosilog'/> Tosilog
<br>
<input name='cs[]' type='checkbox' value='40 |Chiksilog'/>Chiksilog
<br>
<input name='ps[]' type='checkbox' value='45 |Porksilog'/>Porksilog
<br>
<input type="submit" name="go" value= "Total">
</form>
<?php
//tosilog
$ts = $_POST['ts'];
$value = explode("|",$ts[0]);
echo $value[0];
echo $value[1];
//chiksilog
$cs = $_POST['cs'];
$value = explode("|",$cs[0]);
echo $value[0];
echo $value[1];
//porksilog
$ps = $_POST['ps'];
$value = explode("|",$ps[0]);
echo $value[0];
echo $value[1];
?>
<!-- compute for the 1st value on checkbox -->
<?php
$ts=$_POST['ts[0]'];
$cs=$_POST['cs[0]'];
$ps=$_POST['ps[0]'];
?>
<?php $compute = $ts[0] + $cs[0] + $ps[0];?>
<?php echo "$compute " ; ?>
If you're getting an array of checkbox elements, and they are numeric, you can use array_sum(). Not sure I understand your suggested structure, but I'll give you a code sample here based on the existing form structure. Then I'll post another bit to try to make this simpler for you. Executive summary: You do not need all the variables that are created by this form structure.
<?php // RAY_temp_user193.php
error_reporting(E_ALL);
$total = 0;
$inputs = array();
$errors = array();
if (!empty($_POST))
{
if (!empty($_POST['ts']))
{
foreach ($_POST['ts'] as $ts)
{
$inputs[] = current(explode(' |', $ts));
}
}
else
{
$errors[] = 'Tosilog';
}
if (!empty($_POST['cs']))
{
foreach ($_POST['cs'] as $cs)
{
$inputs[] = current(explode(' |', $cs));
}
}
else
{
$errors[] = 'Chiksilog';
}
if (!empty($_POST['ps']))
{
foreach ($_POST['ps'] as $ps)
{
$inputs[] = current(explode(' |', $ps));
}
}
else
{
$errors[] = 'Porksilog';
}
// IF ERRORS
if (!empty($errors))
{
echo 'UNABLE TO PRINT COMPLETE TOTAL. MISSING: ' . implode(',', $errors);
}
$total = array_sum($inputs);
if ($total) echo "<br/>TOTAL: $total <br/>" . PHP_EOL;
// END OF THE ACTION SCRIPT
}
// CREATE THE FORM
$form = <<<ENDFORM
<form method="post">
<input name='ts[]' type='checkbox' value='40 |Tosilog'/> Tosilog
<br>
<input name='cs[]' type='checkbox' value='40 |Chiksilog'/>Chiksilog
<br>
<input name='ps[]' type='checkbox' value='45 |Porksilog'/>Porksilog
<br>
<input type="submit" value= "Total">
</form>
ENDFORM;
echo $form;
See http://www.laprbass.com/RAY_temp_miggy.php
This is probably more along the lines of the way I would do it. The script knows what goes into the HTML and it knows exactly what to expect in the POST request. It is easy to add or remove different inputs. Often these kinds of inputs come from a data base table.
<?php // RAY_temp_miggy.php
error_reporting(E_ALL);
$total = 0;
// EXPECTED INPUTS
$inputs = array
( 'Tosilog' => 40
, 'Chiksilog' => 40
, 'Porksilog' => 45
)
;
if (!empty($_POST))
{
// ACTIVATE THIS TO SEE WHAT WAS SUBMITTED
// var_dump($_POST);
// SUM OF THE ELEMENTS
$total = array_sum($_POST);
echo "TOTAL: $total";
// SUM OF THE EXPECTED INPUTS
$expect = array_sum($inputs);
if ($total != $expect) echo " BUT THERE MAY BE INCOMPLETE INPUTS!";
// END OF THE ACTION SCRIPT
}
// CREATE THE FORM
$checkboxes = NULL;
foreach ($inputs as $name => $value)
{
$checkboxes
.= '<input name="'
. $name
. '" type="checkbox" value="'
. $value
. '" />'
. $name
. '<br/>'
. PHP_EOL
;
}
$form = <<<ENDFORM
<form method="post">
$checkboxes
<input type="submit" value= "Total">
</form>
ENDFORM;
echo $form;

Get POST variable that is an array

All,
I have the following hidden variables:
<input type="hidden" name="chk[10]" value = "cats">
<input type="hidden" name="chk[13]" value = "dogs">
<input type="hidden" name="chk[14]" value = "fish">
I want to get these variables through POST and print them. How can I do this in PHP?
Thanks
foreach($_POST['chk'] as $key => $value) {
echo $key, ' => ', $value, '<br />';
}
$chks = $_POST["chk"];
print_r($chks);

Categories