Adding users with hidden inputs using selection box in php - php

I'm trying to allow users to add other users on a form via a selection box. So, a user can select another user from a drop down selection, press add user, and they're added to an array. The user can select another user, and that second user should be added to this array. Right now, each time a user is added the original user disappears. I tried using hidden inputs but I'm not having any luck.
On formprocessing.php
//this generates a list of users. The code to actually generate the users is omitted
<center><h3>Invite Friends</h3></center><br>
<form method="post">
<tr><td>Friends:</td><td><select name ="friend"><Option value="">Friends<? echo $selection; ?></Select></td>
<input type="hidden" name="hiddenFriends" value="<? echo $friends; ?>"/>
<td><input type="submit" name="submitFriend" value="Add Friend"/>
</form>
On form.php
//i'm trying to create an array of users which are added. When completed, they'll be added to the db
<?
if(isset($_POST['submitFriend'])){
if(count($_POST['hiddenFriends']) > 0){
$friend = $_POST['friend'];
array_push($friends,$_POST['friend']);
}
else{
$friends = array('');
array_push($friends,$_POST['friend']);
}
}
?><input type="hidden" name="hiddenFriends" value="<? echo $friends; ?>"/><?
print_r($friends);
?>

Dude you trying to add PHP array into html input. Its IMPOSSIBLE.
but don't worry you can use explode and implode functions.
<?
$friends = array();
if(isset($_POST['submitFriend'])){
if(strlen($_POST['hiddenFriends']) > 0){
$friends = explode(',',$_POST['hiddenFriends']);
}
$friends[] = $_POST['friend'];
}
?>
<input type="hidden" name="hiddenFriends" value="<? echo implode(',',$friends); ?>"/>

Related

Allow user to modify a selected element of a task list

I have an html form (and accompanying php code) that allows user to input task names, which are stored in a PHP array $task_list[].
I also have, below the task list, a dropdown from which users can select a desired value. I want to create a button modify which will allow users to change the value they have selected.
For instance, if the task list is:
Get up
Take a bath
Go for a jog
Go to school
Come back home
A user should be able to select "Go to school", and having done so, click Modify and change that to "Go to work", or something else.
I've made the select dropdown list work and can access the selected value and store it in a variable $selected. But I am completely flummoxed as to how I can allow users to change the value in the $task_list array.
My code–
task_list.php:
<form action="." method="post" >
<!//select dropdown>
<select name='taskid'>
<?php foreach( $task_list as $id => $task ) : ?>
<option value="<?php echo $id; ?>">
<?php echo $task; ?>
</option>
<?php endforeach; ?>
</select>
<?php foreach( $task_list as $task ) : ?>
<input type="hidden" name="tasklist[]" value="<?php echo $task; ?>">
<?php endforeach; ?>
<br />
<label> </label>
<input type="hidden" name="action" value='modify'>
<input type=submit value="Modify Task">
<br />
<label> </label>
<input type="hidden" name="action" value='promote'>
<input type=submit value="Promote Task">
</form>
index.php:
//get selected value
function getSelected()
{
if(isset($_POST['taskid']))
{
$myvalue = $_POST['taskid'];
}
else
{
$myvalue = $task_list[0];
}
return $myvalue;
}
//skipping ahead to modify field (all other buttons work perfectly):
case 'modify':
$myval = getSelected(); //$myval returns index of selected item
$selected = $task_list[$myval]; //selected item
break;
I just want to allow the user to click modify and change the value they selected in the $task_list array.
Thank you.

How to get access to dynamically generated radio buttons using POST

I have some dynamically generated HTML radio buttons based on fetching each character from a database table as show below:
<form name="form" id="myForm" method="POST" action="process.php">
<?php
$stmt = $this->registry->db->getDB()->prepare("SELECT * FROM characters");
$stmt->execute();
if($stmt->rowCount() > 0)
{
foreach($stmt as $row)
{
?>
<input type="radio" name="<?php print $row[0];?>" value="<?php print "value" . $row[1];?>">
<?php
}
}
?>
</form>
How would I get access to each selected radio button that is generated in my PHP script using $_POST[], because the name attribute of each radio button is created dynamically, I can't get my head around how I would access each radio button value in PHP so I can process the form.
Note that each radio button generated will be unique so they will not be grouped with the same name.
One easy way would be to create an array, in this case data[]:
<input type="radio" name="data[<?php print $row[0];?>]" value="<?php print $row[1];?>">
Then to get them:
if(isset($_POST['data'])) {
foreach($_POST['data'] as $name => $value) {
echo "$name = $value<br/>";
}
}

Trying to update dates in database with dynamically created forms

So i've created an administration page that creates X-number of forms based on how many users we have in our database, each of which have a submit button next to them to submit changes to the dates we have in our DBs. My problem is that when I want to get the value of what gets posted I can't extract exactly what I need from what gets posted. It gets saved into an array called and when I print_r the array I get exactly what I want, which is:
[1] => "whatever date they typed in"
(obviously the 1 changes depending on which item they changed the date of)
I need be able to query my datebase by:
UPDATE users SET subdate="whatever they typed in" WHERE id="the array reference number"
I know exactly what I need to do, I'm just not as familiar with SQL as i'd like to be, so any help would be greatly appreciated. Thanks in advance.
Code for reference:
<div class="form-section grid12" id="changedates">
<h1>Change Dates</h1>
<?php
$query = mysql_query("SELECT * FROM users WHERE admin='y'");
?>
<table>
<?php
while($row = mysql_fetch_assoc($query)) {
?>
<tr>
<td>
<h5><?php echo $row['displayname'];?></h5>
</td>
<td>
<form action="" method="POST">
<input type="text" name="subdate[<? echo $row['id'] ?>]" value="<?php echo $row['submissiondate'];?>">
<input type="text" name="nextupdate[<? echo $row['id'] ?>]" value="<?php echo $row['nextupdate'];?>">
</td>
<td>
<input type="submit" value="Set Date" name="setdate">
</form>
</td>
<?php
}
?>
</table>
</div>
You could use foreach...
foreach ($_POST[nextupdate] as $rowId => $time)
{
// do db update
}
Edit: Just realised you have more than one input per form.
Why not name each input with an array name:
<input type="text" name="form_data[<?= $row_id ?>][subdate]">
<input type="text" name="form_data[<?= $row_id ?>][nextupdate]">
In PHP:
foreach ($_POST[form_data] as $rowId => $values)
{
$subdate = $values[subdate];
$nextupdate = $values[nextupdate];
// do SQL stuff
}

Grabbing specific variable from while loop for form submit

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_*
}

Two forms with multiple submit buttons in single PHP file

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.

Categories