Welcome! Log In Create A New Profile

Advanced

[PHP] embedding php inside of php

Posted by Tim Dunphy 
Tim Dunphy
[PHP] embedding php inside of php
July 01, 2012 02:10AM
Hello,

I am trying to get the hang of php using some examples that I found
in a book. I've been making progress lately, but one thing has me a
bit stumped.


In an HTML form that I am echoing through PHP I would like to embed
smaller chunks of php in the code like so:


echo '<br /><br />
<form method="post" action="sendemail.php">
<label for="subject">Subject of email:</label><br />
<input id="subject" name="subject" type="text" value="<?php
echo $subject;?>"><br />
<label for="elvismail">Body of email:</label><br />
<textarea id="elvismail" name="elvismail" rows="8"
cols="40">"<?php echo $text;?>" </textarea><br />
<input type="submit" name="Submit" value="Submit" />
</form>';



If I do embed the smaller chunks of php in the form the way I've just
shown you the script instantly breaks and the web page shows only a
white screen of death.

And I see this in the web server logs

[Sat Jun 30 19:12:54 2012] [notice] child pid 7769 exit signal
Segmentation fault (11)


If I remove the smaller bits of php as I show here the web page starts
working again


echo '<br /><br />
<form method="post" action="sendemail.php">
<label for="subject">Subject of email:</label><br />
<input id="subject" name="subject" type="text"><br />
<label for="elvismail">Body of email:</label><br />
<textarea id="elvismail" name="elvismail" rows="8"
cols="40"></textarea><br />
<input type="submit" name="Submit" value="Submit" />
</form>';


Here, I'll show the entire script so you can get a better sense of what it does

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">;
<html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Make Me Elvis - Send Email</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<img src="blankface.jpg" width="161" height="350" alt=""
style="float:right" />
<img name="elvislogo" src="elvislogo.gif" width="229" height="32"
border="0" alt="Make Me Elvis" />
<p><strong>Private:</strong> For Elmer's use ONLY<br /><br
Write and send an email to mailing list members.</p>

<?php

error_reporting(E_ALL);
ini_set('display_errors', 'On');

if (isset($_POST['Submit'])) {

$from = '[email protected]';
$subject = $_POST['subject'];
$text = $_POST['elvismail'];
$output_form = "false";

if (empty($subject) && empty($text)) {
echo 'You forgot the email subject and body.<br />';
$output_form = 'true';

}


if (empty($subject) && !empty($text)) {
echo 'You forgot the email subject.<br />';
$output_form="true";

}

if ((!empty($subject)) && empty($text)) {

echo 'You forgot the email body text.<br />';
$output_form="true";


}

} else {

$output_form = 'true';

}



