I have a php file that is receiving some checkbox values from a form. Both the checkbox name and value are set up to match an Item_Name field in a mysql table. My current attempt is below:
while($row = $items->fetch_assoc()){
if( isset($_POST[$row['Item_Name']])) {
\\ Code to perform if true, mostly echoes
}
}
//Checkbox setup:
echo "<input type='checkbox' name=" . $row['Item_Name'] . "value=" . $row['Item_Name'] . ">"
$items is the data returned by my query of the mysql table. Currently none of the echoes inside the if are triggering so I think something is wrong with my if statement, but I'm to new to php to know what is wrong exactly.
Your problem is in your checkbox setup; you are missing quotes around the name and value attributes. Try this instead:
echo "<input type='checkbox' name=\"" . $row['Item_Name'] . "\" value=\"" . $row['Item_Name'] . "\">";
Related
I am very new to php and am teaching it to myself, so please keep that in mind.
I am working on a project that presents users with a list of items, from a database, with checkboxes, and allows the user to check them. I want to save the values of the checked fields. This is the line of code that prints all of the options. It prints a course code and course title, with a checkbox.
echo "<input type='checkbox' name ='boxes'>" . $row['course'] . ' ' . $row['title'] . "<br>";
However, when I try to print the values selected, it doesn't work. I get an error that says invalid argument supplied foreach()
if(isset($_POST['submit_courses'])){
if(!empty($_POST['boxes'])){
foreach($_POST['boxes'] as $selected ){
echo $selected."</br>";
}
}
}
Please help!
Try:
echo "<input type='checkbox' name ='boxes[]'>" . $row['course'] . ' ' . $row['title'] . "<br>";
EDIT:
To better answer the question:
You need to add the value="" attribute to your input fields. So if you have an ID in your $row array then it would be like this...
echo "<input type='checkbox' name ='boxes[]' value=' . $row['id'] . '>" . $row['course'] . ' ' . $row['title'] . "<br>";
Now you should be able to get the selected id's from the populated values inside of the $_POST['boxes'] array. Just loop through them and do something with them like echo them out.
foreach ($_POST['boxes'] as $box_value) {
echo $box_value . "<br>";
}
Only the boxes that are selected will be in the array and you will have the identifier for whatever courses were selected.
First off, I want to store the names of these checkboxes which are submitted, and not their values.
This is my code:
<?php
$con=mysqli_connect("localhost","root","","notifier");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM student");
echo "Enter the attendance. Please untick for 'ABSENT' students and submit";
echo "<br>";
echo "<form action=\"d.php\" method=\"post\">";
while($row = mysqli_fetch_array($result))
{
echo "<br>" .$row['classrollno'] . "   <input type=\"checkbox\" name=\"" . $row['studentid'] . "\" value=\"P\" checked>";
}
echo "<input type=\"submit\" name=\"submit\" value=\"submit\">";
echo "</form>";
?>
This code simply fetches a column of student rollnumberss from student table, prints them, and as well as prints a checkbox infront of them which is checked by default.
Names of checkboxes will be the student id (varchar, another column).
Now since All Checked checkboxes, that is the checboxes which will be submitted to next page will have same default value "P", I m not concerned about their values.
How do I store the names of these checkboxes in an array, and later on use it to perform updation in table for all these student id's?
Use the following code:
while($row = mysqli_fetch_array($result))
{
echo '<br>' .$row['classrollno'] . ' <input type="checkbox" name="studentId[]" value="' . $row['studentid'] . '" checked />';
}
Then, when you process the form, the $_POST['studentId'] variable will contain an array with all the id's.
Since the value that will probably be inserted in the db is 'P' for every student, you wouldn't need to include it in your form, but just hardcode it in your query.
Keep adding the names to an array. Its straight forward.
Declare $allStudentIds = array(); outside while loop. Then, to store in that array,
$allStudentIds[] = $row['studentid'];
Since you wanted to use these values later, you can directly store them inside a session variable:
$_SESSION['allStudentIds'][] = $row['studentid'];
In above case, $_SESSION['allStudentIds'] will be an array of all student ids selected.
Note: You need to start session using session_start() as the first line in the script after opening <?php tag.
Simply, in the fetching while loop, define an array and set each checkbox value to one of its elements then assign it as a session variable:
while($row = mysqli_fetch_array($result))
{
echo "<br>" .$row['classrollno'] . "   <input type=\"checkbox\" name=\"" . $row['studentid'] . "\" value=\"P\" checked>";
$names[] = $row['studentid'];
}
Then,
$_SESSION['names'] = $names;
Your confusion seems to stem from the fact that you are mixing the View (the name of the checkbox in HTML) and the Model/Data (which the student_id you are getting from your DB query ie. the $row = mysqli_fetch_array($result) in the while loop).
All you need to do is create an empty array (eg. $studentid_arr) before the loop and after the echo statement which is just contributing to the view (the HTML) you do some work with your data. What you want to do currently is to store the student_ids (and not the name of the checkbox) in your $studentid_arr.
That can be done with a simple array_push ($studentid_arr,$row['studentid']);
So your while loop would look like
while($row = mysqli_fetch_array($result))
{
echo "<br>" .$row['classrollno'] . "   <input type=\"checkbox\" name=\"" . $row['studentid'] . "\" value=\"P\" checked>";
array_push ($studentid_arr,$row['studentid']);
}
Now you can just POST this PHP array to your next script which is expecting these values. (which is what I assume you mean by submitting to the next page)
I'm working on trying to switch out a hidden input with a value set to a button with a value set in order to have more than one possible outcome from the same form.
echo "<button class='btn-mini btn' type='submit' formaction='inc/delete.php' value='" . $record['id'] . "'><i class='icon icon-remove'></i></button>";
Once, submitted I try to assign the value to a variable and dump it:
$getid = $_GET["id"];
var_dump('$getid');
But, I end up with this error:
Notice: Undefined index: id in C:\xampp\htdocs\address-book\inc\delete.php on line 5
string(6) "$getid"
This there a different way to pull the value of a <button> verse that of a <input> using $_GET?
There is a difference between using single quotes and double quotes.
Simple quote will not parse variables but, understand the input as 'string' only!
So, in essence:
suppose: your URL as : ?id=3
$getid = "hello world";
echo '$getid'; // O/P will be $getid
echo "$getid"; // O/P will be hello world
coming to your question, you need to define a name for anything that is submitted in the form:
echo "<button class='btn-mini btn' type='submit' formaction='inc/delete.php' value='" . $record['id'] . "'><i class='icon icon-remove'></i></button>";
should be:
echo "<button class='btn-mini btn' name='id' type='submit' formaction='inc/delete.php' value='" . $record['id'] . "'><i class='icon icon-remove'></i></button>";
i have this quick issue please.
I have this code here which permits me to extract a user name and a photo, and when the name is clicked it takes me to this hostess.php file, well, i need to pass the id variable to the hostess.php and save it, in order to get information only for that id..
Here is the code:
while ($row = mysql_fetch_array($query)) {
echo "<div id='photo'>";
echo "<div id='picture'>";
echo "<td> <img src=foto/photo1/".$row['photo'] . "></td>";
echo "</div>";
echo "<div id='text'>";
echo '<td>'. $row['first_name_en']." ". $row['family_name_en']."</td>";
echo "</div>";
echo "</div>";
}
How can i just get the id and then how can i save it to the $id variable
Thanks
The table structure is like this:
The table name is called hostess and the field i need to retrieve from hostess is the [id]
Change the line:
echo '<td><a href="hostess.php">'. $row['first_name_en'] .
" ". $row['family_name_en']."</a></td>";
to this:
echo '<td><a href="hostess.php?id={$row[id]}">'. $row['first_name_en'] .
" ". $row['family_name_en']."</a></td>";
and in hostess.php, use this to extract the value:
$id = $_GET['id'];
EDIT
Here is another method to pass variable as POST. Again, change the aforementioned line to:
echo '<td><form method="POST" action="hostess.php">' . "<input type='hidden'
name='id' value='{$row[id]}' />" . "<input type='submit' value='" .
$row['first_name_en'] . " " . $row['family_name_en'] .
" /></form></td>";
You can use CSS to style that button as simple text too. And the value can be retrieved in hostess.php as follows:
$id = $_POST['id'];
Add/change this in your code:
$personID = $row['id']; #or whatever the field name is
echo '<td>'. $row['first_name_en']." ". $row['family_name_en']."</td>";
The addition of "?personID='.$personID will send the value in the GET statement, or within the URL.
On the receiving page get the value that way:
$personID = $_GET['personID']+0; #add zero to force a numeric value--be sure to scrub your data!
#be sure to do other validation if needed!
I have a search function using XPath, when results are shown a check box is also echoed.
<form name="save" method="POST" action="saveProcess.php">
<?php
foreach ($holidays as $holiday)
{
$resultTable .= "<p>{$holiday->title}" . "<br/>" .
"{$holiday->pubDate}" . "<br>" .
"{$holiday->description}" . "<input type='checkbox' name='chk' value='{$holiday->title}' />" . "<br /></p>";
}
?>
<input type="submit" value="submit"/>
</form>
I would like this check box to hold the value of {$holiday->title} which when the form is submitted will be shown in saveProcess.php, i use the isset method to check if the variable is set and it is not.
if (isset($_POST['chk'])) {
echo $_POST['chk'];
}
else
{
echo"variable is not set";
}
Where am i going wrong?
The problem is that you name each checkbox "chk", and when you submit the form, the values get overwritten. That's why it doesn't get anything in saveProcess.php. What you need to do, is either specify that the $_POST["chk"] can contain an array of value, like so:
<input type='checkbox' name='chk[]' value='{$holiday->title}' />
Notice the square brackets in the name. Now $_POST["chk"] will be an array.
Another way, would be to leave the html as it is, and just get the data, in saveProcess.php, using:
$HTTP_POST_VARS["chk"]
The first part basically explains why it doesn't work and how to fix it, while the second suggestion, is merely an alternate way of getting the data.
Have a great day!
Your code looks ok to me, just remember that the value of a checkbox is posted only if the checkbox is checked, if it's not checket $_POST['chk'] is not set
EDIT - since you are revriting your checkboxes as suggested in the comment use an array
<?php
foreach ($holidays as $holiday)
{
$resultTable .= "<p>{$holiday->title}" . "<br/>" .
"{$holiday->pubDate}" . "<br>" .
"{$holiday->description}" . "<input type='checkbox' name='chk[]' value='{$holiday->title}' />" . "<br /></p>";
}
?>
And then server side $_POST['chk'] will be ann array