I have stored multiple user`s information in a table in mysql. These are supposed to be categorized under 5 tags, A, B, C, D and E. The information is stored in the table as follows:
maybe all the above mentioned tags are used
maybe 'A' is used multiple times, same as B, C, etc.
maybe only a few tags are used and other are never used to categorize
I am providing checkbox options for these tags in my html generated by a php script, and when the user checks some or all of the options, it should retrieve the checked tags information from the table and do something.
But in my case, when some tags are checked and those are not stored in the table, I am getting an error (since their is no tag information in the table, obviously). In that case, how should I program, so that even though I checked some tags, and their information is not in the mysql table, the program should ignore them and display the information related to other checked tags. I am not able to write the program in PHP with MySQL queries. It can be a checkbox or a select.
Let me know if the question is unclear, since it is a bit complex to explain here in words.
To what I finally understand, you want to query your database according to the option the user selected through a checkbox. But if the user can't select more than one option then I suggest you use the radio button giving all radio button the same name so the user can select only one option and run your code like this:
<form action="page.php" method="post">
<input type="radio" name="tag" value="A" />
<input type="radio" name="tag" value="B" />
<input type="radio" name="tag" value="C" />
<input type="submit" name="submit" />
</form>
Then your php page should be like this:
<?php
//your db connection here
if(isset($_POST['submit'])){
$tag = $_POST['tag'];
// remember you should use mysqli because mysql is deprecated and $c0n will be the variable assigned to your db connection
$query = mysqli_query($con, "SELECT id FROM table WHERE tag='".$tag."'");
}
?>
But if the user can select more than one option then you can use the checkbox and run your code like this:
<form action="page.php" method="post">
<input type="checkbox" name="a" />
<input type="checkbox" name="b" />
<input type="checkbox" name="c" />
<input type="submit" name="submit" />
</form>
And this should be your php page:
<?php
//your db connection here
if(isset($_POST['submit'])){
$tag1 = $_POST['a'];
$tag2 = $_POST['b'];
$tag3 = $_POST['c'];
//here you assign variables to each checkbox if they are checked
//but if they aren't you assign the variable to be null or anything
//you wish as long as it won't tamper with your query
if($tag1 == "on"){
$a = "A";
} else {
$a = ""; //assign any wrong value here
}
if($tag2 == "on"){
$b = "B";
} else {
$b = ""; //assign any wrong value here
}
if($tag3 == "on"){
$c = "C";
} else {
$c = ""; //assign any wrong value here
}
// remember you should use mysqli because mysql is deprecated and $c0n will be the variable assigned to you db connection
$query = mysqli_query($con, "SELECT id FROM table WHERE tag='".$a."' OR tag='".$c."' OR tag='".$c."'");
}
?>
Hope this will help you
Related
As above, I"m trying to create a simple html / PHP page. When the submit button is clicked, I would like different SQL code to be run depending on which of the checkboxes is checked. The SQL code is pretty simple, its just displaying a different table for each checkbox, but I'm not sure how to check if the checkboxes are checked. My HTML code is below
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ricky Deacon, CSIS3380 Assignment 3. Show Table.html</title>
</head>
<body>
<h1>Book-O-Rama Catalog Search</h1>
<form action="showtab.php" method="post">
<fieldset><legend>Which Tables Would You Like To View?</legend>
<p>
Customers
<input type="checkbox" name="table" value="cust"/>
Orders
<input type="checkbox" name="table" value="ord"/>
Order Items
<input type="checkbox" name="table" value="itms"/>
Books
<input type="checkbox" name="table" value="book"/>
Book Reviews
<input type="checkbox" name="table" value="brev"/> <br /><br />
<input type="submit" name="submit" value="Show Tables">
</p>
</fieldset>
</form>
</body>
I haven't written the PHP response yet as I am not sure where to start
Thanks
Here is a more complete answer of what you'd need to do:
Edited - Also included what your inputs need to look like.
<input type="checkbox" name="table[]" value="cust"/>Orders
<input type="checkbox" name="table[]" value="ord"/>Order Items
<input type="checkbox" name="table[]" value="itms"/>Books
<input type="checkbox" name="table[]" value="book"/>Book Reviews
<input type="checkbox" name="table[]" value="brev"/>
<?php
// Check that the values you're trying to access have actually been posted
// 'table' is the 'name' of your input
if (!empty($_POST['table'])) {
// If it's not empty then set the variable
$tables = $_POST['table'];
}
// If it is empty (Your form didn't submit this input)
else {
// end processing or return to the previous page
return false;
}
// You will now need to loop through the array
foreach ($tables as $table) {
switch($table) {
case 'cust':
// Run Cust SQL Query
break;
case 'ord':
// Run ord SQL Query
break;
case 'itms':
// Run itms SQL Query
break;
case 'book':
// Run book SQL Query
break;
case 'brev':
// Run brev SQL Query
break;
}
}
?>
And for reference why it's better to use a switch case instead of if/else in this situation:
Is "else if" faster than "switch() case"?
You can check whether a checkbox is checked in showtab.php. The 'value' attribute of the input will be posted to the 'name' attribute when the form is submitted. If the checkbox is blank it will post nothing.
<?php
if(isset($_POST['table']) && $_POST['table'] == 'cust') {
// Show table here
}
else {
// Do something else here.
}
?>
Your checkboxes will need unique names.
Hello can get all checked values with $_POST['table'] in your showtab.php.
why if conditon is not behaving properly in comparing values one which is coming from database and other coming from form via post..inside while loop of mysql fetch array
<?php
$right=0;
$wrong=0;
$result = mysql_query("SELECT * FROM mcq") ;
$total =mysql_num_rows($result);
echo "total questions are :".$total;
echo "<br>";
while($row = mysql_fetch_array($result))
{
$b=$row['id'];
$a=$_POST['a_'.$b];
$cor=$row['correct'];
if($a==$cor)
$right++;
else
$wrong++;
}
$a is coming from radio buttons from previous page and $cor is coming from database..i m comparing selected value of radio button with cor(correct answer of that value) which is coming from data base.. but condition is not executing rightly so please help me!!!!
The radio button just send one value. Your way to retrieve it is wrong, because you are basing in the ID, but the id should be used in the value.
Usually in HTML you should use some like this:
<input type="radio" name="myradio" value="item1" />
<input type="radio" name="myradio" value="item2" />
<input type="radio" name="myradio" value="item3" checked />
So, you should try to get the value from post:
$myvar = $_POST["myradio"];
Can you paste the radio button code?
I have a while loop generating information with a checkbox, I would like to update the database with the new "completed" value. How can I select the specific checkbox that is generated. Please help with showing me how I can grab the specific value of a checkbox and the task_name.
Thanks, Ryan
while ($row = mysql_fetch_array($query)){
$task_name = $row['task_name'] ;
$task_description = $row['task_description'];
$task_completed = $row['completed'];
$tasks .= '<div id="tasksBody">
<form action="" method="post">Completed? <input name="completed" type="checkbox" '.
($task_completed == 1?'checked="checked"':'').
' /><input type="submit" value="Update"><br /><br />
<b>'.$task_name.'</b><br /><br />'.$task_description.'<hr><br /></form></div>';
}
}
echo $tasks;
You need to name your input with something unique for the row, such as the task_name, or better, a database record ID.
Then when the user submits the form, you will use $_POST["YourTaskNameOrIDHere"] to check the value.
What you have currently calls all the check boxes the same thing.
EDIT: I'm sorry, you're isolating all of these in their own forms, I just realized that.
What you can add is an <input type="hidden" value="$task_name" name="TaskName" /> to the form, so you can look what the checkbox is corresponding to. Then, when the user submits the form, use $_POST["TaskName"] to find out the name of the task.
Add a hidden field to each of your forms containing the task_id
<form action="" method="post">
Completed?
<input name="completed" type="checkbox" <?=($task_completed == 1?'checked="checked"':'')?> value="1" />
...
<input name="task_id" value="<?=$task_id"?> type="hidden" />**strong text**
</form>
After submit:
if (isset($_POST['task_id']) { // form has been submitted
$task_id = $_POST['task_id'];
$completed = $_POST['completed'];
$sql = "UPDATE task SET task_completed=$completed WHERE task_id=$task_id LIMIT 1";
// code for updating database
// better use PDO or mysqli-* instead of old and deprecated mysql_*
}
I want to have a form that allows the user to choose what data to display from a table through checking the checkboxes. If the user wants only 2 columns to be shown, should only 2 columns be shown. I have my codes, but after I submit, it displays nothing.Here's my code:
<form name="form1" method="post" action="view_emp.php">
<p>Select Option
<input type="checkbox" name="number[]" value="name" />Name
<input type="checkbox" name="number[]" value="hired" />Date Hired
<input type="checkbox" name="number[]" value="basic" />Basic Pay
<input type="checkbox" name="number[]" value="incentives">Incentives
</p>
<input type="submit" name="Submit" value="Submit">
</form>
here's my php:
<?php
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('eis', $db) or die (mysql_error());
$employee = array();
foreach ($_POST['number'] as $employee) {
$number = mysql_real_escape_string($number);
$employee[] = "'{$number}'";
}
$sql = "select * from employees where type in (" .implode(", ", $number). ")";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
print $row['name'];
}
?>
i am a beginner in php and i need help from gurus and experts. thank you...
PHP's implode() and explode() functions might come in handy. You can easily turn your POST or GET attribute, 'number', into a comma-separated list by using implode($_POST['number']). This would be easy to store in one MySQL field, maybe a column in your user table.
If you want users to edit the form later, render the checkboxes with a loop and add a "checked" attribute to each checkbox element whose name exists in 'exploded' list (array) retrieved from your database.
This is basically serialization/deserialization. There are other ways to do it including serialize(), unserialize() or json_encode(), json_decode(). But since your data seems to be easily modeled as a basic list, you can keep it even simpler.
Below is an explanation of exactly what I want
On the submission form I have multiple checkboxs. when users click on checkbox a textbox appears next to it, where user input text. I want to insert the value of texbox in array and insert it in database with PHP-MYSQL.
for example checkbox1 is index 0=>valueoftextbox, checkbox2 is index 1=>value of texbox2
How can I achieve this?
Serialize received $_POST.
<input type="checkbox" name="smth[]" value="One" />
<input type="checkbox" name="smth[]" value="Two" />
<input type="checkbox" name="smth[]" value="Three" />
<?php
if (isset($_POST)) {
$smth = serialize($_POST['smth']);
$query = mysql_query("INSERT INTO table VALUES ('$smth')") or die(mysql_error());
}
A few pointer to get you going:
1) From PHP use:
echo "<pre>";
print_r($_POST);
echo "</pre>";
That way you can see WHAT is posted from your client-form.
2) Note that an unchecked checkbox ISN'T posted at all.
So if you have following checkbox in your form named "wantsnewsletter".
Then check that from PHP like this:
if (isset($_POST["wantsnewsletter"])){
// It was checked.
} else {
// It wasn't checked
}
Futhermore you just have to think up some sensible naming and do the inserts into you DB.