Hello and Thanks in advance to anyone who can help me with this noob php query.

I am trying to create a "share this record" tool in which a User can send another User a link to a record by displaying all the users in the User Table with a query inside a
SELECT tag like below.

The sender should be able to select the 'receiver' from the Drop-Down and the form will send the email message to only the person selected. The problem I am having is that it sends an email to the intended recipient, but it sends one for each user in the User table. So three users, the recipient gets three emails.

Can someone help me figure out what I'm doing wrong?
Here is an example of what I've got going on.

//variable here
$mailmsg="Hi {$row['user_fname']} {$row['user_lname']}. Email message Blah Blah";


<select name='receiver'>
<option value='0' default>Please Specify Support Person to receive email...</option>";

$result=mysql_query("select user_id, user_fname, user_lname, user_email  from users");

while($row=mysql_fetch_assoc($result)){	

mail("$receiver", "Record Shared with You", $mailmsg,"FROM:me@foo.org");
	
	if($row['user_id']==$user_id){
	echo"<option value='{$row['user_email']} ' selected>{$row['user_id']}  {$row['user_fname']} {$row['user_lname']} ";
	}
	else{
		echo"
		<option value='{$row['user_email']}' >{$row['user_id']} {$row['user_fname']} {$row['user_lname']}
		
		";
		
	}
}
	echo"</select>

again, thanks in advance if anyone can help me with this.

-dottomm

For starters, you should have a proper form (Starting with <form>, ending with </form> and including a <input type=submit...>). If you aren't familiar with forms, go to W3schools and look for information there.

Second: Using a form works in two stages. First the form is displayed and the user enters the values and clicks the Enter button. Then, the receiving module (specified in the "action" on the Form statement) is activated and receives the input from the form and takes action on it. The receiving module can be the same one that contains the form (that is the default) but then you need logic in that module to determine if you are at the initial entry stage or if you are processing the input from the form. You can check for one of the variables in the form - you can use the variable created by the <input type=submit...> statement.

In your code, the program doesn't stop after it displays the form variable, it keeps right on going. It then processes the select statement but it doesn't have any conditions so it is going to select all records from the table. You will need a Where clause in the Select statement using the variable received from your form.

I'm not handing you the code but I've given you enough that you can figure it out. If it's too easy you won't learn anything.

I apologize for not posting more complete code. My intention was to only inquire about one portion of code.

Here is a more complete attempt of what I am doing.

//include connection file//
include 'common/useful_stuff.php';
if(!db_connect())
die();

//variables //Getvar a function
$user_id=getvar("user_id");;
$user_fname=getvar("user_fname");
$user_lname=getvar("user_lname");
$user_email=getvar("user_email");
$responsible=getvar("responsible");
$user_email = getvar("user_email");
$email=getvar("email");
$errmsg="";
$error_id=getvar("error_id");
$caller_id = getvar("caller_id");
$receiver = getvar("receiver");
$doit=getvar("doit");

//here is the message to be emailed//
$mailmsg="Hi {$row['user_fname']} {$row['user_lname']}. 
A  call has been shared with you. ";

if($doit == "yes"){	


header("LOCATION:share_confirm.php?user_email=$receiver");

}

include 'common/header.php';

// determine  user currently logged-in//
$result=mysql_query("Select * from users where user_id=$_uid");
	$row=mysql_fetch_assoc($result);
	//display logged in user name  and info//
	echo"
      <div id='dashboard'>
	<p> Administrator is {$row['user_fname']} {$row['user_lname']}</p>
</div>
	";
			
	echo"<div id='content'>
<h3>Share  call Details </span></h3>

<p><font color='red'><b>{$errmsg}&nbsp;</b></font></p>

<form action='{$_SERVER['PHP_SELF']}' method='post'>

<input type='hidden' name='doit' value='yes'>

//get and display name of all users in User Table//
<select name='receiver'><option value='0' default>Please Specify Support Person...</option>";
	$result=mysql_query("select user_id, user_fname, user_lname, user_email  from users");
	while($row=mysql_fetch_assoc($result)){

//here I am trying to capture the user to recieve the email w "receiver" and it works except I get multiple //emails	//
	mail("$receiver", "DR Call Record Shared with You", $mailmsg,"FROM:me@foo.org");
	
	if($row['user_id']==$user_id){
	echo"<option value='{$row['user_email']} ' selected>{$row['user_id']}  {$row['user_fname']} {$row['user_lname']} ";
	}
	else{
		echo"
		<option value='{$row['user_email']}' >{$row['user_id']} {$row['user_fname']} {$row['user_lname']}
		
		";
		
	}
}
	echo"</select> 
	</p>
<p>
	<input type='submit' name='submit' value='Share Call Details'>
</p>

</form>
</div>
";

To summarize:
I want to
* Get all Users first name, last name, email_address from User table.
* Display frist_name, last_name inside html form with <SELECT> tag, so that a user can select another user from drop-down and send email to that user.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.