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 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错