php get label text of multiple checked checkboxes - php

This is an extended question from: Get $_POST from multiple checkboxes
I am trying to get the labels of each checked checkbox, then display it as such:
"Sales | Engineering" (without the quotes)
Or if only one checkbox was selected:
"Sales"
HTML (myform.html):
<!DOCTYPE html>
<html>
<head></head>
<body>
<form name='myform' method='post' action='result.php'>
<label for='sales'>Sales</label>
<input type='checkbox' name='loc_chks[]' id='sales' value='Sales' /><br/>
<label for='insideSales'>Inside Sales</label>
<input type='checkbox' name='loc_chks[]' id='insideSales' value='Inside Sales' /><br/>
<label for='engineering'>Engineering</label>
<input type='checkbox' name='loc_chks[]' id='engineering' value='Engineering' /><br/>
<label for='fieldService'>Field Service</label>
<input type='checkbox' name='loc_chks[]' id='fieldService' value='Field Service' /><br/>
<label for='production'>Production</label>
<input type='checkbox' name='loc_chks[]' id='production' value='Production' /><br/>
<label for='warehouse'>Warehouse</label>
<input type='checkbox' name='loc_chks[]' id='warehouse' value='Warehouse'/><br/>
<br/>
<input type='submit' id='subBtn' value='Submit'/>
</form>
</body>
</html>
PHP (result.php):
<!DOCTYPE html>
<html>
<head></head>
<body>
<p><?php
if(!empty($_POST["loc_chks"]) and is_array($_POST["loc_chks"])) {
echo implode(' | ',$_POST["loc_chks"]);
} else { echo "we have a loser";}
?>
</p>
</body>
</html>
*Edited - the "if" statement in the php file always returns false even if multiple checkboxes are checked

You should use array type naming loc_chks[], this also lets you use specific keys for each item, for example: loc_chks[sales] and with PHP you have it in $_POST["loc_chks"]["sales"]
HTML (myform.html):
<label for='sales'>Sales</label>
<input type='checkbox' name='loc_chks[]' id='sales' value='Sales' /><br/>
<label for='insideSales'>Inside Sales</label>
<input type='checkbox' name='loc_chks[]' id='insideSales' value='Inside Sales' /><br/>
<label for='engineering'>Engineering</label>
<input type='checkbox' name='loc_chks[]' id='engineering' value='Engineering' /><br/>
<label for='fieldService'>Field Service</label>
<input type='checkbox' name='loc_chks[]' id='fieldService' value='Field Service' /><br/>
<label for='production'>Production</label>
<input type='checkbox' name='loc_chks[]' id='production' value='Production' /><br/>
<label for='warehouse'>Warehouse</label>
<input type='checkbox' name='loc_chks[]' id='warehouse' value='Warehouse' /><br/>
In PHP you can use implode funciton, which lets you concatenate array items with the glue string (as first parameter)
PHP (result.php):
<?php
session_start();
if(!empty($_POST["loc_chks"]) and is_array($_POST["loc_chks"])) {
$loc = implode(' | ',$_POST["loc_chks"]);
}
?>

Value is missing... use following
<label for='sales'>Sales</label>
<input type='checkbox' name='loc_chks[]' id='sales' value='Sales' /><br/>
<label for='insideSales'>Inside Sales</label>
<input type='checkbox' name='loc_chks[]' id='insideSales' value='Inside Sales' /><br/>
<label for='engineering'>Engineering</label>
<input type='checkbox' name='loc_chks[]' id='engineering' value='Engineering' /><br/>
<label for='fieldService'>Field Service</label>
<input type='checkbox' name='loc_chks[]' id='fieldService' value='Field Service' /><br/>
<label for='production'>Production</label>
<input type='checkbox' name='loc_chks[]' id='production' value='Production' /><br/>
<label for='warehouse'>Warehouse</label>
<input type='checkbox' name='loc_chks[]' id='warehouse' value='Warehouse'/><br/>

First of all assign those labels as values of checkboxes and secondly name the checkboxes like an array. Without naming all your checkboxes like that, you won't get multiple values like you are expecting in your loop.
<label for='sales'>Sales</label>
<input type='checkbox' name='loc_chks[]' id='sales' value="Sales"/><br/>
^^ ^

