Forum Moderators: coopster

Message Too Old, No Replies

echoing php code

Hopefully a quick question

         

Sub_Seven

12:09 am on Jan 19, 2011 (gmt 0)

10+ Year Member



Hello everybody, it's me again, I hope this is a quick one though

Can you echo some php tags inside another echo?

This is what is happening and I can't fix it:

I had this:

<input type="hidden" name="com_date" <?php echo "value=\'".date("Y/m/d")."\'"; ?> />

and it was getting the date correctly and I would display it then with this:

<?php $date = $line['com_date']; echo date('m-d-Y', strtotime($date)); ?>

But then I needed to display the form that gets the date under an if statement so the whole form is inside echo '//here are a lot of line of code';

and after that everything works fine still except for the line the is getting the date, it looks kinda like this (I'm excluding a lot of unnecessary code):
<?php if($_SESSION['id']){
echo '<form action="/thankyou.php" method="post" onsubmit="return formValidator()">
<input type="hidden" id="status" name="status" value="0" />
<input type="hidden" name="com_date" <?php echo "value=\'".date("Y/m/d")."\'"; ?> />
<p>Name: <input type="text" id="name" name="name" /></p>
<p>City: <input type="text" id="city" name="city" /></p>
<p>State: <input type="text" id="state" name="state" /></p>
... a lot more lines here';}
else echo '<p>Please log in to be able to post comments!</p>'; ?>


So after this my date is being stored as 0000-00-00 in the DB and displayed as 12-31-1969 on the website.

Any suggestions? Thank you all and sorry for the unusual amount of questions lately :)

Shingetsu

3:46 am on Jan 19, 2011 (gmt 0)

10+ Year Member



If you want to echo a VARIABLE in an echo (I think that's what ur doing), you can't do it that way. The way to do it would be...
<?php echo "echo before variable" . $var . "echo after variable"; ?>

StoutFiles

4:08 am on Jan 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php if($_SESSION['id']){
?>
<form action="/thankyou.php" method="post" onsubmit="return formValidator()">
<input type="hidden" id="status" name="status" value="0" />
<input type="hidden" name="com_date" value="<?php echo date("Y/m/d"); ?>" />
<p>Name: <input type="text" id="name" name="name" /></p>
<p>City: <input type="text" id="city" name="city" /></p>
<p>State: <input type="text" id="state" name="state" /></p>
... a lot more lines here';}
<?php
else
{
?>
<p>Please log in to be able to post comments!</p>
<?php
}
?>

You don't need to be echoing large blobs of HTML. Just use PHP to define the if-else brackets and echo the occasional variable, it's much less messy.

Shingetsu

4:11 am on Jan 19, 2011 (gmt 0)

10+ Year Member



Are you sure php will continue on the function after it's been closed off?

StoutFiles

5:02 am on Jan 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes. A lot of people are unaware you can open and close PHP like that. A simple program to test it for yourself.

<?php

$var = 1;

if($var == 1)
{
?>
<b>yes</b>
<?php
}
else
{
?>
<i>no</i>
<?php
}

$var = 2;

if($var == 1)
{
?>
<u>yes</u>
<?php
}
else
{
?>
<strike>no</strike>
<?php
}
?>

Sub_Seven

5:31 am on Jan 19, 2011 (gmt 0)

10+ Year Member



I was actually aware you could do that, I just didn't know that in this case the HTML would be hidden/affected by the if statement inside the php tags, it actually worked just fine, thanks for the help :)