if ($output_form == 'true') {


echo '<br /><br />
<form method="post" action="sendemail.php">
<label for="subject">Subject of email:</label><br />
<input id="subject" name="subject" type="text"><br />
<label for="elvismail">Body of email:</label><br />
<textarea id="elvismail" name="elvismail" rows="8"
cols="40"></textarea><br />
<input type="submit" name="Submit" value="Submit" />
</form>';


} else {

$dbc = mysqli_connect('127.0.0.1', 'admin', 'secret

', 'elvis_store')
or die('Error connecting to MySQL server.');

$query = "SELECT * FROM email_list";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');

while ($row = mysqli_fetch_array($result)){
$to = $row['email'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$msg = "Dear $first_name $last_name,\n$text";
mail($to, $subject, $msg, 'From:' . $from);
echo 'Email sent to: ' . $to . '<br />';
}

mysqli_close($dbc);

}


?>

</body>
</html>


I was hoping that someone might be out there that could understand
this problem and point out where I'm going wrong.

Thanks!

--
GPG me!!

gpg --keyserver pool.sks-keyservers.net --recv-keys F186197B

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Bastien
Re: [PHP] embedding php inside of php
July 01, 2012 02:30AM
Bastien Koert

On 2012-06-30, at 8:00 PM, Tim Dunphy <[email protected]> wrote:

> Hello,
>
> I am trying to get the hang of php using some examples that I found
> in a book. I've been making progress lately, but one thing has me a
> bit stumped.
>
>
> In an HTML form that I am echoing through PHP I would like to embed
> smaller chunks of php in the code like so:
>
>
> echo '<br /><br />
> <form method="post" action="sendemail.php">
> <label for="subject">Subject of email:</label><br />
> <input id="subject" name="subject" type="text" value="<?php
> echo $subject;?>"><br />
> <label for="elvismail">Body of email:</label><br />
> <textarea id="elvismail" name="elvismail" rows="8"
> cols="40">"<?php echo $text;?>" </textarea><br />
> <input type="submit" name="Submit" value="Submit" />
> </form>';
>
>
>
> If I do embed the smaller chunks of php in the form the way I've just
> shown you the script instantly breaks and the web page shows only a
> white screen of death.
>
> And I see this in the web server logs
>
> [Sat Jun 30 19:12:54 2012] [notice] child pid 7769 exit signal
> Segmentation fault (11)
>
>
> If I remove the smaller bits of php as I show here the web page starts
> working again
>
>
> echo '<br /><br />
> <form method="post" action="sendemail.php">
> <label for="subject">Subject of email:</label><br />
> <input id="subject" name="subject" type="text"><br />
> <label for="elvismail">Body of email:</label><br />
> <textarea id="elvismail" name="elvismail" rows="8"
> cols="40"></textarea><br />
> <input type="submit" name="Submit" value="Submit" />
> </form>';
>
>
> Here, I'll show the entire script so you can get a better sense of what it does
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">;
> <html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en">
> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
> <title>Make Me Elvis - Send Email</title>
> <link rel="stylesheet" type="text/css" href="style.css" />
> </head>
> <body>
> <img src="blankface.jpg" width="161" height="350" alt=""
> style="float:right" />
> <img name="elvislogo" src="elvislogo.gif" width="229" height="32"
> border="0" alt="Make Me Elvis" />
> <p><strong>Private:</strong> For Elmer's use ONLY<br /><br
> Write and send an email to mailing list members.</p>
>
> <?php
>
> error_reporting(E_ALL);
> ini_set('display_errors', 'On');
>
> if (isset($_POST['Submit'])) {
>
> $from = '[email protected]';
> $subject = $_POST['subject'];
> $text = $_POST['elvismail'];
> $output_form = "false";
>
> if (empty($subject) && empty($text)) {
> echo 'You forgot the email subject and body.<br />';
> $output_form = 'true';
>
> }
>
>
> if (empty($subject) && !empty($text)) {
> echo 'You forgot the email subject.<br />';
> $output_form="true";
>
> }
>
> if ((!empty($subject)) && empty($text)) {
>
> echo 'You forgot the email body text.<br />';
> $output_form="true";
>
>
> }
>
> } else {
>
> $output_form = 'true';
>
> }
>
>
>
> if ($output_form == 'true') {
>
>
> echo '<br /><br />
> <form method="post" action="sendemail.php">
> <label for="subject">Subject of email:</label><br />
> <input id="subject" name="subject" type="text"><br />
> <label for="elvismail">Body of email:</label><br />
> <textarea id="elvismail" name="elvismail" rows="8"
> cols="40"></textarea><br />
> <input type="submit" name="Submit" value="Submit" />
> </form>';
>
>
> } else {
>
> $dbc = mysqli_connect('127.0.0.1', 'admin', 'secret
>
> ', 'elvis_store')
> or die('Error connecting to MySQL server.');
>
> $query = "SELECT * FROM email_list";
> $result = mysqli_query($dbc, $query)
> or die('Error querying database.');
>
> while ($row = mysqli_fetch_array($result)){
> $to = $row['email'];
> $first_name = $row['first_name'];
> $last_name = $row['last_name'];
> $msg = "Dear $first_name $last_name,\n$text";
> mail($to, $subject, $msg, 'From:' . $from);
> echo 'Email sent to: ' . $to . '<br />';
> }
>
> mysqli_close($dbc);
>
> }
>
>
> ?>
>
> </body>
> </html>
>
>
> I was hoping that someone might be out there that could understand
> this problem and point out where I'm going wrong.
>
> Thanks!
>
> --
> GPG me!!
>
> gpg --keyserver pool.sks-keyservers.net --recv-keys F186197B
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

You can't have an echo inside and echo. Properly quote and concatenate the additional echo and you'll be fine
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tim Streater
Re: [PHP] embedding php inside of php
July 01, 2012 02:10PM
On 01 Jul 2012 at 01:00, Tim Dunphy <[email protected]> wrote:

> I am trying to get the hang of php using some examples that I found
> in a book. I've been making progress lately, but one thing has me a
> bit stumped.
>
> In an HTML form that I am echoing through PHP I would like to embed
> smaller chunks of php in the code like so:
>
>
> echo '<br /><br />
> <form method="post" action="sendemail.php">
> <label for="subject">Subject of email:</label><br />
> <input id="subject" name="subject" type="text" value="<?php
> echo $subject;?>"><br />
> <label for="elvismail">Body of email:</label><br />
> <textarea id="elvismail" name="elvismail" rows="8"
> cols="40">"<?php echo $text;?>" </textarea><br />
> <input type="submit" name="Submit" value="Submit" />
> </form>';


You don't need the nested echoes. Just concatenate instead:

echo '<br /><br />
<form method="post" action="sendemail.php">
<label for="subject">Subject of email:</label><br />
<input id="subject" name="subject" type="text" value="' . $subject . '"><br />
<label for="elvismail">Body of email:</label><br />
<textarea id="elvismail" name="elvismail" rows="8"
cols="40">"' . $text . '" </textarea><br />
<input type="submit" name="Submit" value="Submit" />
</form>';

In short you're doing this :

echo 'Some HTML text here ' . $subject . ' some more HTML text ' . $text . ' and finally other stuff here';

--
Cheers -- Tim

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Yared Hufkens
Re: [PHP] embedding php inside of php
July 01, 2012 02:10PM
If you are using PHP 5.4 or short_open_tag is 1, you can also do this:

Some HTML text here<?=subject?>some more HTML text<?=$text?>and finally other stuff here


Am 01.07.2012 14:01, schrieb Tim Streater:
> On 01 Jul 2012 at 01:00, Tim Dunphy <[email protected]> wrote:
>
>> I am trying to get the hang of php using some examples that I found
>> in a book. I've been making progress lately, but one thing has me a
>> bit stumped.
>>
>> In an HTML form that I am echoing through PHP I would like to embed
>> smaller chunks of php in the code like so:
>>
>>
>> echo '<br /><br />
>> <form method="post" action="sendemail.php">
>> <label for="subject">Subject of email:</label><br />
>> <input id="subject" name="subject" type="text" value="<?php
>> echo $subject;?>"><br />
>> <label for="elvismail">Body of email:</label><br />
>> <textarea id="elvismail" name="elvismail" rows="8"
>> cols="40">"<?php echo $text;?>" </textarea><br />
>> <input type="submit" name="Submit" value="Submit" />
>> </form>';
>
> You don't need the nested echoes. Just concatenate instead:
>
> echo '<br /><br />
> <form method="post" action="sendemail.php">
> <label for="subject">Subject of email:</label><br />
> <input id="subject" name="subject" type="text" value="' . $subject . '"><br />
> <label for="elvismail">Body of email:</label><br />
> <textarea id="elvismail" name="elvismail" rows="8"
> cols="40">"' . $text . '" </textarea><br />
> <input type="submit" name="Submit" value="Submit" />
> </form>';
>
> In short you're doing this :
>
> echo 'Some HTML text here ' . $subject . ' some more HTML text ' . $text . ' and finally other stuff here';
>
> --
> Cheers -- Tim
>
>
>
Daniel Brown
Re: [PHP] embedding php inside of php
July 02, 2012 05:10PM
On Sat, Jun 30, 2012 at 8:00 PM, Tim Dunphy <[email protected]> wrote:
> Hello,
>
> I am trying to get the hang of php using some examples that I found
> in a book. I've been making progress lately, but one thing has me a
> bit stumped.
>
>
> In an HTML form that I am echoing through PHP I would like to embed
> smaller chunks of php in the code like so:
>
>
> echo '<br /><br />
> <form method="post" action="sendemail.php">
> <label for="subject">Subject of email:</label><br />
> <input id="subject" name="subject" type="text" value="<?php
> echo $subject;?>"><br />

You're trying to open PHP tags within a PHP code block. Drop the
nested <?php echo $subject; ?> and replace it with:

'.$subject.'

Thus:

echo '<br/><br/>
<form method="post" action="sendmail.php">
<label for="subject">Subject of email </label><br/>
<input id="subject" name="subject" type="text"
value="'.$subject.'"><br/>

--
</Daniel P. Brown>
Network Infrastructure Manager
http://www.php.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Matijn Woudt
Re: [PHP] embedding php inside of php
July 02, 2012 06:20PM
Hi,

<rant>
First a message to the ones that have responded before me:
You're correct about the nested php tags that are not doing what the
OP wanted, but you might want to take a closer look at the error
that's in the logs. In ANY CASE PHP SHOULD NOT CRASH. What if the OP
really wanted to print PHP tags inside it's textbox? That's perfectly
valid. I tested the code, and it works fine on my machine, even though
I get PHP tags inside the output.
</rant>

On Sun, Jul 1, 2012 at 2:00 AM, Tim Dunphy <[email protected]> wrote:
> Hello,
>
> I am trying to get the hang of php using some examples that I found
> in a book. I've been making progress lately, but one thing has me a
> bit stumped.
>
>
> In an HTML form that I am echoing through PHP I would like to embed
> smaller chunks of php in the code like so:
>
>
> echo '<br /><br />
> <form method="post" action="sendemail.php">
> <label for="subject">Subject of email:</label><br />
> <input id="subject" name="subject" type="text" value="<?php
> echo $subject;?>"><br />
> <label for="elvismail">Body of email:</label><br />
> <textarea id="elvismail" name="elvismail" rows="8"
> cols="40">"<?php echo $text;?>" </textarea><br />
> <input type="submit" name="Submit" value="Submit" />
> </form>';
>
>
>
> If I do embed the smaller chunks of php in the form the way I've just
> shown you the script instantly breaks and the web page shows only a
> white screen of death.
>
> And I see this in the web server logs
>
> [Sat Jun 30 19:12:54 2012] [notice] child pid 7769 exit signal
> Segmentation fault (11)
>

What version of Operating System, Webserver(Apache?) and PHP are you
using? It seems like there is a bug in your PHP, so if you're not yet
at the latest version, you might want to upgrade your Webserver/PHP.

- Matijn

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login