duanjiao8871 2016-04-21 13:52
浏览 88
已采纳

php和html单位转换器/选择选项值

I am a beginner to PHP and I thought it would be nice to learn it by starting simple projects. I chose to start with a simple unit converter.

I do not want to ask for a code, I simply would like to ask for some help with figuring out what methods to use or what steps should I consider next. Or if someone has a link to something similar it would be great, I couldn't find anything myself.

The idea is this: the user will input the value and the original unit, and then I want to print the value in a few different units. This is what I have so far:

html

<div id="converter">
    <h2>Unit converter</h2>
    Convert from:
    <form method="post" action="" class="inline">

    <select name="unit">
        <option value="km">kilometers</option>
        <option value="m">meters</option>
        <option value="dm">decimeters</option>
        <option value="cm">centimeters</option>
        <option value="mm">milimeters</option>
    </select>

    <input type="text" name="value" placeholder="input value here" autocomplete="off" required>     
    <input type="submit" value="convert"/>

    </form>

<?php
    $unit = isset($_POST['unit']) ? $_POST['unit'] : false;
    $value = isset($_POST['value']) ? $_POST['value'] : false; 

    /* the above two lines are copied from a solution for something similar 
that I found online. I know that they're not doing what I would like to do. 
I want $unit to hold the unit the user chose, and $value to hold the value the 
user put in. */

?>

</div>

So ideally I want to be able to input for example 20, choose "meters", click convert and then print "0.02 km, 200 dcm, 2000 cm, 20000 mm".

At the moment I'm stuck on this: how can I check which "original unit" got selected? In other words, how can I check if the user chose kilometers or meters or any other unit and edit my output accordingly?

And: how can I print the output?

I know I should somehow use $_POST but I'm not sure how.

Once again, I am not asking for code (although some would be nice ;) ), but for a pointer. I read a book about PHP but it's not very helpful with this.

Thank you in advance. Sorry if I made any mistakes, English isn't my first language.

  • 写回答

2条回答 默认 最新

  • doushih06137 2016-04-21 14:11
    关注

    coming from a die hard PHP coder to do these things with php is becoming legacy and outdated simply because the whole page has to reload when the user clicks submit. The above can be done much easier with an AngularJS app. This is where web is going is SPA (Single Page Apps) where the page doesn't have to reload and the results are instant.

    You would need good Javascript knowledge and MVC (Model, View Controller) programming concepts before you attempt to learn AngularJS. But it is worth it!

    I would suggest w3schools for a basic crash course. But googling would always produce lots of valuable resources.

    But back to answering your question:

    Your page name needs to be index.php, so that your form can post to index.php through the action attribute:

    <form method="post" action="index.php" class="inline">
    

    Then you can do the following to see what was posted to the page, this will print all the variables in nice readable display (and easy to comment out):

    echo "<pre>";print_r($_REQUEST);echo "</pre>";
    

    Then you will see $_REQUEST['unit'] should give you the var you need, then use a switch statement to do individual calcs for each selection

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号