在PHP中动态存储字符串


Dynamically Store a String in PHP

我正试图从HTML web表单中获取和传输信息(通过输入文本字段),然后将信息存储到PHP变量中的另一个文件中。正如你在下面看到的,我已经创建了一个要搜索的字符串($str_to_find),并手动为该字符串使用了"script",但我想做的是用另一个文件中的web表单中的信息动态填充这个区域。

我知道这可能是一个普遍的问题,但我不知道从哪里开始。

function check_files($this_file) {
$str_to_find='script'; // the string(code/text) to search for
//I want to fill the 'string' above with info from another file's web form, if possible.

听起来您只想使用GET或POST数据。这是基本样品。一个人将在form.html上填写表格,然后单击提交。然后,您将通过属性名称从该表单中收集POST数据。在这种情况下,process.php脚本只打印出"Hello <firstname> <lastname>",但您也可以根据需要显示它。

form.html

<form action="process.php" method="post">
  <input name="fname" type="text" />
  <input name="lname" type="text" />
  <input type="submit" />
</form>

process.php

$fname = $_POST['fname'];
$lname = $_POST['lname'];
echo "Hello $fname $lname"
...

如果你想在同一个页面上显示信息,你可以使用AJAX。提到http://api.jquery.com/jQuery.ajax/例如。我在下面列出了一个:

form.html

...
<head>
...
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
    // when document is ready do this
    $(document).ready(function() {
        // listen to when users click on the send button
        $('#send-ajax').click(function() {
            // get input data
            $fname = $('#fname').val();
            $lname = $('#lname').val();
            // result container
            $result = $('#result-ajax');
            // create ajax request to process and store
            // result in the div container above the form
            $.ajax({
                url: 'process.php',
                type: 'POST',
                dataType: 'HTML',
                data: {
                    fname: $fname,
                    lname: $lname
                },
                success: function($html) {
                    $result.html($html);
                },
                error: function() {
                    $result.html('<b>Request Failed</b>');
                }
            });
        });
    });
</script>
</head>
<body>
    <div id="result-ajax"></div>
    <input id="fname"​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ />
    <input id="lname" />
    <button id="send-ajax" value="send">Send</button>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
</body>
...

process.php

同上