How can I echo out content of if statement - php

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;
}

Related

Generate multiple Form Inputs 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=""/>
}

Activities related to services display properly

I want to store related activities of services
<form method='post' id='userform' action='arrayvalue.php'>
<tr>
<td>Trouble Type</td>
<br>
<td>
<input type='checkbox' name='servicevar[]' value='tds'>tds<br> <br>
<input type='checkbox' name='activityvar[]' value='One'>Return<br>
<input type='checkbox' name='activityvar[]' value='Two'>Filling<br>
<br>
<input type='checkbox' name='servicevar[]' value='Gst'>Gst<br> <br>
<td>
<input type='checkbox' name='activityvar[]' value='One'>Return<br>
<input type='checkbox' name='activityvar[]' value='Two'>Filling<br>
<br>
</td>
</tr>
</table> <input type='submit' name="submit" class='buttons'>
</form>
<?php if(isset($_POST[submit]) {
$activity = $_POST['activityvar'];
$service = $_POST['servicevar'];
foreach ($service as $key => $value) {
echo ($value);
echo "<br>";
foreach ($activity as $key => $value) {
echo ($value);
echo "<br>";
}
}
}
MY OUTPUT:
tds
one
two
one
two
Gst
one
two
one
two
Expected Output:
tds
one
two
Gst
one
two
Try to use One foreach loop with index's like :
if(isset($_POST[submit]) {
$activity = $_POST['activityvar'];
$service = $_POST['servicevar'];
$index = 0;
foreach ($service as $key => $value) {
echo $value;
echo $activity[$index]; $index++;
echo $activity[$index]; $index++;
}
}
EDIT : To store the data in your database, use the same concept like :
if(isset($_POST[submit]) {
$activity = $_POST['activityvar'];
$service = $_POST['servicevar'];
$index = 0;
$nbr_of_related_activities = 2;
foreach ($service as $key => $value) {
$servicevalue = $value;
for ($i=0; $i<$nbr_of_related_activities;$i++) {
$activityvalue = $activity[$index+$i];
$query = "insert into serviceacitivitymap(service_id,activity_id) values('$servicevalue','$activityvalue')";
$insert_row = $conn->query($query) or die ($conn->error.__LINE__);
}
$index += $nbr_of_related_activities;
}
}

Getting checked checkbox values in variable PHP

I have added two checkboxes on page with submit button. After submitting there is a method having foreach loop to check which checkbox is checked , and that checkbox value should get in variable. If both checkbox's are checked then it should append both values in variable. But in my case the values are repeating. And also if i check both checkboxes it is picking up only second checkbox value.
Below is my code for HTML :
<form name="" action="" method="post">
<input type="checkbox" name="checkbox[]" value="Training"/> Training <br/><br/>
<input type="checkbox" name="checkbox[]" value="Posting"/> Posting<br/><br/>
<input type="submit" value="Submit"/>
</form>
Code for PHP:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$value = "";
$name = $_POST['checkbox'];
// optional
// echo "You chose the following color(s): <br>";
foreach ($name as $value){
//echo $value.", ";
$value .= $value;
}
echo $value;
//print "<script>alert('".$value."')</script>";
}
?>
$value is a local variable to the loop it get overwritten when the loop continues again.
$result = '';
foreach ($name as $value){
$result .= $value;
}
echo $result;
$name = (array)$_POST['checkbox'];
$value = '';
foreach ($name as $val){
$value .= $val;
}

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;

How to print_r $_POST array?

I have following table.
<form method="post" action="test.php">
<input name="id[]" type="text" value="ID1" />
<input name="value[]" type="text" value="Value1" />
<hr />
<input name="id[]" type="text" value="ID2" />
<input name="value[]" type="text" value="Value2" />
<hr />
<input name="id[]" type="text" value="ID3" />
<input name="value[]" type="text" value="Value3" />
<hr />
<input name="id[]" type="text" value="ID4" />
<input name="value[]" type="text" value="Value4" />
<hr />
<input type="submit" />
</form>
And test.php file
<?php
$myarray = array( $_POST);
foreach ($myarray as $key => $value)
{
echo "<p>".$key."</p>";
echo "<p>".$value."</p>";
echo "<hr />";
}
?>
But it is only returning this: <p>0</p><p>Array</p><hr />
What I'm doing wrong?
The foreach loops work just fine, but you can also simply
print_r($_POST);
Or for pretty printing in a browser:
echo "<pre>";
print_r($_POST);
echo "</pre>";
<?php
foreach ($_POST as $key => $value) {
echo '<p>'.$key.'</p>';
foreach($value as $k => $v)
{
echo '<p>'.$k.'</p>';
echo '<p>'.$v.'</p>';
echo '<hr />';
}
}
?>
this will work, your first solution is trying to print array, because your value is an array.
$_POST is already an array, so you don't need to wrap array() around it.
Try this instead:
<?php
for ($i=0;$i<count($_POST['id']);$i++) {
echo "<p>".$_POST['id'][$i]."</p>";
echo "<p>".$_POST['value'][$i]."</p>";
echo "<hr />";
}
?>
NOTE: This works because your id and value arrays are symmetrical. If they had different numbers of elements then you'd need to take a different approach.
You are adding the $_POST array as the first element to $myarray. If you wish to reference it, just do:
$myarray = $_POST;
However, this is probably not necessary, as you can just call it via $_POST in your script.
Why are you wrapping the $_POST array in an array?
You can access your "id" and "value" arrays using the following
// assuming the appropriate isset() checks for $_POST['id'] and $_POST['value']
$ids = $_POST['id'];
$values = $_POST['value'];
foreach ($ids as $idx => $id) {
// ...
}
foreach ($values as $idx => $value) {
// ...
}
Just:
foreach ( $_POST as $key => $value) {
echo "<p>".$key."</p>";
echo "<p>".$value."</p>";
echo "<hr />";
}
Because you have nested arrays, then I actually recommend a recursive approach:
function recurse_into_array( $in, $tabs = "" )
{
foreach( $in as $key => $item )
{
echo $tabs . $key . ' => ';
if( is_array( $item ) )
{
recurse_into_array( $item, $tabs . "\t" );
}
else
{
echo $tabs . "\t" . $key;
}
}
}
recurse_into_array( $_POST );
Came across this 'implode' recently.
May be useful to output arrays. http://in2.php.net/implode
echo 'Variables: ' . implode( ', ', $_POST);
$_POST is an array in itsself you don't need to make an array out of it. What you did is nest the $_POST array inside a new array. This is why you print Array.
Change it to:
foreach ($_POST as $key => $value) {
echo "<p>".$key."</p>";
echo "<p>".$value."</p>";
echo "<hr />";
}
$_POST is already an array. Try this:
foreach ($_POST as $key => $value) {
echo "<p>".$key."</p>";
echo "<p>".$value."</p>";
echo "<hr />";
}
As you need to see the result for testing purpose. The simple and elegant solution is the below code.
echo "<pre>";
print_r($_POST);
echo "</pre>";

Categories