Jump to content

How to create a dropdown menu with items from a DB


techiefreak05

Recommended Posts

How would i do this? I have a message system on my site, and the user has to manually enter the username o send the message to, what if they spell it wrong? then it would just take up space on my DB, so instead of a text box, id like a dropdown menu that has all the usernames on my site.
Link to comment
Share on other sites

I made this: but it dont work, it displays "Username does not exist" always, and it sends the message anyway

[code]<?php
if($_POST['sendMess']){
$to = $_POST['to'];
$from = $_SESSION['username'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$date = date("D M j G:i:s T Y");

function userExists($username){
  global $conn;
  $q = "select username from users";
  $result = mysql_query($q,$conn);
  return (mysql_numrows($result) > 0);
}

  if(userExists($_POST['to'])){
mysql_query("INSERT INTO `messages` ( `from` , `to` , `message` , `date` , `status` , `subject`)
VALUES ('$from', '$to', '$message', '$date', 'no', '$subject');") or die("There was an error sending your message");
mysql_query("INSERT INTO `outbox` ( `from` , `to` , `message` , `date` , `status` , `subject`)
VALUES ('$from', '$to', '$message', '$date', 'no', '$subject');") or die(mysql_error());
echo "<font color=red>Your Message has been sent!</font>";
}
  }else{
echo "That username does not exist";
}
?>
<form action="" method="post">
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td ><font color=black>To:</font></td><td><input type="text" name="to" size="35"></td></tr>
<tr><td ><font color=black>Subject</font></td><td><input type="text" name="subject" size="35"></td></tr>
<tr><td ><font color=black>Message:</font></td><td><textarea name="message" rows="10" cols="55"></textarea></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="sendMess" value="-Send Message-"></td></tr>
</table>
</form>
[/code]
Link to comment
Share on other sites

Try this for the userExists function...

[code=php:0]function userExists($to){
  global $conn;
  $q = "select username from users where name = $to";
  $result = mysql_query($q,$conn);
  return (mysql_numrows($result) > 0);
}[/code]

I dunno how your database is set up, so I don't know if it's "where name = $to" or "where username = $to" or whatever..
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.