Checkbox values in to array - php

<?php
$query_o_meno = "SELECT meno FROM uctovnictvo GROUP BY meno ORDER BY meno";
$result_meno = mysql_query($query_o_meno) or die(mysql_error());
?>
<form method="post">
<?php
echo "<strong>Meno:</strong>";
echo "<br />";
while($row = mysql_fetch_array($result_meno)){
echo '<input type="checkbox" name=" option_meno[] " value="XXX" />' . $row['meno'];
echo "<br />";
}
?>
<input type="submit" name="Filtrovat" value="Filtrovat" />
</form>
Hello could u please help me to solve:
SOLVED - Instead of XXX in value="XXX" i wanna have value of $row['meno']
TO DO - Checked values I wanna insert in to array
Thank you

echo "<input type='checkbox' name='option_meno[]' value='".$row['meno']."' />" . $row['meno'];
Does that put the value in the 'value'?

Related

Delete individual lines/rows with PHP button

What I am trying to do is have a PHP page display the contents of a MySQL database and and each line it displays, it give me a delete button so I can delete individual rows. I have the code kind of working. The code delete the lines of code, but not the one I select, instead it deletes the last line and not the one I tell it to delete.
Here is a screenshot, I know it does not look pretty, I was going to clean it up after I get the code working.
HTML:
<?php
// Delete php code
if(isset($_POST['delete_series']))
{
// here comes your delete query: use $_POST['deleteItem'] as your id
$delete = $_POST['delete_series'];
$delete_sql = "DELETE FROM `Dropdown_Series` where `id` = '$delete'";
if(mysql_query($delete_sql, $conn))
{
echo "Row Deleted </br>";
echo "$delete";
};
}
//Insert Code
if(isset($_POST['add_series']))
{
$insert_sql = "INSERT INTO dropdown_series (series) VALUES ('$_POST[add_series]' )";
if(!mysql_query($insert_sql, $conn))
{
die ('Error: ' . mysql_error());
}
echo "Series Added <br><br>";
}
?>
<br />
<h1>Add New Series Title</h1>
<h3>Add New: </h3><br />
<form name = action="add_series.php" method="POST">
<input type = "text" name = "add_series" required/><br /><br />
<input type ="submit" name="submit" value="Add">
</form>
<h3>Current Series: </h3><br />
<?php
//Delete button on each result of the rows
$query = mysql_query("SELECT id, series FROM Dropdown_Series"); // Run your query
// Loop through the query results, outputing the options one by one
echo "<form action='' method='POST'>";
while ($row = mysql_fetch_array($query))
{
echo $row['series'] . " ";
echo $row['id'];
echo "<input type='hidden' name='delete_series' value=' " . $row['id'] . "' />";
echo "<input type='submit' name='submit' value='Delete'>";
//echo $row['series'] . " ". $row['id'] . "<input type='submit' value='delete'>";
echo "<br />";
}
echo "</form>";
?>
Your loop will result in multiple delete_series inputs - within the same form.
You could create separat forms for each option:
while ($row = mysql_fetch_array($query))
{
echo "<form action='' method='POST'>";
// ...
echo "<input type='hidden' name='delete_series' value=' " . $row['id'] . "' />";
echo "<input type='submit' name='submit' value='Delete'>";
// ...
echo "</form>";
}
I believe that a very quick answer would be
while ($row = mysql_fetch_array($query))
{
echo "<form action='' method='POST'>";
echo $row['series'] . " ";
echo $row['id'];
echo "<input type='hidden' name='delete_series' value=' " . $row['id'] . "' />";
echo "<input type='submit' name='submit' value='Delete'>";
//echo $row['series'] . " ". $row['id'] . "<input type='submit' value='delete'>";
echo "<br />";
echo "</form>";
}
If I were you I would prefer injecting some javascript in the logic, and perform the Delete request using an ajax call.
you can refer to this tutorial to get more understanding of what I am refering to https://scotch.io/tutorials/submitting-ajax-forms-with-jquery

PHP variables inside HTML Code

