Forum Moderators: coopster

Message Too Old, No Replies

php, new lines and form variables

troubles with echo in an if statement

         

Webartist

4:48 am on May 27, 2003 (gmt 0)

10+ Year Member



I've got an integer variable from a post, $_POST["prodqty8352"]. It might be empty or have a numeric value.
If it has a number, I write a new line to a form, like so:

if ( $_POST["prodqty8352"] > 0) {
echo '<input type="Hidden" name="prodnum" value="8352">' ;

So far, so good. Now, I want to insert a new line, but every time I try, it just gives me a parser error or prints the actual backslash n. That's the first problem.

The second one is I want to print the value of that variable in this line:
<input type="Hidden" name="prodqty" value="$_POST["prodqty8352"]"> in place of $_POST["prodqty8352"].

I've tried several things, but need help.

jatar_k

5:02 am on May 27, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hey Webartist, I am taking a wild guess and going the php type answers. ;)

What is the actual line (and possibly the line or two before) that is causing the error.

value="$_POST["prodqty8352"]"

you can't actually do this. The page, first of all, has to be parsed by php and then you need to echo/print the variable value. I usually use something like this

value="<?= $_POST["prodqty8352"]?>"

that initiates the php parser and therefore puts the value of the variable into the value attribute of your hidden element.

Webartist

5:51 am on May 27, 2003 (gmt 0)

10+ Year Member



Actually, I found that I can get the variable value to print with the whole line I want by doing this:

<?php
if ( $_POST["prodqty8352"] > 0) {
echo ('<input type="Hidden" name="prodqty" value="') ;
echo ($_POST["prodqty8352"]) ;
echo ('">');
}
?>

However, I still can't get the @#$! new line to give me a carriage return before or after that line.

I've tried:

echo ("\n");
echo \n;
echo '\n';

Any ideas?

Webartist

5:59 am on May 27, 2003 (gmt 0)

10+ Year Member



I finally got it! I jumped out of PHP and put in two carriage returns and Voila! a new line was born.

WibbleWobble

12:34 pm on May 27, 2003 (gmt 0)

10+ Year Member



\r\n does carriage return, newline, I think. (\r being simply carriage return)

daisho

1:22 pm on May 27, 2003 (gmt 0)

10+ Year Member



Here is a slightly cleaner solution. To do variable substitutions you need to enclose in double quotes not single quotes.

I am not sure where you want the newline but "\n" works great for me everywhere I've used it. Remember that you _WILL NOT_ see them on the webpage as you will need to use "<BR>" but if you view source you will see the new lines just fine.

<?php
if ( $_POST["prodqty8352"] > 0) {
echo ("<input type='Hidden' name='prodqty' value='${_POST['prodqty8352']}'>") ;
}
?>

daisho.