checkbox array not working - php

<input type='checkbox' id='checkbox-" . $counter ."' class='mdl-checkbox__input' name='product[]' value='$counter'>
for some reason, when I run the form with this name for this input, it won't run the PHP script, it won't even run the beginning of the script.
Is there some reason this isn't working? am I doing it wrong? I thought this is the actual way this would work.
[Edit]
this is the actual full script in the form:
foreach($producten as $row)
{
echo("
<label class='mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect' for='checkbox-" . $counter ."'>
<input type='checkbox' id='checkbox-" . $counter ."' class='mdl-checkbox__input' name='product[]' value='$counter'>
<span class='mdl-checkbox__label'>" . $row['productcode'] . ' ' . $row['categorie'] . ' ' . $row['merk'] . ' ' . $row['type'] . ' ' . $row['cpu'] . ' ' . $row['ram'] . ' ' . $row['os'] . ' ' . $row['hdd'] . ' ' ."</span>
</label>
");
$counter++;
}

.
I have checked with below code as related your code. It's working with Checkbox array. I hope it's will help you. All the best.
<form action="#" method="POST">
<?php
$producten = array('1a','2b','3c','4d');
$counter = 1;
foreach($producten as $row)
{
echo("
<input type='checkbox' id='checkbox-" . $counter ."' class='mdl-checkbox__input' name='product[]' value='$row'> <label class='mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect' for='checkbox-" . $counter ."'> $row </label><br>
");
$counter++;
}
?>
<input type="submit" name="submit" value="Submit">
</form>
<?php echo "<pre>"; print_r($_REQUEST); ?>
Output:
Array
(
[product] => Array
(
[0] => 1a
1 => 2b
[2] => 3c
[3] => 4d
)
)

Related

Codes works with the PHP on windows server but same not in linux

The code below works fine with the windows server for php 8.0, but it does not load the DOM further on Linux server with same php version,
<div class="form-group">
<label>User: </label>
<select class="form-control select2 user_cal" type="text" name="user_holidays[]" id="users_filter" data-placeholder="Select User.." multiple>
<?php
echo "<option value=''>--</option>";
$sql_qry_bdg = "select Distinct d.Badge,d.First_Name,d.Last_Name,(select count(*) from Calend where to_date(booked_date,'YYYY/MM/DD') > TO_DATE('" . $last_year_sunday . "','YYYY/MM/DD') and to_date(booked_date,'YYYY/MM/DD') < TO_DATE('" . $next_year_sunday . "','YYYY/MM/DD') and IS_ACTIVE='Y' and User_Badge=D.Badge) as count from Dir d
where d.type !='Agency'";
foreach ($conn->query($sql_qry_bdg) as $row) {
//$manu = $row['FULLNAME'];
echo "<option value='" . $row['BADGE'] . "'";
if (in_array($row['BADGE'], $_POST['user_holidays'])) {
echo " selected ";
}
echo ">" . $row['BADGE'] . " - " . $row['FIRST_NAME'] . " " . $row['LAST_NAME'] . " (" . $row['COUNT'] . ")</option>";
}
?>
</select>
</div>
Can anyone please help
Thank You

How to append new elements to the end of a session in php?

I have a Session where I am storing some IDs and I would like to add to the end of that session a string (attach to its own respective id).
Here I have a table with some elements and I have some checkboxes there so when each checkbox is selected I am taking their ids and when posting I am storing those ids to a session:
<form method="post">
<?php if (!empty($arr_devices)) { ?>
<?php foreach ($arr_devices as $device) : ?>
<tr>
<td>
<input type="checkbox" name="devices[]" value="<?php echo $device["id"] ?>">
<?php echo '<a href=error_report.php?device_id=' . urlencode($device["id"]) . "&dev_name=" . urlencode($device["name"]) . "&imei=" . urlencode($device["serial_imei"]) . "&serial_no=" . urlencode($device["serial_no"]) . "&device_no=" . urlencode($device["device_no"]) . '>' . $device["name"] . '</a>'; ?>
</td>
<td>
<?php echo '<a href=error_report.php?device_id=' . urlencode($device["id"]) . "&dev_name=" . urlencode($device["name"]) . "&imei=" . urlencode($device["serial_imei"]) . "&serial_no=" . urlencode($device["serial_no"]) . "&device_no=" . urlencode($device["device_no"]) . '>' . $device["device_no"] . '</a>'; ?>
</td>
<td>
<?php echo '<a href=error_report.php?device_id=' . urlencode($device["id"]) . "&dev_name=" . urlencode($device["name"]) . "&imei=" . urlencode($device["serial_imei"]) . "&serial_no=" . urlencode($device["serial_no"]) . "&device_no=" . urlencode($device["device_no"]) . '>' . $device["barcode"] . '</a>'; ?>
</td>
<td>
<div class="input-group">
<textarea name="dev_comment" placeholder="Write your comment here" rows="1" cols="50"><?php echo $dev_comment; ?></textarea>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php } ?>
<input class="btn" type="submit" name="submit" value="Report">
<form>
Here is the the way I am storing those IDs:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$_SESSION['devices_id'] = $_POST['devices'];
}
what I would like to do is to add the $dev_comment to that Session. So that when I use that Session I need to have the $dev_comments attached to each respective id. How can I implement that?
At first, ensure that $_SESSION['devices_id'] exists and is an array:
if(!isset($_SESSION['devices_id'])) {
$_SESSION['devices_id'] = [];
}
The, $_POST['devices'] is an array, you can easily merge:
$_SESSION['devices_id'] = array_merge($_SESSION['devices_id'], $_POST['devices']);

Get request doesnt work

I have a form that queries my server for data using MySQL. I am using a form that sends get requests. It doesn't show anything and I don't know why. I is so strange because my query is valid and I tested it on PHPmyadmin..I am not striving for answers only, I want to know why this happened and what is the reason behind it.
Here is my code:
<form name="get" action="Roster.php" method="get">
<select name="course" id="course">
<?php
$get = mysqli_query($con, "SELECT teaching.Course_ID FROM `teaching` WHERE teaching.F_ID=213000000 ");
while ($row = mysqli_fetch_assoc($get)) {
echo '<option value ="' . $row["Course_ID"] . '"> ' . $row["Course_ID"] . ' </option>';
}
?>
</select>
<select name="group">
<?php
$get = mysqli_query($con, "SELECT `Group_ID` FROM `teaching` WHERE `teaching`.F_ID= 213000000");
while ($row = mysqli_fetch_array($get)) {
echo '<option value ="' . $row["Group_ID"] . '"> ' . $row["Group_ID"] . ' </option>';
}
?>
</select>
<date-util format="yyyy-mm-dd">
<label for="Date" > Date </label><input id="meeting" name="date" type="date" />
</date-util>
<input type="submit" name="Send" value="Get"/>
</form>
<?php
if ($_GET['submit']) {
$sql = " SELECT enrollment.S_ID,student.ID,student.F_Name,student.L_name,attendance.Status,attendance.Date
From enrollment
INNER JOIN student On enrollment.S_ID
INNER JOIN attendance On enrollment.S_ID
where enrollment.Course_ID =" . $_GET["course"] . "and enrollment.Group_ID =" . $_GET["group"] . "and attendance.date =" . $_GET["date"] . " ";
$result = mysqli_query($con, $sql);
$message = "Please Choose Course_ID and Group_ID ";
if ($result > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "Hello";
echo "<tr>";
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['F_Name'] . " " . $row['L.name'] . '</td>';
echo '<td>' . $row['Date'] . '</td>';
echo '<td>' . $row['Status'] . '</td>';
echo "</tr>";
}
} else {
echo "<tr>";
echo '<td>' . $message . '</td>';
}
}
?>
$_GET['submit'] does not exist. You need to add submit as your name attribute to your button like so
<input type="submit" name="submit" value="Get"/>
Also you should use prepared statements to prevent SQL injection attacks.

