dongyihao9887 2017-01-05 10:39
浏览 27

PHP代码未在表单提交上执行

Problem:

I have created a simple web server with apache hosted by google cloud services and I am having a hard time figuring out how to get my PHP code to execute when a form has been submitted.


Objective:

I want the user to be able to input some information into a form and have it sent to the server. From there the server will run some PHP code and write the information to a file in the same directory.


How it works now:

When I submit the form instead of executing the action it just opens up the file as a text document in the browser.

Redirected after form submission


Form from Main.html

<form action="processForm.php" method="POST">
      Spotify URI: <input type="text" name="uri">
      <br/>
      Description: <input type="text" name="description">
      <br/>
      <input type="submit">
</form>

processForm.php

<?php
if(isset($_POST['uri']) && isset($_POST['description'])) {
    $data = $_POST['uri'] . '-' . $_POST['description'] . "
";
    $ret = file_put_contents('data.txt', $data, FILE_APPEND | LOCK_EX);
    if($ret === false) {
        die('There was an error writing this file');
    }
    else {
        echo "$ret bytes written to file";
    }
}
else {
   die('no post data to process');
}

I don't believe it is an issue with PHP being installed improperly. When I run php -v it echoes

PHP 5.6.29-0+deb8u1 (cli) (built: Dec 13 2016 16:02:08) Copyright (c) 1997-2016 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies


展开全部

  • 写回答

1条回答 默认 最新

  • duanchi3109 2019-04-20 12:01
    关注

    I had exactly this problem. I got phpinfo() working but form submission just gave the PHP in plain text, not executing. Funk Forty Niner had the right idea. From file:///DocumentRoot/Main.html (where DocumentRoot is whatever it is on your machine), the PHP doesn't execute. But from localhost/Main.html, it does. It's exactly the same file, but the browser treats the form differently.

    评论
    编辑
    预览

    报告相同问题?

    手机看
    程序员都在用的中文IT技术交流社区

    程序员都在用的中文IT技术交流社区

    专业的中文 IT 技术社区,与千万技术人共成长

    专业的中文 IT 技术社区,与千万技术人共成长

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    客服 返回
    顶部