Drop down selection member list - php

Where the sent to input is, I want it to be a selection one. I cannot figure out how to make the selection one show usernames that have signed up to my system.
This is how I want it to look like: http://i.stack.imgur.com/wbDER.png
This keeps happening: http://i.imgur.com/XJv5h.png
The code:
<form action="pm.php?mode=sendit" method="post">
Send To:<br />
<?
$sql = mysql_query("select * from usr_users");
while($m = mysql_fetch_array($sql))
{ ?>
<SELECT name="sendto">
<OPTION value="<? echo("$m[username]");?>"><? echo("$m[username]");?></OPTION>
</select>
<? } ?>
Message Subject:<br />
<input type="text" name="subject" /><br /><br />
Message:<br />
<textarea name="privmsg" id="new_message" cols="40" rows="5"></textarea>
<script language="JavaScript">
generate_wysiwyg('new_message');
</script>
<br /><br />
<input type="hidden" value="<? echo("$_SESSION[usr_name]");?>" name="sentfrom" />
<input type="submit" value="Send" />
</form>
Does anyone know how to do this?
Thanks in advance!

See this http://www.w3.org/wiki/HTML/Elements/select

You can add users sign session to your databse when they are login, and then select them from your table, and add to option list. If they are logout you delete correct record, or put on cron script which delete sign session records if they are not activity for some time period. This version will be bad, if you have huge system.
When you already made a query to your data base and get them in variable, you should do it something like that:
foreach ($login_user as $item){
echo '<option value="'.$item['session_id'].'">'.$item['user_name'].'</option>';
}

Here's a quick example for you:
<?php
//query the db and select all usernames. Here, I'm just using an array for testing purpose
$usernames[] = 'abc';
$usernames[] = 'treyt';
$usernames[] = 'uiu';
$usernames[] = 'cvcvb';
$usernames[] = 'mty';
$usernames[] = 'opo';
//so $usernames array holds all usernames now.
?>
<html>
<body>
<form method="post" action="index.php">
Username: <select name="user_name">
<?php
//output the usernames
foreach($usernames as $user)
echo '<option>' . $user . '</option>';
?>
</select>
<input type="submit" value="Send" />
</form>
</body>
</html>
But if you have lot of users, then I would not suggest this method. Because, the list would be a long one. Instead, you could use AJAX to dynamically display a list of usernames(suggestions), while the user starts typing the name of the user in a text box.
Hope this will help you. :)

Related

Three page sequence of forms (checkboxes and text/number boxes) with foreach in php

I would like to pass variables through 3 pages. The 1st page asks the user which music genres they like (there will eventually be 20+ genres). The 2nd page asks the user to rank the genres they have selected and the 3rd page sorts and displays their ranking.
This first page asked the user to pick which genres they like:
<form id="genre" name="genre" method="post" action="musicsell.php">
<input type="checkbox" name="genre[]" value="Rap"/>Rap<br />
<input type="checkbox" name="genre[]" value="HipHop"/>HipHop<br />
<input type="checkbox" name="genre[]" value="RnB"/>RnB<br />
<input type="checkbox" name="genre[]" value="Rock"/>Rock<br />
<input type="checkbox" name="genre[]" value="Jazz"/>Jazz<br />
<p>
<input type="submit" value="Next">
<br />
</p>
</form>
This second asks them to rank (prioritize) the genres they have selected with 1 being the best:
<body>
The genre(s) you selected are: <br>
<form id="form1" name="form1" method="post" action="musicresults.php">
<?php
$name = $_POST['genre'];
if(isset($_POST['genre'])) {
foreach ($name as $genre){
?>
<input
type="number" required="required" id="<?php echo $genre ?>" name="music[<?php echo $genre ?>]" />
<?php echo $genre ?><br />
<?php
}
}
?>
<input type="submit" name="button" id="button" value="Submit" /></form>
</body>
The third and last page sorts and echos the results:
<?php
//Get the form results (which has been converted to an associative array) from the $_POST super global
$musicgenres = $_POST['music'];
//Sort the values by rank and keep the key associations.
asort($musicgenres, SORT_NUMERIC );
//Loop over the array in rank order to print out the values.
foreach($musicgenres as $music => $rank)
{
echo "$musicgenres is your $rank choice";
echo "<br>";
}
?>
It all works fine until the last page where I'm getting an "array to string conversion" error. Maybe I need to put in session variables but I'm not sure.
Any help would be appreciated.
It's exactly what the error says. It can't convert and array to a string.
Replace
echo "$musicgenres is your $rank choice";
with
echo "$music is your $rank choice";

Add Web form input to an Array

