doupa2871 2019-05-11 08:50 采纳率: 100%
浏览 94

如何在运行代码时分离html代码和php代码并获得相同的效果?

I'm making a calculator to a webbsite. I have my php and html code on the same file but need to put them in separate files (a requirement for my course). I have tried using <form action = "cal.php">, and even though it works it puts the result on a different page and not on the exact spot on the screen i want it to show up.

<!DOCTYPE html>

<html>

<head>
  <meta charset="UTF-8">

  <title>Something</title>
</head>

<body>

  <form>
    <input type="text" name="num1" placeholder="Tal 1">

    <input type="text" name="num2" placeholder="Tal 2">

        <select name="operator"><!-- used to choose which operator to calculate with from the php code -->

            <option>Operation One</option> <!-- add -->

            <option>Operation Two</option> <!-- sub -->

        </select>

    <br>

    <button type="submit" name="submit" value="submit">Calculate </button>

  </form>

  <p>Result: </p>
  <!-- php code -->

</body>


</html>

If i have the code on the same file i get the answer on the same page and exactly after "Result:", but when i separate them the answer prints out on a new blank page and in the corner. How do I get the same effect as if they were on the same file?

</div>
  • 写回答

2条回答 默认 最新

  • donlih2986 2019-05-11 11:23
    关注

    An easy way to do this is mix PHP with HTML. Not the best way, because you should sepearte logic from Template/Output. To do this there are some PHP Templating Enginges that will help you.

    If your are starting with PHP, the following will do the trick. Maybe it is necessary to change the file ending from .html to .php but it depends on your webserver configuration.

    <?php 
    
    //include your file calc.php to access the functions, see https://php.net/require
    require 'calc.php';
    
    // do your fancy stuff an set your calculation result
    $result = ...
    ?>
    <!DOCTYPE html>
    
    <html>
    
    <head>
      <meta charset="UTF-8">
    
      <title>Something</title>
    </head>
    
    <body>
    
      <form action="" method="get">
        <input type="text" name="num1" placeholder="Tal 1">
    
        <input type="text" name="num2" placeholder="Tal 2">
    
            <select name="operator"><!-- used to choose which operator to calculate with from the php code -->
    
                <option>Operation One</option> <!-- add -->
    
                <option>Operation Two</option> <!-- sub -->
    
            </select>
    
        <br>
    
        <button type="submit" name="submit" value="submit">Calculate </button>
    
      </form>
    
      <p>Result: </p>
      <!-- php code -->
    
      <?php echo $result; ?>
    
    </body>
    
    
    </html>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP