For a survey app which has a combination of radiobuttons and checkboxes. How do I use PHP to insert the selected values to mysql db using php?
This is the query for fetching the questions from db:
while($row2 = $result2->fetch_assoc())
{
if($row2["subtype"] =="radio")
{
echo "<input id = \"radio\" class='radio_input' type=\"radio\" name=answergroup[".$row["PK_QUESTION_ID"].
"] value=".$row2["opt_id"].">".$row2["opt_text"]."</input>";
else if($row2["subtype"] =="checkbox")
{
echo "<input id = \"checkbox\" class='radio_input' type=\"checkbox\" name=answergroup[".$row["PK_QUESTION_ID"].
"] value=".$row2["opt_id"].">".$row2["opt_text"]."</input>";
}
}
I tried using $_POST['answergroup'] for fetching the selected options, but in the case of a checkbox, only one option is being fetched even if there are multiple selections made.
When you are dealing with multiple checkboxes with the same name, in that case the name of checkboxes must be an array like:
<input type="checkbox" name="options[]" value="1">
On form submit you can get the values like:
$options = $_POST['options'];
here $options is an array, so use foreach() to get its elements.
Related
I have the table below which I have fileld with some default values:
while($records=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>".$records['item1']."</td>";
echo "<td>".$records['item2']."</td>";
echo "<td>".$records['item3']."</td>";
echo "<td>".$records['item4']."</td>";
echo "<td>".$records['item5']."</td>";
echo "<td>".$records['item6']."</td>";
echo "<td>".$records['item7']."</td>";
echo "</tr>";
}
echo "</table>";
I also have a my_sql query statement that looks like this and references a function which sorts all the data by asc or desc:
$sortBy=$_POST['SortBy'];
$sortIn=$_POST['SortIn'];
$sql="SELECT * FROM Results ORDER BY $sortBy $sortIn";
$result=mysql_query($sql, $conn);
echo "<table>";
All of this works successfully with a bunch of connection statements that I won't include.
I also have a 2 option radio button that looks like this:
Include newItem
<input type="radio" name="IncludeNewItem" value="1" />Yes
<input type="radio" name="IncludeNewItem" value="0" />No
What I am trying to do is set it so that when I submit the user options from the HTML page, the table is generated (and sorted) and depending on the state of the radio button, it should either display or not display another column which already exists in the database. (For examples sake, let's call this 'item 8').
I tried creating something that looks like this:
$includeNI = if(isset($_POST['IncludeNewItem']))
{Alter Table Results Add $newItem };
But I'm really unsure about the syntax as I'm new to this language.
All help is appreciated.
I am trying to get the option selected by the user by using PHP (failing in that) where the dropdown itself contains the names of the tables and has been generated using PHP. This is my code for dropdown and grabbing its value:
Edited version of the code:
if(isset($_POST["submit"]))
{
//This code is to show all the tables in database ecommerce having word "brand" in it
$query = "SHOW TABLES FROM ecommerce LIKE '%_brand'";
$runQuery = mysql_query($query);
echo "<form method='post' action='#'>";
echo "<select name='mee'>";
while($row = mysql_fetch_array($runQuery))
echo "<option value='something'>".$row[0]."</option>";
echo "</select>";
echo "</form>";
$var = $_POST["mee"];
echo $var;
}
this is the error:
Undefined index: mee in C:\......
Try this for the option tag.
<option value="something ">.$row[0].</option>
Maybe you are missing option value
Add values to options. You're adding values from database to options in that way that user can see the value when selecting. But you don't set value of each option, so when you send selected option it will use value of option and send it with post method.
echo "<option value='".$row[0]."'>something that user see</option>"
I'm working on a homework assignment that involves PHP. Basically, I have a page that contains a web form that renders items pulled from a database. There's checkboxes on each row and 2 radio buttons. The user selects "accept" or "deny" and when submit is clicked, the items that are checked are supposed to change to that approval status. All of the items in the form are submitted into post. I thought that post is an array so I could just use a while loop with a counter so that the loop traverses through the array and when it gets to the last index (which should contain approve or deny). A query is generated that changes all of the previous indexes to approve or deny. I'm sorry if this isn't making much sense.
Here's a picture for more clarification
Here's the code I used to generate the webform:
<?php
#create a query string
$query = "SELECT * FROM Request WHERE superemail = '$user'";
#echo $query;
#run the query
$result = mysqli_query($link, $query) or die('error querying');
while($row = mysqli_fetch_array($result)){
#print out each row of the queryi
#line up the query results with temporary strings
$change = $row['KEY'];
$name = $row['first']. " " . $row['last'];
#echo $name;
$email = $row['email'];
#echo $email;
$type = $row['type'];
#echo $type;
$duration = $row['duration'];
$status = $row['status'];
#create a table row with the query results
echo "<tr><td><input type=checkbox name=$change /></td>
<td>$name</td>
<td>$email</td><td>$type</td>
<td>$duration</td><td>$status</td></tr>";
} #end while
?>
<label for=update>Change status to:</label><br />
<input type=radio name=update value=A />Approved<br />
<input type=radio name=update value=D />Denied<br />
<input type = submit value = "Change Status" />
Each input tag (your checkboxes and your radio button) in your html should have a name attribute, such as: <input type='radio' name='accept' value='1' />. When the form is submitted and processed by PHP, your script will be able to access that info at $_POST['accept'] (or $_GET['accept'] depending on the form's method).
So you should be able to specify a name for the radio button, then check to see if there is a value in the POST array at that index.
I am going to assume a few things. First, that it is a design goal of yours to only process items in your approval queue that you choose to select, leaving the others alone. Second, that you want to change their status to what is chosen in a radio button. Third, that you want both a code snippet and an explanation as to how the task got accomplished.
So, here goes.
You have a series of checkboxes; I assume they have the same name. If you wish for the values to be passed to PHP as an array, you absolutely need to name the inputs whatever[] with emphatic emphasis on the []! This is what creates an array from the checkbox to appear in the $_POST/$_GET. Only those items selected will appear in the array. The value ought to be something useful (A value in the corresponding database table, say) which you can select for and query on...after sanitizing the input, of course.
So, your HTML input tag should look like this:
<input type="checkbox" name="process[]" value="<?php echo $employeeName ?>" >
You should have this coming back at you...
$_POST['process'][array(0=>name1, 1=>name2/*...etc*/)]
...which you can loop through with a foreach at your leisure.
I have this code for example :
$b = "";
while ($row = mysql_fetch_array($rows)) {
if ($row['enabled'] == 1) {
$b = "checked";
} else {
$b = "":
}
echo "<\input name='nam[$row[id]]' type='checkbox' value='$row[id]' $b />";
}
When I execute this code, I will get a list of checkboxes, some of them are checked and others are not.
I can use this code to get a list of checked checkboxes.
if (isset($_POST['sub'])) { //check if form has been submitted or not
$nam = $_POST['nam'];
if (!empty($nam)) {
foreach($nam as $k=>$val){
// proccess operation with checked checkboxes
}
}
I need to know how I can get list of unckecked checkboxes after submitting the form.
Thanks in advance.
If the check boxes are dynamically created this is a trivial task:
Create a naming convention. For example, lets say that each checkbox is tied to a row in the DB, each row in the DB has a primary key. You can name each checkbox based off this key:
<input type="checkbox" id="foo_1" name="foo_1" />
<input type="checkbox" id="foo_2" name="foo_2" />
<input type="checkbox" id="foo_3" name="foo_3" />
Now when the form is submitted you can query the database to get the Id's and process the request as follows:
$res = mysql_query("select * from foo;");
while($row = mysql_fetch_array($res))
$checked = isset($_POST["foo_{$row['id']}");
...
}
Browsers send nothing for unchecked boxes, so your only hope is creating a hidden field that tells you the name of each checkbox (or add a hidden field for each checkbox).
I'm trying to build a simple app:
Six columns with these values
Nr, Name, checkbox 1, checkbox 2, checkbox 3, checkbox 4.
The checkboxes are project stages. I'm using MySQL to store the data and retrieving it to a webpage with PHP.
My problem is on how to go updating the checkboxes, and also how to filter the results (display only records with checkbox 1 checked).
Or can anyone recommend other solutions/platforms?
This is not overly complex I thought someone must knew a better or easier way to do this.
After your sql query, something like this would work:
if($row['checkbox1'])
echo "<INPUT TYPE=CHECKBOX NAME=checkbox1 CHECKED>";
else
echo "<INPUT TYPE=CHECKBOX NAME=checkbox1>";
Query for filtering:
select * from table where checkbox1 = 1;
<input type='checkbox' name='aname' value='avalue' <?php
if(isChecked($dbrow, $itemname)) // you write this function
{
echo "checked='checked'";
}
?>/>