Nevermind - Im as dumb#$$. I didn't close out the "name" property in the form. Everything works - Thank you all for your input. George PHP has a slightly better solution.

Related

Have a time seperator : within a input field

Is it possible to have a : separator in a form input field for a manual time input? Something like the following example:
<p>
<div class='field'>
<label for='$time_in'>Time In</label>
<input type='text' name='time_in' id='time_in'
size='10' maxlength='5' /></div>
</p>
Using "Internet explorer"
You can take 2 seperate fields like
<p>
<div class='field'>
<label for='$time_in'>Time In</label>
<input type='text' name='time_in1' id='time_in1'
size='10' maxlength='2' />
:
<input type='text' name='time_in2' id='time_in2'
size='10' maxlength='2' />
</div>
</p>
and handle them separately in the code, it will work!

Using content from different sources on a page

I have a page with mixed sources of content. The top half of it is hard coded html with values echo'd from a db like so:
Part Number: <input type="text" name="pn" size="25" id="1" value="<?php
echo "$STH->pn"; ?>"/>
Part Nomenclature: <input type="text" id="partnomen" name="part_nomenclature"
size="35" value="<?php echo "$STH->part_nomenclature"; ?>"/>
Quantity:<input type="text" name="qty" size="3" value="<?php echo "$STH->qty";
?>"/><br />
Serial Number <input type="text" name="sn" size="25" value="<?php echo "$STH->sn";
?>"/>
ATA Code: <input type="text" name="ata" size="12" value="<?php echo "$STH->ata"; ?>"/>
Control Order: xxxxxx
Engine Model: xxxxxxxx<br />
Engine Serial Number: xxxxxxxx
Removed From A/C Serial# <input type="text" name="acsn" value="<?php echo
"$STH->acsn"; ?>"/>
TT / TC: ___________________-->
<input type="hidden" name="db_date" value="<?php echo "$STH->db_date"; ?>"/>
<h3 id="addinfo">Reason For Workorder: </h3>
The middle contains html and values that are both echo'd onto the page:
echo "
<div id='addlinfo'>
<input type='hidden' name='wo_id' value='$wo_id'/>
Sequence<input type='text' name='sel_id[]' size='1' value='$i'/>
Repair ID:<input type='text' name='repair_id[]' size='1'
value='$STH->repair_id'readonly/>
Part Nomenclature: <input type='text' name='' size='35'
value='$STH->part_nomenclature'readonly/>
Repair Name:<input type='text' name='repair_name[]' size='20'
value='$STH->repair_name' readonly/><br />
Location: <input type='text' name='location[]' size='20' value='$STH->location'
readonly/>
Description:<br /> <textarea id='' rows='5' cols='100' name='description[]'
id='text'>$STH->description</textarea>
</div>";
The bottom half is pulled in from a separate php page using a jquery ajax call like so:
$(document).ready(function(){
$('#button1').click(function(){
$.ajax({
url: 'http://localhost/php_site_copy/process.php',
type: 'POST',
data: {part_name: $('#partnomen').val()},
success: function(msg){
$('#result').html(msg);
}
}); //event handler
});
});
<div id="result"></div><br />
<button id="button1">Add Repairs</button>
The goal here is to collect all of the data displayed into a $_POST from the page but I'm finding that I only get part of the array. The bottom part form elements are not picked up in the post at all and only some of the upper form elements.
You would think that this would work but I'm beginning to think I'm traveling down the wrong road when it comes to mixing content from different sources. Is there a standard that I'm missing or should I just put my nose back to the grindstone?
Edit: to answer quids' question, I'm using Firefox and the form arrangement is like so:
<form action='test_page.php' method='post'/>
<top html inputs/>
<middle inputs/>
<bottom inputs/>
<input type='submit' value='submit'/>
</form>

One form and two actions and two submit in php

