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条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改