How to pre-fill a Textarea with data from a MySQL Query
Moderator: General Moderators
-
cecilchampenois
- Forum Commoner
- Posts: 47
- Joined: Thu Nov 06, 2014 10:29 am
- Location: Gilbert, Arizona
- Contact:
How to pre-fill a Textarea with data from a MySQL Query
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']";
The query is something like this:
$Email_Address="select email_address from logins where login_id = $_SESSION['myusername']";
Cecil Champenois
Re: How to pre-fill a Textarea with data from a MySQL Query
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.
The PHP code outputs HTML. Put the $Email_Address into that output so it appears inside the textarea's tags.
-
cecilchampenois
- Forum Commoner
- Posts: 47
- Joined: Thu Nov 06, 2014 10:29 am
- Location: Gilbert, Arizona
- Contact:
Re: How to pre-fill a Textarea with data from a MySQL Query
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".
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".
Cecil Champenois
Re: How to pre-fill a Textarea with data from a MySQL Query
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.
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
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;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/>';-
cecilchampenois
- Forum Commoner
- Posts: 47
- Joined: Thu Nov 06, 2014 10:29 am
- Location: Gilbert, Arizona
- Contact:
Re: How to pre-fill a Textarea with data from a MySQL Query
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:
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/>';
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/>';
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/>';
Last edited by cecilchampenois on Thu Dec 11, 2014 1:00 pm, edited 1 time in total.
Cecil Champenois
Re: How to pre-fill a Textarea with data from a MySQL Query
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.
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.
-
cecilchampenois
- Forum Commoner
- Posts: 47
- Joined: Thu Nov 06, 2014 10:29 am
- Location: Gilbert, Arizona
- Contact:
Re: How to pre-fill a Textarea with data from a MySQL Query
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
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.
Cecil Champenois
Re: How to pre-fill a Textarea with data from a MySQL Query
Basically. It'd be easier to say how to do that if I could see the code.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.
-
cecilchampenois
- Forum Commoner
- Posts: 47
- Joined: Thu Nov 06, 2014 10:29 am
- Location: Gilbert, Arizona
- Contact:
Re: How to pre-fill a Textarea with data from a MySQL Query
Here's the ultimate code, which worked:requinix wrote:Basically. It'd be easier to say how to do that if I could see the code.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.
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'];
Cecil Champenois
Re: How to pre-fill a Textarea with data from a MySQL Query
mysql_query() or die()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>
Nope. Nope nope nope.