Below is my code
if (!empty($_POST['ok'])) {
$errorMessage = array();
$loopcount = 0;
$i = 0;
foreach ($_POST['theDate'] AS $i => $theDate) {
if ($_POST['EW'][$i] == 'EW') {
$ew = "yes";
} else {
$ew = "no";
}
$i = $i + 1;
echo $ew;
}
}
its pulls the checkbox value of below and assigns it yes or now if value == ew
E/W<input name="EW[]" ID="EW[]" value="EW" type="checkbox" />
the issue is if check these
row checked
1 no
2 yes
3 no
3 yes
the out result when submitted is
row checked
1 yes
2 yes
3 no
4 no
It seems to stick anything checked as ew to the top and I don't get why here is a live working example that when submit is clicked echos with output.... all fields need to be filled but is u just add a number i will work
http://runningprofiles.com/tests/addbet.php
The checkbox is not submitted, if it does not have a value, thus, the 'yes' are the only ones in the loop. the index numbers for that field only from the submitted ones.
edit - clearification: if not checked - it its value does not get submitted.
foreach ($_POST['theDate'] AS $i => $theDate)
You missing the { at the end
like this
foreach ($_POST['theDate'] AS $i => $theDate){
Related
My problem is that the below code changes the value of the checked ones, but the index i is assigned to the checked ones only. I want it to pass through all check boxes. For examples if the first and last check boxes were checked, I am getting job[0] is "yes" and job[1] is "yes" and the rest are "no" while I should get "yes" for job[0] and job[4].
Code below:
if(!empty($_POST['job'])){
$i = 0;
$jobArray = array("no","no","no","no","no");
// Loop to store and display values of individual checked checkbox.
foreach($_POST['job'] as $selected){
$jobArray[$i] = "yes";
$i++;
}
} //$_POST['job']
The thing is that checkboxes values exist in your request only if they are checked. You should keep it in mind.
So, you can change your application logic to work with that. But if you really need to have an array with 'yes', 'no' values, just create it.
Let's say you create your checkboxes like so:
<?php for ($i = 0; $i < 10; $i++): ?>
<input type="checkbox" name="job[<?= $i ?>]" value="yes">
<?php endfor; ?>
Then, in your code:
$jobs = [];
for ($i = 0; $i < 10; $i++) {
$jobs[$i] = $_POST['job'][$i] ?? 'no';
}
I'm working on a project. My codes are simply written below
HTML
<input type="checkbox" name="A" value="A">A
<input type="checkbox" name="B" value="B">B
<input type="checkbox" name="C" value="C">C
<input type="submit" name="submit" value="Submit">
PHP
<?php
$price=0;
if(isset($_POST["submit"])){
//the code goes here
}
?>
If only one of the option is chosen, it's free (no price). But, if the user chooses more than one, the $price is +10 in every option. So, it can be illustrated like this
Choose 1 = free
Choose 2 = +10
Choose 3 = +20
I have no idea with my PHP and the line //the code goes here is still empty. Any idea?
Homework, huh? I didn't even have to go to college for that one.
<?php
if( isset($_POST) )
{
$count = 0;
$arr = [
array_key_exists('A', $_POST),
array_key_exists('B', $_POST),
array_key_exists('C', $_POST)
];
for( $i = 0; $i < 3; $i++ ) {
if( $arr[$i] ) $count++;
}
// Now count the total - 1 and * it by 10
if( $count > 1 ) $total = ($count - 1) * 10;
}
Basically this script will check for the key in the $_POST array, if you do not select a checkbox it would not create the key for _POST array and thus it would be false.
If it's false then just skip by default. However, if it's true, it will increment to the $count variable. Then if $count is greater than one than we add create a new variable of $total = $count - 1. That will remove one value from $count then times it by 10.
I have a checkbox table declared like this:
for ($x = 0; $x < 6; $x++) {
echo "<tr>";
echo "<td>"; echo $days[$x]; echo '</td>'; //displays days
echo '<td><input type="checkbox" name="check_list[]" onClick="toggle(this, '.$x.')" value="1"/> All';
echo '<input type="hidden" name="check_list[]" onClick="toggle(this, '.$x.')" value="0"/></td>'; //creates check all buttons
for ($y = 0; $y < 12; $y++){
echo '<td><input type="checkbox" name="'.$x.'" value="1"> Bar 1<br/></td>'; //creates the other buttons
}
echo "</tr>";
}
The name="check_list[]" checkbox selects all the checkboxes in the same row when checked. It is done with this script:
<script language="JavaScript">
function toggle(source, id) {
checkboxes = document.getElementsByName(id);
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
Then the data are stored like this in the database:
<?php
$i=0; $store[20]; $check[20];
foreach($_POST['check_list'] as $value){
$store[$i] = $value; $i= $i + 1;
}
$check = str_split(preg_replace('/10/','1',implode('',$store)));
array_walk($check, 'intval');
if($check[0]) { $monday = 1; } if(!$check[0]) {$monday = 2; }
if($check[1]) { $tuesday = 1; } if(!$check[1]) {$tuesday = 2; }
if($check[2]) { $wednesday = 1; } if(!$check[2]) {$wednesday = 2; }
if($check[3]) { $thursday = 1; } if(!$check[3]) {$thursday = 2; }
if($check[4]) { $friday = 1; } if(!$check[4]) {$friday = 2; }
if($check[5]) { $saturday = 1; } if(!$check[5]) {$saturday = 2; }
?>
Obviously, this is not the whole code (I wrote the sql query to connect and insert the variables for the days. And it worked). But till now I am only taking the values of the first checkbox for each row. Which means that if I check it, all the checkboxes of that row are checked. But I am only reading the fact that all the checkboxes of that row are checked. If I only check a normal checkbox in a row, its value is not posted and I the only info stored is that not all checkboxes are checked (represented by value 2).
What I want to do is take the value of the normal checkboxes for each row instead, and concatenate them in a number. For example, for monday checkboxes (checked, unchecked, unchecked, checked) i would store $monday = 1221.
The same must be done for all days (monday to saturday)
You can do as follows:
for ($y = 0; $y < 12; $y++){
<input type="text" name="matrix['.$x.']['.$y.']" value="Text" />
}
And in your sever:
foreach($_POST['matrix'] as $key => $value) {
echo $key;// Value of $x
foreach($value as $k => $val) {
echo $k;// This would be the value of $y
echo $val;// This would be the value of the inputs
}
}
The way you describe it, it is not possible. It is not possible to put an array declaration as a variable name. However, I somewhat get (and assume) a picture of what you want to do.
After creating the input elements dynamically, also create a hidden input element that contains the "count" of how many elements where created. Then, in PHP code, first access the count element to find out how many elements are present. Then, loop through that index, to build the loop name for each element and access their values.
I appreciate the suggestions given, but I actually tried an alternative solution. Instead of referring to the elements by name or id, I refer to them by class and by name. I use the class (variable) to do the toggle function and the name (which is a one dimensional array) to get their values.
In short, this is the general idea of the code:
The checkbox declaration
echo '<td><input type="checkbox" class="'.$x.'" name="check_list[]" value="1"> <br/>';
The toggle function:
function toggle(source, id) {
checkboxes = document.getElementsByClassName(id);
for(var i=0, n=checkboxes.length;i<n;i++) {checkboxes[i].checked = source.checked;}
}
The php code to get the values:
$i=0; $unclean[180];
foreach($_POST['check_list'] as $value){
$unclean[$i] = $value; $i= $i + 1;
}
Then, knowing the dimensions the matrix had, i simply separate this one dimensional array into an array of one dimensional arrays (basically a matrix) iterating through it with a for loop
I have the following to generate a state drop down on a form:
$states = array('State', 'Alabama', 'Alaska', 'Arizona', 'Arkansas');
echo "<select name='choose_state'>\n";
foreach ($states as $key => $state)
{echo "<option value='$key'>$state</option>\n";}
echo "</select>";
How would I go about making sure a user
1) only selects one of the options in the array
2) doesn't select the default value? ([0]=> string(5) "State")
edit: validate in php, this is for a form collecting user information before posting to a db
I tried using in_array and got stuck trying to exclude the default value
I think you're missing some checks. You should never rely on what is exacly posted, and always perform thorough checking:
$chosen_state = null;
if (array_key_exists('choose_state', $_POST))
{
$choose_state = $_POST['choose_state'];
if (array_key_exists($choose_state, $states) && $choose_state > 0)
{
// Value does actually exist in array and is not item 0.
$chosen_state = $states[$chose_state]);
}
}
Following example assumes that you're storing the key provided for the select in the var $state_key...
try this:
$max = sizeof($states) - 1; // this is the number of possible values that you have, minus the default
if($state_key != 0 && $state_key > 0 && $state_key < $max)
{
// do whatever here, you've got good data at this point
}
This also assumes that your default value is always key #0 (first in the array), by the way.
Validating form submit in php:
When you submit form in php, Select input type returns selected value in post. So you can do something like:
$selectedindex = $_POST["choose_state"];
if($selectedindex == 0)
{
echo "Default item has been selected";
}
else{
echo "Other than default item has been selected ";
//you can do further validation here for selected item
//is in between 0 and 5 if you need to do so
}
Hi all
Now is 2 week that i searching for an answer to my problem and no luck. hope somone can help me on this
i have a chackbox option that i call from the database
<form style="margin-top:-30px" method="POST" action="extradata.php">
<?php
$sql = "SELECT ext_id,ext_price,ext_name,ext_description FROM tbl_extra ORDER by ext_id ASC";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
$number = mysql_num_rows($result);
$i = 0;
while ($number > $i) {
$NA= mysql_result($result,$i,"ext_name");
$PR= mysql_result($result,$i,"ext_price");
print "<input type='checkbox' name='extra[]' value='$NA'></td>";
$i++;
}
?>
What i trying to do is to pass the value to extradata.php with 2 value
1.$NA,
2.$PR
when selected box then insert to the database the value of $NA to ex_tra and $PR to ex_price
the extradata.php
<?php
require_once 'library/config.php';
$id=$_POST['pd_id'];
$ssid=$_POST['ct_session_id'];
$total=$_POST['tot'];
$name=$_POST['basedes'];
$qty=$_POST['ct_qty'];
$extra_array = $_POST['extra'];
if ( $extra_array > "0" ) {
foreach ($extra_array as $one_extra) {
$source .= $one_extra.", "; }
$extra = substr($source, 0, -2);
} else {}
$result=mysql_query("INSERT INTO tbl_cart (pd_id, ct_qty, ct_session_id, ex_tra, ex_tra2, ex_price, ct_date) VALUES ('$id','$qty','$ssid','$extra','$name','$total', NOW())");
?>
Best regard to all
Sending two values in one variable is bound to get messy. I would just send the id and get the corresponding values from the database again in extradata.php.
If you really want to send multiple values, I would use fixed indices for the checkboxes (not extra[] but extra[SOME_NUMBER] and add a hidden field next to it (extra_pr[SOME_NUMBER]?) to pass the second value.
Note that you have to use fixed indices as unchecked checkboxes donĀ“t get posted but all hidden fields will get posted.