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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