Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Ela Elwertowska
Ela Elwertowska
1,183 Points

Empty field - getting value from user - and returning it to user

Problem: After making conversion "kg-pounds" in php according to Alena's tutorial, I wanted to enhance it by making some interaction with any user using <input> field from the chapter about posting emails. My first try:

<?php
$kilogram = trim(filter_input(INPUT_POST,"kilogram",FILTER_SANITIZE_STRING));
$pounds=$kilogram/0.45359237;
echo $pounds pounds;
?>
<input type="text" id="kilogram" name="kilogram" value="<?php if (isset($kilogram)) { echo $kilogram; } ?>" />

resulted in nothing...

Generally speaking, Is it possible to do it in php? or should I try it with the help of Javascript language?

3 Answers

Hi there,

Your calculations are indeed working, but you just need to fix that line of code that echoes it out.

PHP can absolutely handle just about any math or calculations you can give it, as long as any integers stay within bounds.

What you're probably getting under the hood is: Parse error: syntax error, unexpected 'pounds' (T_STRING), expecting ',' or ';'

To fix this, you just need to concatenate correctly:

echo $pounds . " pounds";

Did that help at all?

Ela Elwertowska
Ela Elwertowska
1,183 Points

Well, a little bit. Still... I wonder maybe PHP needs some trigger... perhaps reloading page... I put some value inside box... made F5 for reloading page and... well, I got something.... 0 pounds next to the empty box... It's a progress... but far to expected results

Ah, you would need to add a Submit button - I assumed there was one there. Then you would just have the form action refer to itself:

<form method='post' action=''>
<input type="text" id="kilogram" name="kilogram" value="<?php if (isset($kilogram)) { echo $kilogram; } ?>" />
<input type='submit' value='Submit'>

Then when you hit the submit button, your PHP code will run and have a value associated with $kilogram because that's what will be submitted by the form. Right now, there's simply no value associated with $kilogram because the only way to assign a value to it is to POST one from the form via a SUBMIT button.

Try that and see, let me know!

Ela Elwertowska
Ela Elwertowska
1,183 Points

That's exactly what I needed. Thanks Matthew.

My pleasure!! I'm glad it worked!