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 html5 qrcode 扫描器
  • ¥15 爬取网页信息并保存需要完整代码
  • ¥15 一分十不等功分器阻设计问题,请问这个56Ω怎么得到的
  • ¥15 (标签-matlab)
  • ¥100 求看看这个数学建模,有偿
  • ¥15 深度学习目标检测现在框架加注意力的创新可以投几区?
  • ¥15 PdfiumViewer pdf转图片
  • ¥15 利用Java连接API接口总是出问题
  • ¥15 请教一个关于镜头标定,棋盘格格子大小的问题(畸变测试)