dongyongmin5711 2017-04-11 23:07 采纳率: 100%
浏览 122
已采纳

如何从PHP中的HTML表单按钮获取值,然后写入php文本

I'm new to php and html and therefore need to get the basics of both php and html.

I'm making a mini calculator and so far I have only made the layout. I need help in knowing that how do you get the value from a button pressed(from HTML form) in your php code section (on the same page), process it in php and then return it back to be shown on the text box of the HTML form.

    <?php
    /**
     **need help here
     */


    ?>

    <html lang="en">
    <head>
        <title>Title</title>
    </head>

    <body>


    <form name="calculator" method="post" action="index.html">

        <input type="text"  name="disp" value="" /> <br> <br>
        <input type="button" name="one" value="1">
        <input type="button" name="two" value="2">
        <input type="button" name="three" value="3">
        <input type="button" name="bksp" value="DEL"> <br>

        <input type="button" name="four" value="4">
        <input type="button" name="five" value="5">
        <input type="button" name="six" value="6">
        <input type="button" name="reset" value="AC"> <br>

        <input type="button" name="seven" value="7">
        <input type="button" name="eight" value="8">
        <input type="button" name="nine" value="9">
        <input type="button" name="equal" value="="> <br>

        <input type="button" name="zero" value="0"> <br>

        <input type="button" name="plus" value="+">
        <input type="button" name="minus" value="-">
        <input type="button" name="mult" value="*">
        <input type="button" name="divide" value="/">




    </form>
    </body>
    </html>
  • 写回答

2条回答 默认 最新

  • ds342222222 2017-04-11 23:22
    关注

    First, you can't process a PHP code inside html. So save for file as .php and leave your action attribute blank since you want to process the POST request within the same page. And a type of button for your input element will not going to submit your form. Changed it to submit

    <form name="calculator" method="post" action="">
       <input type="submit" name="one" value="someString">
    

    Second, PHP needs to know or should have a reference of what specific input field you're trying to get value from. So the name attribute will be the reference. But you MUST know the exact name you're trying to pull or it will throw you an undefined index error, PHP will not know that for you.

    $string = ""; //declare your variable as global to be used later
    if(!empty($_POST)){
       $string = $_POST['one']; //assuming you know that you are getting the value of input field `name=one`
    }
    

    From there, you can now use the variable string to display in your HTML input/textarea.

    <textarea><?php echo $string; ?></textarea>
    <input type="text" value="<?php echo $string;?>">
    

    Note:

    This is for PHP only, this kind of scenario is mostly done via javascript, but it depends on your preference or purpose though

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器