Page 1 of 1

How to pre-fill a Textarea with data from a MySQL Query

Posted: Wed Nov 19, 2014 10:29 pm
by cecilchampenois
I want to pre-fill a Textarea control with data returned from a query. The Textarea has a name and a value. I am not at work right now, but how do I pass the query result, which goes into a memory variable, into the Textarea control?

The query is something like this:
$Email_Address="select email_address from logins where login_id = $_SESSION['myusername']";

Re: How to pre-fill a Textarea with data from a MySQL Query

Posted: Wed Nov 19, 2014 11:44 pm
by requinix
You don't "pass" the result. There is no textarea "control".

The PHP code outputs HTML. Put the $Email_Address into that output so it appears inside the textarea's tags.

Re: How to pre-fill a Textarea with data from a MySQL Query

Posted: Thu Nov 20, 2014 7:12 am
by cecilchampenois
Well, I am "new" to all of this, so please bear with me.

Here's the current code:

...other code preceding which started a Switch:
case 'SendEmailToMe':
$lcSQL = "select email_address from logins where login_id = " . $_SESSION['myusername'];
$lcEmail_Address = mysql_query($lcSQL);
break;
....more code in between....

echo '<input type="checkbox" name="btnUpdate" value="SendEmailToMe" checked > Send email to self?<br>';
echo '<input type="submit" name="btnUpdate" value="Email" size="20">';
echo '<input type="text" size=60 name="EmailList" id="EmailList"/><strong>--Email List (sep by commas)</strong><br/><br/><br/><hr/>';

Are you saying that I should use the resulting value from the above SQL statement, $lcEmail_Address, as my value for the above TEXTBOX, such as:

echo '<input type="text" size=60 name="EmailList" id="EmailList" value="$lcEmail_Address"/><strong>--Email List (sep by commas)</strong><br/><br/><br/><hr/>';

I can see that my problem is that I've been a desktop developer for so long that I don't think "web".

Re: How to pre-fill a Textarea with data from a MySQL Query

Posted: Thu Nov 20, 2014 11:52 am
by requinix
Almost. $lcEmail_Address is not a value but a "resource" you can use to get one. You have to call a function like mysql_fetch_array() to get a row of data (even if that row has only one value in it), and then you can get the email address itself.

Code: Select all

case 'SendEmailToMe':
    $lcSQL = "select email_address from logins where login_id = " . $_SESSION['myusername'];
    $lcEmail_Address = mysql_query($lcSQL);
    list($Email_Address) = mysql_fetch_array($lcEmail_Address);
    break;
You also can't use variables inside single-quoted strings, and when outputting something into HTML you (almost) always need to escape it, so

Code: Select all

echo '<input type="text" size=60 name="EmailList" id="EmailList" value="' . htmlspecialchars($Email_Address, ENT_QUOTES, 'UTF-8') . '"/><strong>--Email List (sep by commas)</strong><br/><br/><br/><hr/>';

Re: How to pre-fill a Textarea with data from a MySQL Query

Posted: Mon Dec 08, 2014 4:32 pm
by cecilchampenois
I got pulled off of PHP for a while and had to write some EXCEL Automation programs. Lucky me!

Okay, I am trying to get back on track with this. I just noticed, while using Firefox's Firebug that the session variable, $_SESSION['user_email'], is already holding the user's Email Address. For example, I see in single quotes my own email address, since I am the user who logged in. So, this is what I needed. I just need to figure out how to do two things:

(1) Display that value for the email address inside the Email textbox when the form is initialized and,
(2) Have that value set in the variable for that textbox already.

How do I do the two things listed above?

Remember, I am a novice to writing PHP code. Most of my code is written in Visual FoxPro which is a database language.

What exists right now is the following code, prior to my changing it:

Code: Select all

echo '<input type="checkbox" name="btnUpdate" value="SendEmailToMe" checked > Send email to self?<br>';
// $_SESSION['user_email'] // I might need this soon.
echo '<input type="submit" name="btnUpdate" value="Email" size="20">';
echo '<input type="text" size=60 name="EmailList" id="EmailList" value="' . htmlspecialchars($lcEmailAddress, ENT_QUOTES, 'UTF-8') . '"/><strong>--Email List (sep by commas)</strong><br/><br/><br/><hr/>';
Okay, what I did to make this work was this: I only changed the third input type textbox to use $_SESSION['user_email'], instead of $lcEmailAddress. Now, a further complication. The person requesting this is now asking that I do not show/display the user's email address in the textbox, but that we do everything behind the scenes so that the user never sees their own user email address. The memory variable for the text box is EmailList. Would I "pass" the value from the Session memory variable for the email address to the EmailList? Somehow, I think you will say not to do that.