I am making a student sign-up form using html and php.
First you are asked to insert your name, password and email and when you click submit, it takes you to another page (ChooseDepartment.php) in which you get a list of all departments from my database to choose your own.
Now, I am a total newbie, so here is the part of my php code that I am stuck with in ChooseDepartment.php:
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
echo "<br />"."Available departments: "." ".mysql_num_rows($ShowPossibleDep)."<br />";
echo "<br />";
echo '<form id = "dept" action = "Courses.php" method = "post">';
while($row = mysql_fetch_array($ShowPossibleDep))
{
echo $row['NAME'];
echo '<input type="radio" name="department" value=<?php $row['DEPT_ID'] ?>>';
echo "<br />";
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment">';
echo </form>;
}
I am trying to make the value of the radio button carry the value of the
the department id so I can then update my database with the student's department which is currently NULL, but I can't figure out how to use both html and php at the very same line correctly! This gives me syntax error!
as you're in PHP, so you don't need to open and close PHP tag.
The reason you're getting Syntax error is just because you're not manipulating string properly.
error is with this line
echo '<input type="radio" name="department" value=<?php $row['DEPT_ID'] ?>>';
^ here ^ here
So you need to remove the PHP tags and need to concatenate string properly like:
echo '<input type="radio" name="department" value="'.$row['DEPT_ID']. '">';
and with this one
echo </form>;
you're missing quotes around form tag. So it should be,
echo '</form>';
There are some other typos are as well, so your final code will be look like this.
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
echo "<br />Available departments: ".mysql_num_rows($ShowPossibleDep);
//echo "<br />"; add this <br /> tag to next echo
echo '<br /><form id = "dept" action = "Courses.php" method = "post">';
while($row = mysql_fetch_array($ShowPossibleDep))
{
echo $row['NAME'];
echo '<input type="radio" name="department" value=" '.$row['DEPT_ID'].'"><br />';
//or you can do this way
//echo "<input type='radio' name='department' value='$row[DEPT_ID]'><br />";
//echo "<br />"; appended in upper statement.
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment"></form>';
//echo </form>; closed already(above statement).
}
and without comments, more cleaner :)
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
echo "<br />Available departments: ".mysql_num_rows($ShowPossibleDep);
echo '<br /><form id = "dept" action = "Courses.php" method = "post">';
while($row = mysql_fetch_array($ShowPossibleDep))
{
echo $row['NAME'];
echo '<input type="radio" name="department" value=" '.$row['DEPT_ID'].'"><br />';
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment"></form>';
}
Take a look at string operators
http://php.net/manual/en/language.operators.string.php
You can combine two strings in php with a dot, so that part of your code would become this:
{
echo $row['NAME'];
echo '<input type="radio" name="department" value="'.$row['DEPT_ID'].'">';
echo "<br />";
}
No need to open php tag again
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0) {
echo "<br />"."Available departments: "." ".mysql_num_rows($ShowPossibleDep)."<br />";
echo "<br />";
echo '<form id = "dept" action = "Courses.php" method = "post">';
while ($row = mysql_fetch_array($ShowPossibleDep)) {
echo $row['NAME'];
echo '<input type="radio" name="department" value="' . $row['DEPT_ID'] .'">';
echo "<br />";
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment">';
echo "</form>";
}

PHP remeber checked checkboxes

I am trying to have a filter using checkboxes. Problem is when I unselect some boxes
and submit values, all boxes are selected again. I think the problem can be here
if (isset($_POST['option_meno']))
What can be the problem?
And yes Im new here, so any ideas hot to make my code simpler would help me also.
Thank you.
<?php
// Make a MySQL Connection
$query_o_meno = "SELECT meno FROM uctovnictvo GROUP BY meno ORDER BY meno";
$result_meno = mysql_query($query_o_meno) or die(mysql_error());
?>
<form method="post">
<?php
if (isset($_POST['Filtrovat'])) {
// Print checked checkboxes
echo "<strong>Meno:</strong>";
echo "<br />";
while($row = mysql_fetch_array($result_meno)){
//I believe the problem is line below -----------------------------
if (isset($_POST['option_meno'])) {
echo "<input type='checkbox' name='option_meno[]' value='".$row['meno']."' checked />" . $row['meno'];
echo "<br />";
//}
}
else{
echo "<input type='checkbox' name='option_meno[]' value='".$row['meno']."' / >" . $row['meno'];
echo "<br />";
}
}
}
else{
$option_meno = array();
echo "<strong>Meno:</strong>";
echo "<br />";
while($row = mysql_fetch_array($result_meno)){
echo "<input type='checkbox' name='option_meno[]' value='".$row['meno']."' checked />" . $row['meno'];
echo "<br />";
$option_meno[] = $row['meno'];
}
}
?>
<input type="submit" name="Filtrovat" value="Filtrovat" />
</form>
This is not tested, but should work:
if (isset($_POST['Filtrovat']))
{
// Print checked checkboxes
echo "<strong>Meno:</strong>";
echo "<br />";
while($row = mysql_fetch_array($result_meno))
{
$checked = "";
if(isset($_POST['option_meno']))
$checked = in_array($row['meno'],$_POST['option_meno'])?"
checked = 'checked' ":"";
echo "<input type='checkbox' name='option_meno[]'
value='".$row['meno']."' $checked / >" . $row['meno'];
echo "<br />";
}
}
Since you're doing name='option_meno[]', $_POST['option_meno'] will be an array. That array will always be set as long as at least one checkbox is checked. Try something like this:
<?php
// Make a MySQL Connection
$query_o_meno = "SELECT meno FROM uctovnictvo GROUP BY meno ORDER BY meno";
$result_meno = mysql_query($query_o_meno) or die(mysql_error());
?>
<form method="post">
<?php
if (isset($_POST['Filtrovat'])) {
// Print checked checkboxes
echo "<strong>Meno:</strong>";
echo "<br />";
while($row = mysql_fetch_array($result_meno)){
//I believe the problem is line below -----------------------------
if (isset($_POST['option_meno'][$row['meno']])) {
echo "<input type='checkbox' name='option_meno[".$row['meno']."]' value='".$row['meno']."' checked='checked' />" . $row['meno'];
echo "<br />";
//}
}
else{
echo "<input type='checkbox' name='option_meno[".$row['meno']."]' value='".$row['meno']."' / >" . $row['meno'];
echo "<br />";
}
}
}
else{
$option_meno = array();
echo "<strong>Meno:</strong>";
echo "<br />";
while($row = mysql_fetch_array($result_meno)){
echo "<input type='checkbox' name='option_meno[".$row['meno']."]' value='".$row['meno']."' checked />" . $row['meno'];
echo "<br />";
$option_meno[] = $row['meno'];
}
}
?>
<input type="submit" name="Filtrovat" value="Filtrovat" />
</form>
This way you check for each checkbox individually.
Note: You should always fully declare the checked attribute, like so: checked='checked'

Inserting values from mysql_fetch_assoc into form input

I have som problem writing back to table.
I connect to table as wanted and get the information output correctly. The input correctedby, and the checkboxes is saving as i want.
but i dont know how to write the mysql_fetch_assoc back to table
I have this kode;
<?php
session_start();
$_SESSION['mypassword']="myusername";
echo "Logged in as:<br>" .$_SESSION['myusername'];
include "header.inc.php";
include "funksjoner.inc.php";
//in this file the connetion to server
$connection= kobleTil(); //trenger ikke oppgi databasenavn
//Steg 2: SQL-query
$sql = "SELECT * FROM oppgave WHERE modulid=1 AND resultat is NULL ORDER BY RAND() LIMIT 1";
$result = mysql_query($sql, $connection);
echo "<hr>";
while ($nextrow= mysql_fetch_assoc($result)){
echo "answer: " . $nextrow['answer'];
echo "<br>Modulid: " . $nextrow['modulid'];
echo "<br>student: " . $nextrow['studentid'];
echo "<br>";
}
echo '<form name="input" action="tilretting.php" method="get">';
echo'<input type="text" name="correctedby" value="'.$_SESSION['myusername'].'">';
echo 'Not approved<input type="checkbox" name="resultat" value="0">';
echo 'Approved<input type="checkbox" name="resultat" value="1">';
echo '<input type="text" name="studentid" value="dont know how to write correct here">';
echo '<input class="levermodulknapp" type="submit" name="lever1" value="Lever modul 1">';
echo "</form>";
echo "<hr>";
?>
how can I get the form to get the value from mysql_fetch_assoc into the form?
Is the mysql_fetch_assoc the right thing to use?
Very gratefull for any tip!
$result = mysql_query($sql, $connection);
echo "<hr>";
while ($nextrow= mysql_fetch_assoc($result)){
echo "answer: " . $nextrow['answer'];
echo "<br>Modulid: " . $nextrow['modulid'];
echo "<br>student: " . $nextrow['studentid'];
echo "<br>";
echo '<form name="input" action="tilretting.php" method="get">';
echo'<input type="text" name="correctedby" value="'.$_SESSION['myusername'].'">';
echo 'Not approved<input type="checkbox" name="resultat" value="0">';
echo 'Approved<input type="checkbox" name="resultat" value="1">';
echo '<input type="text" name="studentid" value="'.$nextrow['columnName'].'">';
echo '<input class="levermodulknapp" type="submit" name="lever1" value="Lever modul 1">';
echo "</form>";
echo "<hr>";
}
or
$data = null;
$result = mysql_query($sql, $connection);
echo "<hr>";
while ($nextrow= mysql_fetch_assoc($result)){
echo "answer: " . $nextrow['answer'];
echo "<br>Modulid: " . $nextrow['modulid'];
echo "<br>student: " . $nextrow['studentid'];
echo "<br>";
$data = $nextrow['colunmName'];
}
echo '<form name="input" action="tilretting.php" method="get">';
echo'<input type="text" name="correctedby" value="'.$_SESSION['myusername'].'">';
echo 'Not approved<input type="checkbox" name="resultat" value="0">';
echo 'Approved<input type="checkbox" name="resultat" value="1">';
echo '<input type="text" name="studentid" value="'.$data.'">';
echo '<input class="levermodulknapp" type="submit" name="lever1" value="Lever modul 1">';
echo "</form>";
echo "<hr>";
}
?>

Get and display the multiple choice that have been selected

So this is my form that let the users select input from a table :
echo"Please select questions to analyze : ";
$qsq = mysql_query("SELECT DISTINCT question_text FROM questions ");
echo "<form name='whatever' action='next.php' method='get'>";
while($row = mysql_fetch_assoc($qsq))
{
echo"<input type='checkbox' name='choice' value='" . $row['question_id'] . "' /> ". $row['question_text'] . '<br />';
}
echo"<br>";
echo "<input type='submit' value='submit' /></form>";
In the next.php where the form directs to,how can i display the selected one ? should i make an array or any better suggestions ?Please help me i am a beginner in this.Thanks in advance.
echo"<input type='checkbox' name='choice[]' value='" . $row['question_id'] . "' /> ". $row['question_text'] . '<br />';
Add [] to the name of checkbox.
This will post the check box as an array.
$_POST['choice'] = array('questionId1', 'questionId2');

Categories