Jump to content

[SOLVED] How do I use variables inside of a 'here doc'?


javacious

Recommended Posts

Hello all, first time poster!  :)

 

I'm having some difficulties with creating a form for a website. The idea here is I have a variable called $ip that I've stuck the form-posters IP address into. I am now trying to stick that same variable as a value into a hidden input field...

 

So how do I insert a variable INSIDE of a here doc?

 

Example:

 

<?php

print<<<_FORM

 

<input name="ipaddy" type="hidden" value="<?php echo $ip ?>"/>

 

<textarea cols="40" rows="7" name="message"></textarea>

 

<input name="submit" type="submit" value="Click to Send"/>

 

_FORM;

?>

 

The above example doesn't work.... the area in red needs help.

This should work though if the above doesn't:

 

echo "<form method=' ' action=' ' >\n

<input name='ipaddy' type='hidden' value='$ip' />\n

<textarea cols='40' rows='7' name='message'></textarea>\n

<input name='submit' type='submit' value='Click to Send'/>";

<?php

print <<<_FORM

<input name="ipaddy" type="hidden" value="$ip"/>

 

<textarea cols="40" rows="7" name="message"></textarea>

 

<input name="submit" type="submit" value="Click to Send"/>

 

_FORM;

?>

 

Don't use chaking's last way.  HTML tags should have " " around their values, not ' '.  Some browsers don't like it.

This is the correct way:

 

<?php

$variable = "I'm a variable. Howdy!";

echo <<<HTML
This is some text.
The following is a variable: $variable
Enjoy.
HTML;
?>

 

[17:39] [root@geeklan : ~]# php heredoc.php
This is some text.
The following is a variable: I'm a variable. Howdy!
Enjoy.

 

Another example (extended):

 

<?php

$variable = "I'm a variable. Howdy!";

echo <<<HTML
This is some text.
The following is a variable: $variable
Enjoy.
HTML;

$variable2 = <<<VAR
I'm text in a variable assigned using HEREDOC! Wow!
And I'm the original variable: $variable
VAR;

echo $variable2;

?>

one more idea, sorry im late to the party, make your form tag read <form method="post" action="'.$_SERVER['PHP_SELF'].'"> and the value="ip" should become a key in the array $_POST accessed by $_POST['ip']

 

$_SERVER has many different array keys, dont remember them all but that is how I process my forms

 

new to php but makes sense to me

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.