Echo numerical value of a variable in a table

I’ve lost track of various attempts to print a number.
I have a table of client variables that displays client-entered values. All string variables display properly.
A numerical value does not display properly; eg. …

the variable 'Quantity" displays as ‘quantity’ instead of its value.

The code being used is

[php]

Quantity <?php echo "$quantity"?>[/php]

Was gibt???
usit

Is the $quantity variable set? If it is this should work:
[php]

Quantity <?= $quantity ?>[/php]

If it doesn’t work, please post more code.

JImL, Didn’t work, but your ‘more code’ comment led me to investigate a simplification of my longer program to see what I could learn about php. Here’s the result:

— My simple test script: —
[php]<?php

TEST_varformat.html $quantity:
<?php $quantity = $_POST['quantity']; echo 'Quantity = $quantity

'; echo End of TEST_varformat.html ,"

"; ?> ?>[/php]

— IE’s screen display of this script’s output after I enter the value ‘1’ … —

$quantity: [ 1 ]
"; ?>

Note: “;?> These 4 characters came from the last 2 lines of the php script. Do they have anything to do with the following error?

— IE’s Error mssg: ‘The webpage cannot be found’ —
The IE url window shows that IE is looking for …

http://www.domain.net/Apr2013_En/TmpTestFiles/<?php echo $_SERVER[‘PHP_SELF’] ?>

Thanks for the useful response,
usit

When using single quotes you must concatenate variables, you also don’t need to set a local variable, you can just use it directly. You should also escape all variables outputted to the user (XSS). Also your paragraph tags make no sense.

[php]<?php
$quantity = $_POST[‘quantity’];
echo 'Quantity = $quantity

';
echo End of TEST_varformat.html ,"

";
?>[/php]–>[php]<?php
echo '

Quantity = ’ . $_POST[‘quantity’] . ‘

’;
echo ‘

End of TEST_varformat.html

’;
?>[/php]

And you have an extra ?> at the end of the file
[php]

?>[/php]-->[php] [/php]

BIG eye opener, but am still confused.
Why doesn’t the opening <?php require a closer?
The same error, ‘The web page cannot be found.-’, is thrown. IE is now looking for

http://www.domain.net/Apr2013_En/TmpTestFiles/$_SERVER[?quantity=2

I have no idea how – ?quantity=2 – comes about in the $_SERVER argument.

The script is
[php]

TEST_varformat.html $quantity:
<?php echo Quantity = $quantity, "
"; echo End of TEST_varformat.html "
"; [/php]

My use of paragraph tags was simply to generate more white space.

More thanks,
usit

You should start php (<?php) when you are going to write php code, you should stop php (?>) when you go from php to another language (html, javascript, etc)

[php]

TEST_varformat.html " method="get"> $quantity:
Quantity = <?= $quantity ?>
End of TEST_varformat.html
[/php]

Didn’t know that. Thanks again JimL.
Now I’m down to one line of code that still throws an error on the call to $_SERVER[‘PHP_SELF’].
Here’s the latest try with the call as shown in Programming PHP on p.166…

[php]

TEST_varformat.html $quantity:
<?php echo Quantity = $quantity, "
"; echo End of TEST_varformat.html "
"; [/php]

… and the errorant file call produces …

http://www.domain.net/Apr2013_En/TmpTestFiles/<?quantity=1

Shouldn’t, ‘/%3C?quantity=1’, be something like, '$_SERVER[‘PHP_SELF’] ’ ?

The tunnel is getting shorter (I hope)
Thanks
usit

Tried various variations and have been around the web several times on this one, but still have no idea of what’s happening. The ‘<’ character in gets changed to ‘%3C’ when $_SERVER[‘PHP_SELF’] is called. Why? Here’s my script:
[php]<?php //TEST_varformat.html
if(isset($_POST[‘submit’]))
{
echo Quantity = $quantity, “
”;
}
?>

$quantity:
[/php]

An html 404 error is thrown and IE’s url window points to:
http://www.domain.net/Apr2013_En/TmpTestFiles/<?php $_SERVER[‘PHP_SELF’] ?>
showing the errant ‘%3C’.

Any ideas greatly appreciated.
Thanks
usit

Please add this to to the beginning of the php script, then you can start fixing stuff :slight_smile: You have a lot of syntax errors
[php]ini_set(‘error_reporting’, E_ALL);
ini_set(‘display_errors’, ‘1’);[/php]

No errors reported.
The form input seems to work, then the call to ‘PHP_SELF’ gets cobbled.
The ‘%3C’ mystery is discussed numerous times on the web with a common problem double calls to echo. I removed the echo in <?php $_SERVER['PHP_SELF'] ?>" METHOD=‘POST’> and got the same html error – so assumed it was not needed (although PHP texts all show it as <?php echo $_SERVER['PHP_SELF'] ?>" METHOD=‘POST’>).

This does throw errors, as said the syntax is invalid.

[php]<?php //TEST_varformat.html
ini_set(‘error_reporting’, E_ALL);
ini_set(‘display_errors’, ‘1’);

if(isset($_POST['submit']))  
	{         
	echo Quantity = $quantity, 	"<br />";
	}

?>

$quantity:
[/php]

Yep! Your correct, JimL. So am now searching the web for discussions of php syntax errors. Meanwhile,
I put single quotes around ‘Quantity = $quantity’ and reran the script getting the same html 404 code with corrupted ‘<’.
So submitted the script to piliapp.com/php-syntax-check which found “No syntax errors found in CODE”
Still stumped.

usit

[php] if(isset($_POST[‘submit’])) [/php]
You are not submitting a form field with the name submit, so this will never be true
[php] if(isset($_POST[‘quantity’])) [/php]

[hr]

[php]echo ‘Quantity = $quantity’, “
”;[/php]
As said before you need to concatenate when using single quotes.
[php]echo 'Quantity = ’ . $quantity . ‘
’;[/php]

[hr]

[php] [/php]
You should echo the value
[php] [/php]
the code above shows a shorthand echo, the full echo would look like this:
[php] [/php]
But since you want to post to the same file you don’t need to have an action at all, html forms will submit to the same url by default
[php] [/php]

[hr]

[php]$quantity:[/php]
Not sure what you intend to do here, it will just show up on the website as $quantity:

[hr]

Finished code
[php]<?php

if(isset($_POST[‘quantity’])) {
echo 'Quantity = ’ . $_POST[‘quantity’] . ‘

’;
}

?>

Quantity:


[/php]

WOW! What’a lesson. THANKS JimL
Now I have the element I need to fix my registration script.
YOUR ACCURATE, RAPID, AND INSIGHTFUF RESPONSES ARE REALLY APPRECIATED.
usit

I clicked ‘Topic Solved’, then looked back a few seconds later and found it showing ‘Topic not solved’.
Don’t know what to do about that.
And don’t know how to give you proper credit with kudos or other stars.
usit

The topic is marked as solved, appreciate your kudos ^^ No further starring necessary :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service