I cannot figure out how to go about keeping the data saved on $myArray after hitting Save. Everytime i do that, it replaces the other input.
I need to be able to save all user enters and save in text file. basically keep saving data on array and update file as well.
Any suggestions?
<!DOCTYPE>
<html>
<body>
<form action="list.php" method="POST">
Name: <input type="text" name="name[]" /><br />
Email: <input type="text" name="name[]" /><br />
<input type="submit" name="save" value="Submit" /><br />
<?php
$myArray = array();
if (isset($_POST['save']))
{
array_push($myArray,$_POST['name']);
}
print_r($myArray);
?>
</body>
</html>
Every time your user hits Submit, there is a new request sent to your script.
The variable $myArray is created on the first line of your PHP code, and is an empty array.
Variables do not persist across requests. I suggest you use either a cookie, or a session variable for this.
Example of the latter is here:
<?php
session_start(); // This has to be done before any data is sent to the client
session_regenerate_id(); // Good practice to prevent session hijacking
?>
<!DOCTYPE>
<html>
<body>
<form action="list.php" method="POST">
Name: <input type="text" name="name[]" /><br />
Email: <input type="text" name="name[]" /><br />
<input type="submit" name="save" value="Submit" /><br />
<?php
if( !isset( $_SESSION['myArray'] ) || !is_array( $_SESSION['myArray'] ) )
{
$_SESSION['myArray'] = array();
}
if (isset($_POST['save']))
{
$_SESSION['myArray'][] = $_POST['name'];
}
print_r($_SESSION['myArray']);
// Here you can write the contents of $_SESSION['myArray'] to a file.
?>
</body>
</html>
This way you will keep track of it in the text file:
<html>
<body>
<form action="list.php" method="POST">
Name: <input type="text" name="name[]" /><br />
Email: <input type="text" name="name[]" /><br />
<input type="submit" name="save" value="Submit" /><br />
<?php
$myArray = array();
if (isset($_POST['save']))
{
$f = file_get_contents("myfile.txt");
$myArray = split($f, "\n");
array_push($myArray,$_POST['name']);
file_put_contents("myfile.txt", implode($myArray, "\n"));
}
print_r($myArray);
?>
</body>
</html>
(You may want to use PHP_EOL [or whatever that constant is - I always have to look it up] in place of "\n")
Since HTML is by nature transactional you do not keep track of ANYTHING except sessions, databases, files - any variables you have start over empty each time you load your page again.

JQuery / PHP Dropdown Variable For TextArea Value

I have a functioning "Account Notes" page that allows the user to add notes to their account for their own uses. I want to allow my admins to input notes into user accounts without logging in. I have a Dropdown on the Admin page that allows the admins to select a user. After selecting the user I want to echo the notes already in their notes section into the TextArea below and allow an update to the notes.
function getNotes(){
global $id;
$result = query("SELECT * FROM `extras` WHERE `userid`=".$id);
while ($row = mysql_fetch_assoc($result)){
return $row['notes'];
}
}
<form action="update.php" method="post">
<h4 class="widgettitle title-primary">Account Notes</h4>
<br/>
<textarea rows="20" scroll="auto" class="txt" name="notes" style="width: 80%;"/><?=getNotes();?></textarea>
<div style="text-align:center;"><button class="btn btn-primary">Update</button></div>
<input type="hidden" name="formtype" value="updatenotes" />
<input type="hidden" name="redir" value="notes.php" />
</form>
</form>
</div>
</div><!--row-fluid-->
All of the above code works correctly, so there are no issues above. My issue lies below...
The following code is what I have on my admin.php page for the user notes addition code...
<div id="addusernotes">
<form action="update.php" method="post">
<h4 class="widgettitle title-primary"><center>Add User Notes</center></h4><br/>
<?php
echo '<select id="user" name="user">';
$user = query("SELECT * FROM `users` ORDER by `user`");
if($user && mysql_num_rows($user))
{
while ($row = mysql_fetch_assoc($user))
{
echo '<option value="'.$row['id'].'">'.$row['user'].'</option>';
}
}
echo '</select>';
?><br/>
Notes:<br/>
<textarea rows="5" scroll="auto" class="txt" name="adminusernotes" style="width: 80%;"/></textarea><br/>
<input type="submit" name="adminnotes" value="Add Notes"><br/><br/>
<input type="hidden" name="formtype" value="adminusernotes" />
<input type="hidden" name="redir" value="admin.php" />
</form>
What JQuery do I need to add to the admin.php file to allow the Notes from the DB to display in the TextArea? I have been stumped on this for nearly 3 days.....Any help will be more than appreciated!!!
FYI....I have intentionally left out some of my "proprietary code for security reasons...
To alter a textarea's content with jQuery, you simply use:
$("#element-id-here").val("whatever value...");
But give your textarea an ID first. And you can change the value to a variable too.
If you want PHP to echo the textarea contents:
<textarea><?php echo "contents here"; ?></textarea>

