Forum Moderators: coopster

Message Too Old, No Replies

Insert variable into variable inside of textarea. Yeah, crazy huh.

         

jbearnolimits

2:59 am on Oct 11, 2022 (gmt 0)

Top Contributors Of The Month



I didn't know if this should be in php or database section because it deals with both. Anyway, I have page 1 sending information from a form to page 2. This includes the variable $HomeAddress. On page 2 I have an sql query to get the value of Task1 which is stored in the database (a separate table from the one I am inserting into) and then insert Task1 and HomeAddress into the table.

The value of Task1 in the other table is something like: Hi, this is my address $HomeAddress if you want to drop by.

Now, I am trying to output Task1 on another page inside of a textarea. But I want it to show: Hi, this is my address 555 Some st. if you want to drop by.

But I am only getting this in the textarea: Hi, this is my address $HomeAddress if you want to drop by.

How do I insert that value into the textarea variable with php?

Just some notes on the code: It's Mysqli PDO. PHP 7. Here is the relevant code:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$id = $_POST['id'];
$HomeAddress = $_POST['HomeAddress'];
$ListName= $_POST['ListName'];
}
$sql = "SELECT Task1 FROM AutoPilotTaskLists WHERE ListName = '$ListName'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$Task1 = $row["Task1"];
}
} else {
echo "0 results";
}
if (!empty($Task1)) {
$sql = "INSERT INTO Tasks (HomeAddress, Task1) VALUES ('$HomeAddress', (SELECT Task1 FROM AutoPilotTaskLists WHERE ListName = '$ListName'))";
$result = $conn->query($sql);
}


The following is what I have for the textarea:

<textarea name=\"Task1\" id=\"Task1\" rows=\"5\" cols=\"45\" value=\"" . $row["Task1"]. "\">" . $row["Task1"]. "</textarea>

jbearnolimits

6:48 pm on Oct 11, 2022 (gmt 0)

Top Contributors Of The Month



It appears I need to not use the textarea. But even just having it echo the content of the database it gives the same issue. So it appears the problem is the database is escaping the PHP code on its own automatically. Is there a way to prevent that?

robzilla

8:08 pm on Oct 11, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



(SELECT Task1 FROM AutoPilotTaskLists WHERE ListName = '$ListName')

There's no need to run this query again within the INSERT if you've already stored that value in $Task1.

If $row["Task1"] contains the following string:
Hi, this is my address $HomeAddress if you want to drop by.

Then echoing that string is not going to have $HomeAddress be replaced by the value of the $HomeAddress variable, they're separate things so PHP is just going to output the string as-is. To replace "$HomeAddress" in the string with the value of the $HomeAddress variable, you could use something like str_replace().

echo str_replace('$HomeAddress', $HomeAddress, $row['Task1']);

To avoid confusion with the dollar sign, you could use something like #HomeAddress# instead.

jbearnolimits

9:31 pm on Oct 11, 2022 (gmt 0)

Top Contributors Of The Month



THANK YOU! I asked this on stack overflow and all people were telling me was that I didn't know what I was doing. Which is exactly as helpful as telling someone asking how to do something that they don't know how to do it. That place is toxic!

jbearnolimits

2:57 am on Oct 12, 2022 (gmt 0)

Top Contributors Of The Month



It also allowed me to use it in the textarea. And just to help anyone that comes across the problem like I did here is the code:

<div class=\"InnerDatabaseRowType2HomeAddress\"><textarea name=\"Task1\" id=\"Task1\" rows=\"5\" cols=\"45\">";?><?php echo str_replace("#HomeAddress#","" . $row["HomeAddress"]. "","" . $row["Task1"]. "");
?><?php echo "</textarea> </div>


I know I could clean it up as far as the escapes and the mix of php and html. But I'm just happy it works for now.

robzilla

8:44 am on Oct 12, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Glad you got it working! And while code doesn't always have to be super clean, I'm not sure I understand why you added all those concatenations of empty strings:
str_replace("#HomeAddress#","" . $row["HomeAddress"]. "","" . $row["Task1"]. "");
vs.
str_replace("#HomeAddress#", $row["HomeAddress"], $row["Task1"]);

If you have a variable $numbers as the string "123" and you do "0" . $numbers . "4" you get "01234", a concatenation of "0" to "123" to "4". If you don't put anything between the quotes, as in your code above, you're also not concatenating anything, so you wouldn't need that operator either. It's like saying "take this variable and add nothing to it" vs. simply "take this variable".

jbearnolimits

1:49 pm on Oct 12, 2022 (gmt 0)

Top Contributors Of The Month



Yeah, I see that now. Thanks. I think there was a reason at one point during the whole project and I have just been doing a copy and paste. Then I forgot the reason and started thinking the " . $row[variable] . " was actually the syntax lol.

It's been so long since I did any of this that I am having to learn all over. Only now it seems like there's 101 ways to write the same thing. So long story short I got confused lol.

jbearnolimits

4:00 am on Oct 19, 2022 (gmt 0)

Top Contributors Of The Month



So out of curiosity what would you do if you had two different things to search for and replace with two different variables? I ask because I see how the structure works now but this is only with one variable. Would this be how you would work with multiple variables?

<?php echo str_replace(array('#HomeAddress#', '#Name#'), array($row["HomeAddress"], $row["FirstName"]), $row["Task1"]);?>

robzilla

9:49 am on Oct 19, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes!

You can also write an array like so: ['#HomeAddress#', '#Name#']

May make things a bit easier to read, without the embedded round brackets.

robzilla

11:05 am on Oct 19, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes!

You can also write an array like so: ['#HomeAddress#', '#Name#']

May make things a bit easier to read, without the embedded array() function and rounded brackets.

And <?= is shorthand for <?php echo so you could write it like this:

<?= str_replace(['#HomeAddress#', '#Name#'], [$row["HomeAddress"], $row["FirstName"]], $row["Task1"]); ?>