This worked, but as I said above, now the user doesn't want to see their email address in the box.
echo '<input type="text" size=60 name="EmailList" id="EmailList" value="' . htmlspecialchars($_SESSION['user_email'], ENT_QUOTES, 'UTF-8') . '"/><strong>--Email List (sep by commas)</strong><br/><br/><br/><hr/>';

Re: How to pre-fill a Textarea with data from a MySQL Query

Posted: Mon Dec 08, 2014 7:24 pm
by requinix
Is it just me or did you answer those couple questions by yourself?

So as far as I know, all that's left is to not include the email address in there. Since the email address is already in the session and you know that the contact form (or whatever) should always send a copy (or whatever) to that email address, you can just... leave it out. Ignore it. Form doesn't have to know about it.
Somewhere you have code that deals with the various recipients, right? It takes that list from the form and splits it apart on commas to get the email addresses into one list. All you have to do is stick the user's email into that list too.

Re: How to pre-fill a Textarea with data from a MySQL Query

Posted: Tue Dec 09, 2014 7:18 am
by cecilchampenois
You got it. Yeah, I think I talked it out to myself. Web programming isn't like desktop programming. Yes, there is a variable called EmailList or something close to that,. So I can "pass" that session variable to that memory variable, EmailList. Sometimes, we don't know what to do until we explain it to someone else, then the lights come on.

Thank you for your help.

Cecil
requinix wrote:Is it just me or did you answer those couple questions by yourself?

So as far as I know, all that's left is to not include the email address in there. Since the email address is already in the session and you know that the contact form (or whatever) should always send a copy (or whatever) to that email address, you can just... leave it out. Ignore it. Form doesn't have to know about it.
Somewhere you have code that deals with the various recipients, right? It takes that list from the form and splits it apart on commas to get the email addresses into one list. All you have to do is stick the user's email into that list too.

Re: How to pre-fill a Textarea with data from a MySQL Query

Posted: Tue Dec 09, 2014 1:06 pm
by requinix
cecilchampenois wrote:You got it. Yeah, I think I talked it out to myself. Web programming isn't like desktop programming. Yes, there is a variable called EmailList or something close to that,. So I can "pass" that session variable to that memory variable, EmailList. Sometimes, we don't know what to do until we explain it to someone else, then the lights come on.
Basically. It'd be easier to say how to do that if I could see the code.

Re: How to pre-fill a Textarea with data from a MySQL Query

Posted: Thu Dec 11, 2014 12:59 pm
by cecilchampenois
requinix wrote:
cecilchampenois wrote:You got it. Yeah, I think I talked it out to myself. Web programming isn't like desktop programming. Yes, there is a variable called EmailList or something close to that,. So I can "pass" that session variable to that memory variable, EmailList. Sometimes, we don't know what to do until we explain it to someone else, then the lights come on.
Basically. It'd be easier to say how to do that if I could see the code.
Here's the ultimate code, which worked:

Code: Select all

	// 12/09/2014 Cecil Champenois. Set the email up to send to Sxxxs if it is Client 40023, or 40024.
	// Added additional conditions to account for the fact that the user may not enter an email address.
	if (($_SESSION['active_client'] == 40024) || ($_SESSION['active_client'] == 40023)) {
	    if ($_POST['EmailList']=="") {
			// There is nothing in the Session email address variable, EmailList.
			$_POST['EmailList']="sxxxx@xp.com" . "," . $_SESSION['user_email'];
		} else {
			// 12/09/2014 Cecil Champenois. In the below case, the user has entered an email address.
			// The email address which the user entered will precede the following Sears and User's own email addresses.
			$_POST['EmailList']=$_POST['EmailList'] . ",sxxxx@xp.com" . "," . $_SESSION['user_email'];
		}
	} else { // 12/09/2014 Cecil Champenois.
	// 12/09/2014 Cecil. New line of code to pass user_email over to EmailList.
		if ($_POST['EmailList']=="") {
			$_POST['EmailList']=$_SESSION['user_email'];
		} else {
			$_POST['EmailList']=$_POST['EmailList'] . "," . $_SESSION['user_email'];
		}
	}
	
	$email_to=$_POST['EmailList'];
Thank you for helping me to think through this, Requinix!

Re: How to pre-fill a Textarea with data from a MySQL Query

Posted: Fri Jan 30, 2015 7:39 am
by Celauran
jeffreyappel wrote:You have to do the task you mentioned in the following way:

Code: Select all

<?php
$Email_Address="select email_address from logins where login_id = $_SESSION['myusername']";
$QueryEmail_Address = mysql_query($Email_Address) or die(mysql_error());
$ArrayEmail_Address = mysql_fetch_array($QueryEmail_Address);
?>
<textarea name="" ><?php echo $ArrayEmail_Address["email_address"]; ?></textarea>
mysql_query() or die()

Nope. Nope nope nope.