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_*
}
Related
I'm stuck on this. What I'm trying to do is this. A notification system. When I comment on something, I need it to get all other users who previously commented and send them a notification as well as the owner of the item, that the post has new activity.
Here's the issue. I'm populating a comment box underneath the post, and you write your comment, and hit enter, and it sends the post ID your commenting on, as well as your username and comment to a php script which calls a function to send a notification.
So, as hidden inputs, I'm pulling out from the database in the comment form, anyone who has previously commented on the post. Here's the issue, let's say User A has commented 3 times, the database would have him as a commenter 3 times, so his name will be pulled into the comment form as three different inputs. Here is some code for the mysql call that is pulling the previous commenters:
// Query the comments to see who has commented.
// Their username will be in the owner column.
// $postid is being pulled from the actual post id that you are commenting on.
// At this point the post content and previous comments are already
// processed, this is just processing the comment form.
$sql = "SELECT * FROM comments WHERE postid = '$postid'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)){
echo '<input type="hidden" name="commented[]" value="'.$row['owner'].'" />';
}
If user A has commented 4 times, the comment form would look something like this, then:
<form id="comment-form-##">
<input type="hidden" name="commented[]" value="usera" />
<input type="hidden" name="commented[]" value="usera" />
<input type="hidden" name="commented[]" value="usera" />
<input type="hidden" name="commented[]" value="usera" />
<input type="hidden" name="commented[]" value="userc" />
<input type="hidden" name="commented[]" value="userc" />
// And the id of the post you're commenting on
<input type="hidden" name="postid" value="24" />
// And then the actual input box for your comment:
<input type="text" name="comment" placeholder="Write a comment" />
So how can I pull the users who have previously commented, and skip their multiple listings in the database. Ideally, I'd like the comment form to look like this when complete:
<form id="comment-form-##">
<input type="hidden" name="commented[]" value="usera" />
<input type="hidden" name="commented[]" value="userc" />
// And the id of the post you're commenting on
<input type="hidden" name="postid" value="24" />
// And then the actual input box for your comment:
<input type="text" name="comment" placeholder="Write a comment" />
Hope there is a way to do this as this was my first inclination on how to notify previous commenters to the new activity. Thanks for any and all help I can get here.
Too much work.
SELECT DISTINCT owner
FROM comments
WHERE postid = ?
This is what I suggest, insert all owners into an array, then use array_unique on it so duplicates are removed, then run a foreach loop that would display the hidden form fields
$sql = "SELECT * FROM comments WHERE postid = '$postid'";
$result = mysqli_query($con,$sql);
$owners = array();
while($row = mysqli_fetch_array($result)) $owners[] = $row['owner'];
$owners = array_unique($owners);
foreach($owners as $owner) echo '<input type="hidden" name="commented[]" value="'.$owner.'" />';
Also, let me mention that I think this is not the best way to do what you want. These hidden form fields could be tampered with easily and you would be sending notifications to the wrong people.
Since you send the post_id as a hidden form field to a page that notifies people, I suggest you modify that page that so that it queries everyone who commented on that specific post_id except the one that is currently commenting and send them a notification instead of simply adding their ids in a hidden form field.
This should work
SELECT * FROM comments WHERE postid = '$postid' GROUP BY owner
Change your query to
$sql = "SELECT DISTINCT owner from comments where postid = '$postid'";
Then you'll get a list of the users who've commented without any duplicates.
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.
With this line of code, I add every selected checkbox's value into the database:
// ADD ALL TYPES TO PRODUCTIONLOG_TYPE TABLE
$x=1;
$values=array();
foreach($_POST['id'] as $x)
{
$AddToListQuery = "
INSERT INTO
productionlog_type
(productionlogid, typeid)
VALUES
('" . mysql_real_escape_string($_GET['productionlog']) . "', '". mysql_real_escape_string($x) ."')
";
mysql_query($AddToListQuery)or die("query fout " . mysql_error() );
}
I do this so I can echo out the checkbox checked if this is alreayd in the database.
The problem now is, when the user unchecks the checkbox and sends the form, it's not passing any value, right? So I can't delete it from the database...means that the checkbox remains checked.
What can I do about this?
If you know which of the checkboxes will be returned, you can filter out the ones you know something about and only the checkboxes that are not checked remain then.
You can also have a look at some processing of your page before you send the request, with JavaScript for example.
Or you can pass another hidden variable in your form that contains all the checkboxes you should receive in your PHP script. For example:
<input type="hidden" value="id_1,id_2,id_3" />
If you then receive this in your PHP script, you can see which of the ids (of the checkboxes) are expected.
EDIT
If you have:
<form method="POST">
<input type="hidden" name="checks" value="id1,id2" />
<input type="checkbox" name="id1" checked="checked" />
<input type="checkbox" name="id2" />
</form>
And then in PHP:
$arr = split(",", $_POST["checks"]);
foreach ($arr as $x){
if(isset($_POST[$x])){
// Add or update
}else{
// Remove
}
}
Note: I haven't tested the code, but it you just to get an idea on how it works
I am trying to write a dynamic form using PHP. I'd like to have a single webpage that contains two forms:
The upper form allows to search for an element in the mysql database, e.g., for a name
The lower form shows the data that is associated with this name in the database
If I press on the "Search" button of the upper form, then the the lower form is shown and the text fields are filled with data from the database that belong to this name. If I change the user name to some other value and press again "Search", then the data that is associated with the new record is shown and so on.
The lower form also has a button "Update" which allows to transfer changes made to the text boxes (in the lower part) to the database.
Now, I have the following problem: In my script I set initially the value of name (from the upper form) to "". When I then press the "Search" button, then the lower part of the form is shown and the corresponding data is shown in the lower part. When I then press the "Update" button, then the text field associated with name is set to the empty string. This is because in my script I set initially name to the "". I'd like that in this case the data entered in the upper form is not changed, i.e., it stays the same.
I guess, I am missing something here. There is probably an easy solution for this and I am doing something fundamentally wrong. It'd be great if you could help me.
That's what I tried... I deleted lots of details, but I guess that can give you an idea what I am trying to do. Notice that the whole code is in the file update.php.
<?php
function search_bus($mysql, $name)
{
// do some stuff here...
}
function update_bus($mysql, $b_id)
{
// do some stuff here...
}
// some global variables
$b_id = 0;
$username = ""; // username of business
// get b_id that corresponds to username
if (isset($_REQUEST['search']))
{
$b_id =0; // business id
if (isset($_POST['user']))
{
$username = $_POST['user'];
$b_id = search_bus($mysql, $username);
}
}
elseif(isset($_REQUEST['update']))
{
update_bus($mysql, $b_id);
}
?>
<h2>Search:</h2>
<form name="search_bus" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Username: <input type="text" name="user" value="<?= htmlentities($username) ?>"/>
<input type="submit" value="Suchen" name="search"/>
</form>
<?php
if($b_id != 0)
{
?>
<h2>Data:</h2>
<form name="business_design" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<-- some form follows here -->
<?php
}
?>
I think what you're missing is to create a HTML Hidden field to keep the value of Name variable.
<input type="hidden" name="name" value="<?php print $nameVar ?>" />
Add this input to both forms so you can keep the value no matter what button the user clicks.
Hope this helps.
Adding code to verify the
<h2>Search:</h2>
<form name="search_bus" method="post"
action="<?php echo $_SERVER['PHP_SELF'];?>">
Username: <input type="text" name="user" value="<?= htmlentities($username) ?>"/>
<input type="hidden" name="b_id" value="<?php print $b_id?>" />
<input type="submit" value="Suchen" name="search"/>
</form>
<?php if($b_id != 0) { ?>
<h2>Data:</h2>
<form name="business_design" method="post" action="<?php echo $_SERVER['PHP_SELF'];>">
<input type="hidden" name="b_id" value="<?php print $b_id?>" />
<-- some form follows here -->
<?php } ?>
Dont initialize $b_id if it already comes into the http request.
if (!isset($_POST['b_id']))
{
$b_id = 0;
}
else
{
$b_id = $_POST['b_id'];
}
This way you can alway remember the last selected value of b_id.
Hope this can help you.
Hello i have a html form in side php everything is working fine and i have just been showed how to do hidden fields from this website.
I have a submit button on each and every result letting the user pick what they want. When they press the submit button i want the info to be submitted and added to the database. But for some reason when the user click submit on item 1 it adds the last item into the database e.g item 6 ?? So there is 6 results and a submit button for each one so 6 buttons. When the user presses submit on number 1 item it submits number 6 for some resson.
<form method="post" action="buydo.php">
<label><br />
<br />
</label>
<p>
<?php
$sql = "SELECT * FROM sell
ORDER BY Pokemon_level ASC";
$res = mysql_query($sql) or die(mysql_error());
while ($v = mysql_fetch_array($res)) {
echo '
<div class="auction_box">
<img src="http://myrpg.net/new_rpg/'.$v['Pokemon_pic'].'" width="100" height="100"><br/>
£'.$v['price'].'<br/>
<label id="pokemonName'.$v['id'].'">'.$v['pokemon_name'].'</label><br/>
<label>Level '.$v['Pokemon_level'].'</level><br/>
<label>Exp '.$v['exp'].'</level><br/>
<label>Time Left:';
echo '</label>
<br/>
<input type="hidden" name="Name" value="'.$v['pokemon_name'].'">
<input type="hidden" name="level" value="'.$v['Pokemon_level'].'">
<input type="hidden" name="vid" value="'.$v['id'].'">
<input type="hidden" name="price" value="'.$v['price'].'">
<input type="hidden" name="exp" value="'.$v['exp'].'">
<input type="submit" id="'.$v['id'].'" class="buy_submit" value="Buy Now" /> </div>';
}
?>
</p>
<p> </p>
</form>
That is the select with the submit buttons for each result.
Then i insert the info they have chosen.
include 'config.php';
session_start() ;
$name = mysql_real_escape_string($_POST['Name']);
$Pokemon_level = mysql_real_escape_string($_POST['level']);
$idofpokemonsell = mysql_real_escape_string($_POST['vid']);
$price = mysql_real_escape_string($_POST['price']);
$exp = mysql_real_escape_string($_POST['exp']);
$sql = "SELECT * FROM `users` WHERE `username` = '" . $_SESSION['username'] . "'";
$result = mysql_query($sql) or die(mysql_error());
$values = mysql_fetch_array($result);
echo $values['money'] ;
if ( $values['money'] == $price ) {
echo "Give them the pokemon yay";
}
if ( $values['money'] > $price ) {
mysql_query("INSERT INTO `user_pokemon` (`pokemon`, `belongsto`, `exp`, `slot`, `level`) VALUES ('$name','" . $_SESSION['username'] . "','$exp','0','$Pokemon_level')") or die(mysql_error());
echo "Your money is over";
}
Like i say the insert only inserts item 6 no matter if u press item 1 - 5 it inserts 6 the level , exp, name everything of item 6
Since you display it all in one form, the latest values are overwriting the earlier ones. Try making each item its own form by moving your <form> and </form> tags inside your while loop
or try to track with jquery clicked button id and fill a hidden field with the clicked id
This is because your html is wrong. The values get overwritten
Basically you have to options:
render a form for each item
do it with a dropdown or radio buttons
With the second option you need to rewrite your code more, but it is more elegant, if you ask me. So what do you need to do.
Move the submit button out of the loop
Only generate code with the id and labels like this:
<option value="'.$v['id'].'"/>YOUR LABEL</option>
When the form is submitted, you have to retrieve the values corresponding to your the submitted id