Check the answer and verify from database whether it is correct or not in PHP

Fetching questions from database and displaying in screen now what should be the logical part or how to implement for checking if selected answer is correct or not and how to store correct answer in database and verifying them.
Here is the code
<?php
// Create connection
$conn = new mysqli("localhost","root","","QuizQuestions");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully<br><br>";
$sql = "SELECT Question, Answer1, Answer2, Answer3, Answer4 FROM Questions";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$i = 1;
while ($row = $result->fetch_assoc()) {
echo "<br>Question: " . $row["Question"] . "<br>";
echo ' A) <input type="radio" name="ans' . $i . '" value="' .
$row["Answer1"] . '">' . $row["Answer1"] . '<br>';
echo ' B) <input type="radio" name="ans' . $i . '" value="' .
$row["Answer2"] . '">' . $row["Answer2"] . '<br>';
echo ' C) <input type="radio" name="ans' . $i . '" value="' .
$row["Answer3"] . '">' . $row["Answer3"] . '<br>';
echo ' D) <input type="radio" name="ans' . $i . '" value="' .
$row["Answer4"] . '">' . $row["Answer4"] . '<br>';
$i++;
}
} else {
echo "0 results";
}
$conn->close();
?>
Because all of the radio inputs have the same name. They all will be considered as same radio group. You need to have different names for different questions. Something like -
$i = 1;
while ($row = $result->fetch_assoc()) {
echo "<br>Question: " . $row["Question"] . "<br>";
echo ' A) <input type="radio" name="ans' . $i . '" value="' . $row["Answer1"] . '">' . $row["Answer1"] . '<br>';
echo ' B) <input type="radio" name="ans' . $i . '" value="' . $row["Answer2"] . '">' . $row["Answer2"] . '<br>';
echo ' C) <input type="radio" name="ans' . $i . '" value="' . $row["Answer3"] . '">' . $row["Answer3"] . '<br>';
echo ' D) <input type="radio" name="ans' . $i . '" value="' . $row["Answer4"] . '">' . $row["Answer4"] . '<br>';
$i++;
}
You also can use the row id instead of the of $i.
You have set same name for all radio buttons. You should group the radio buttons for each question. For that you can get the question id from the database and set the radio button name like
echo ' A) <input type="radio" name="ans'.$row["id"].'"
value="'.$row["Answer1"].'">'.$row["Answer1"].'<br>';
Make sure you have different names for each input field in the radio group so that it treats each question as a different record.

PHP _POST Data from Form

I have this form that I'm creating using PHP. The names of the form elements are a number
<form method="post" action="keywords.php">
<?php
echo $question
?>
</div>
<div class="panel-body">
<?php
foreach ($orderexplode as $o)
{
switch ($o) {
case 0:
print(" " . "<label>" . $presetwords[$i] . "</label>" . " ");
$i++;
break;
case 1:
print(" " . "<input type='text' class='form-control' name='$k' id ='$k'placeholder=" . $responsewords[$k] . ">" . " ");
$k++;
echo $k;
break;
}
}
?>
<br>
<button type='submit' class='btn btn-info'>Submit</button>
</form>
How would I go about getting the value of each input box I want to check if it matches $exploded string?
$string = "Hello How Are You"
$explodedstring = explode(" ",$string)
You are trying to use array indexes. You need to use indexes:
<?php
foreach ($orderexplode as $k => $o)
{
print(" " . "<label>" . $k . "</label>" . " ");
print(" " . "<input type='text' class='form-control' name='" . $k . "' id ='" . $k . "'placeholder=" . $o . ">" . " ");
}
?>

Categories