Wokring on a project where the user have the ability to chose from date and to date, and then chose one of the radio buttoms. After that chose pdf or excel to generate the to preferred format.
The problem is the form, I want it to action generateExcel.php if excel is pressed and generatePdf.php if PDF is pressed. This is how far I have came and not working yet:
<form action='generatePdf.php' method='Post'/>
Fra Dato: <input type="text" name="fraDato" value="<?php echo date('d-m-Y'); ?>" />
Til Dato: <input type="text" name="tilDato" value="<?php echo date('d-m-Y'); ?>"> <br>
<input type="radio" name="hent" value="timesmaling">Times malinger<br>
<input type="radio" name="hent" value="tredjetimesmaling">Tredje times malinger <br>
<input type="radio" name="hent" value="oppgaver">Oppgaver <br>
<input type="radio" name="hent" value="dagvakt">Dagvakt <br>
<input type="radio" name="hent" value="kveldsvakt">Kveldsvakt <br>
<input type="radio" name="hent" value="kontrollcm">Kontroll CM <br>
<input type='submit' name='pdf' value='PDF'>
<form action='generateExcel.php' method='Post'/>
<input type='submit' name='excel' value='excel'>
</form>
It is possible to override the action attribute of the parent form using the HTML5 formaction attribute on a button. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button
<input type='submit' name='pdf' value='PDF' formaction='generatePdf.php'>
<input type='submit' name='excel' value='excel' formaction='generateExcel.php'>
The browser support looks pretty good: http://www.wufoo.com/html5/attributes/13-formaction.html
However, webeno's answer would definitely work in all browsers and it can be easier to manage all your form processing code in one file.
I'd recommend you to put both of your scripts on the same file and validate against the button that has been clicked.
EDIT: If your 2 files are too big (or you would like to keep them separate for any other reason), you could still use include (or require - more info on the differences: Difference between "include" and "require" in php).
original file:
<form action='generate.php' method='Post'> <!-- removed the slash from the end here -->
Fra Dato: <input type="text" name="fraDato" value="<?php echo date('d-m-Y'); ?>" />
Til Dato: <input type="text" name="tilDato" value="<?php echo date('d-m-Y'); ?>"> <br>
<input type="radio" name="hent" value="timesmaling">Times malinger<br>
<input type="radio" name="hent" value="tredjetimesmaling">Tredje times malinger <br>
<input type="radio" name="hent" value="oppgaver">Oppgaver <br>
<input type="radio" name="hent" value="dagvakt">Dagvakt <br>
<input type="radio" name="hent" value="kveldsvakt">Kveldsvakt <br>
<input type="radio" name="hent" value="kontrollcm">Kontroll CM <br>
<input type='submit' name='pdf' value='PDF'>
<input type='submit' name='excel' value='excel'>
</form>
generate.php:
if (isset($_POST['pdf'])) {
include('generatePdf.php');
}
if (isset($_POST['excel'])) {
include('generateExcel.php');
}
EDITED
Alternatively you could just use redirect on that separate file (generate.php - make sure there is nothing else on this page):
if (isset($_POST['pdf'])) {
header('Location: generatePdf.php');
}
if (isset($_POST['excel'])) {
header('Location: generateExcel.php');
}

how to get input box values which is called by ajax?

<form action="chngschdl.php" method="post">
<fieldset>
<label class="home">Flight Name</label> <select name="select_catalog_query" ><?php echo $options1; ?></select>
<br/><br/>
<label class="home">Starting Route</label> <input class="text" type="text" name="stroute" onKeyUp="numericFilter(this);" /> <label class="home">Deperture Time</label> <input class="text" type="text" name="stdrt" /><br/>
<label class="home">Ending Route</label> <input class="text" type="text" name="enroute" onKeyUp="numericFilter(this);" /> <label class="home">Arrival Time</label> <input class="text" type="text" name="enart" /><br/>
<label class="home">Break Route Number</label> <input class="text" type="number" name="bpn" maxlength='1' onkeyup="Bpoint(this.value)" /><br/>
<label id='bp' > <?php //$i=$_SESSION['point'];$_SESSION['bp']=$broute[$i];?></label>
<button class="btn">Go</button>
</fieldset>
</form>
ajax getting data from:
<?php
session_start();
if( $_SESSION['type']!='admin')
{
header("Location: index.php");
return;
}
$point=$_GET['q'];
$_SESSION['point']=$point;
$bpoption="";
for($i=0;$i<$point;$i++)
{
echo "<label>Break Route[$i]<label> <input class=text type=text name=broute[$i] /> ";
echo "<label>Arrival Time[$i]<label> <input class=text type=text name=bart[$i] /> ";
echo "<label>Departure Time[$i]<label> <input class=text type=text name=bdrt[$i] /> ";
echo "<br/><br/>";
}
?>
i have to collect every data of Break Route, Arrival time and Departure time... how i suppose to get this data and use it in another page?? is there is other way around by not using ajax... bt it must be remembered that Break Route is not predefined...
since your form fields are named like "name[$i]" it doesn't matter how many ajax-populated fields there are.
All you have to do on the page processing the form submit is:
foreach ($_POST["name"] as $name) {
// do things
}