How to not clear input fields in php form?

I have a webpage that uses php and has a bunch of input fields in which a user enters some data and I turn the input into an SQL statement to query some database. I successfully parse the input fields and put the SQL statement together but when they click the "submit" button, all the input fields get cleared. How can I make it so these input fields don't get erase every time the user clicks "submit"?
Store them in local variables like
<?php
$name = '';
$last = '';
if(isset($_POST['post_me'])) {
$name = $_POST['post_me'];
$last = $_POST['last'];
//Process further
}
?>
<form method="post">
<input type="text" name="name" value="<?php echo $name; ?>" />
<input type="text" name="last" value="<?php echo $last; ?>" />
<input type="submit" name="post_me" />
</form>
Something like this should work.
<?
if(!isset($_POST['stackoverflow'])){
$txt = "";
} else {
$txt = $_POST['stackoverflow'];
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="" method="post">
<input type="text" name="stackoverflow" value="<?= $txt ?>">
</form>
</body>
</html>
You need to check if the POST values (assuming you're using POST) are already set when the page loads (because they have already been submitted). If they are set, echo their contents into the value attributes of the form fields, or set checked=checked etc. depending on the field types.
This has always worked for me:
<input type="text" name="somename" value="<?php echo htmlspecialchars($_POST['somename']); ?>">
The key is to store the post values in session and display it using value tag.
Example:
HTML:
<form action="index.php" method="post">
<input type="text" name="name" placeholder="First Name"
value="<?php
if (isset($_SESSION['name'])) {
echo $_SESSION['name'];
}?>" >
<input type="submit" name="submit">
</form>
PHP:
session_start();
if (isset($_POST['submit'])) {
$name=$_POST['name']);
$_SESSION['name']=$name;
}
<?php
if(isset($_POST['submit'])) {
$decimal = $_POST['decimal'];
?>
<form action="" method="post">
<input type="number" name="decimal" required value="<?php echo (isset($decimal)) ? $decimal: ''?>">
</form>
Just use your $_POST['decimal'] into your input value
Have you used sessions and Cookies in PHP??
You can store the values in session on form submit before updating in Database for a user, then check if there is a session for user and value is set for field output the value else output null.
You just have to make sure the HTML input value has a php echo statement of the appropriate php variable for that field.
Simplest answer
`<form method="post" onsubmit="return false">`
coding your input with PHP is dangerous even if with htmlentities - hope this helps

HTML submit forms using PHP

I am learning PHP now, so pardon my silly question which I am not able to resolve.
I have created a simple web form where in I display the values entered by a user.
function submitform()
{
document.forms["myForm"].submit();
} // in head of html
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" onclick="submitForm()" />
</form>
and res.php contains:
foreach($_POST as $field => $value)
{
echo "$field = $value";
}
When I click on the submit button, I just get a blank page without any values from the form. Can anyone please let me know what am I missing?
There's no need for the javascript. This should do:
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" />
</form>
Let's start with fixing errors:
JavaScript is case-sensitive. I see that your function name is submitform and the form's onclick calls submitForm.
The javascript is not really necessary from what you've shown us, I would try this on a single php page and see if it works:
Create a test.php file for test purpose:
<?php
if($_POST){
foreach($_POST as $key=>$value){
echo "$key: $value<br />";
}
}else{
<form action="test.php" method="post">
<input type="text" value="1" name="name1" />
<input type="text" value="2" name="name2" />
<input type="submit" value="submit" name="Submit" />
</form>
}
?>
If it does work, slowly work your way into your current form setup to see what is breaking it. If it doesn't work, there's something larger at play.
There are 2 things you should do now.
Remove the JavaScript function to submit the form. It's not required (or necessary). The default behavior of a submit button is to... well... submit. You don't need to help it with JavaScript.
Enable error display by using error_reporting(E_ALL).
After you do both things, you should be able to debug and assess the problem much more easily.
Put your php code inside php tags!
<?php
foreach($_POST as $field => $value)
{
echo $field ." = ." $value.'<br />';
}
?>
If you do
<?php
print_r($_POST);
?>
what do you get?
If this still doesn't work, does your server parse php?
Create the file test.php and access it directly http://localhost/test.php or whatever your URL is
<?php
echo 'Hello';
?>
if this doesn't work..it's a whole diferent problem
You can submit an HTML form using PHP with fsubmit library.
Example:
require_once 'fsubmit.php';
$html = "<form action ='res.php' method='post'><input type='text' name='name'/></form>";
$form = new Fsubmit();
$form->url = 'http://submit_url_here.com';
$form->html = $html;
$form->params = ['name'=>'kokainom'];
$response = $form->submit();
echo $response['content'];

Categories