Multiple post data to Mysql DB

I have problem with: I want to create admin for add questions to quiz system. Structure is:
<label>Question 1</label>
<input type='text' name='question' value=''/>
<label>Possible reply</label>
<input type='text' name='1' />
<input type='text' name='2' />
...
<input type='text' name='6' />
<label>**Correct reply</label>
<input type='text' name='correct' />
<label>Question 2 </label>
<input type='text' name='question' value=''/>
<label>Possible reply </label>
<input type='text' name='1' />
<input type='text' name='2' />
...
<input type='text' name='6' />
<label>Correct reply </label>
<input type='text' name='correct' />
<label>Question 3 </label>
...
<input type='submit' name='submit' value='submit'>
And I need multiple questions post to Mysql db tables: question, 1,2,3,4,5,6, correct.
I was create this:
<?php
if(isset($_POST['submit']))
{
$question $_POST['question '];
$a = $_POST['1'];
$b = $_POST['2'];
$c = $_POST['3'];
$d = $_POST['4'];
$e = $_POST['5'];
$f = $_POST['6'];
correct = $_POST['correct '];
$result=mysql_query("insert into test (question, 1, 2, 3, 4, 5, 6, correct) values ('$result', '$a', '$b', '$c', '$d', '$e', '$f', '$correct' )");
}
else
{
?>
<label>Question 1 </label>
<input type='text' name='question' value=''/>
<label>Possible reply </label>
<input type='text' name='1' />
<input type='text' name='2' />
...
<input type='text' name='6' />
<label>Correct reply </label>
<input type='text' name='correct' />
<label>Question 2 </label>
<input type='text' name='question' value=''/>
<label>Possible reply </label>
<input type='text' name='1' />
<input type='text' name='2' />
...
<input type='text' name='6' />
<label>Correct reply </label>
<input type='text' name='correct' />
<label>Question 3 </label>
...
<input type='submit' name='submit' value='submit'>
<?
}
But this send only 1 question to DB.
If you can't use
<input type='text' name='question_1' value=''/>
<input type='text' name='question_1' value=''/>
<input type='text' name='question_1' value=''/>
Write in HTML:
<input type='text' name='question[]' value=''/>
<input type='text' name='reply1[]' />
<input type='text' name='reply2[]' />
<input type='text' name='reply3[]' />
<input type='text' name='reply4[]' />
<input type='text' name='reply5[]' />
<input type='text' name='reply6[]' />
<input type='text' name='correct[]' />
In PHP:
<?php
if (isset($_POST['submit'])){
$questions=$_POST['question'];
$reply1=$_POST['reply1'];
$reply2=$_POST['reply2'];
$reply3=$_POST['reply3'];
$reply4=$_POST['reply4'];
$reply5=$_POST['reply5'];
$reply6=$_POST['reply6'];
$correct=$_POST['correct'];
foreach($questions as $key=>$value){
$result=mysql_query("insert into test (question, 1, 2, 3, 4, 5, 6, correct) values ('$value', '".$reply1[$key]."', '".$reply2[$key]."', '".$reply3[$key]."', '".$reply4[$key]."', '".$reply5[$key]."', '".$reply5[$key]."', '".$correct[$key]."' )");
}
}
It would be nice if you can state your database structure and expected result, because it may not work the way you think and we'll never know until we see it.
If you want 1 answer per row, you need to use a multiple row insertion - How to insert multiple rows in single insert statement?.
try that:
<input type='text' name='question1' value=''/>
<input type='text' name='question2' value=''/>
instead of
<input type='text' name='question1' value=''/>
<input type='text' name='question1' value=''/>

Categories