【问题标题】:Pass dynamic text input to PHP in order to write to text file将动态文本输入传递给 PHP 以写入文本文件
【发布时间】:2015-03-04 18:16:05
【问题描述】:

建立我的第一个网站,但无法弄清楚最后一点。我的 RSVP 表单具有以下用户输入:1)下拉选择菜单中的客人数量 2)客人姓名 3)文本区域。用户选择号码后,客人姓名文本框会动态显示用于姓名输入。全部完成后,用户点击 RSVP,PHP 代码将其写入文本文件。

问题:客人号码和文本区域写得很好,但动态客人姓名框没有传递给 PHP。

我应该如何解决这个问题?一直在尝试一切。非常感谢任何帮助。

<script> //--add numbers to the dropdown select menu and append the options to select--//
$(function() {
  var $select = $("#selectguest");
  for (i=1;i<=10;i++) {
    $select.append($('<option></option>').val(i).html(i))
  }
});
</script>

    <script type='text/javascript'> //--makes Next button responsive to number of guest input--//
        function addFields(){
            // Number of inputs to create
            var number = document.getElementById("selectguest").value;
            // Container <div> where dynamic content will be placed
            var container = document.getElementById("guest_names");
            // Clear previous contents of the container
            while (container.hasChildNodes()) {
                container.removeChild(container.lastChild);
            }
            for (i=0;i<number;i++){
                // Append a node with a random text
                container.appendChild(document.createTextNode("Guest" + (i+1)));
                // Create an <input> element, set its type and name attributes
                var input = document.createElement("input");
                input.type = "text";
                input.name = "guestnames_' + i +'";
                container.appendChild(input);
                // Append a line break 
                container.appendChild(document.createElement("br"));
            }
        }
    </script>
<?php
date_default_timezone_set("America/Vancouver");
//Make sure the file exists.
touch('rsvp.txt');
//Check the length of the comments file.
if ( filesize('rsvp.txt') < 50000 ) {
  //Get the user's message.
  $date = date('l, F jS Y, h:i:s A');
  $guests = substr($_POST['guest_number'], 0, 2);
  $guests = htmlentities($guests);
  $guestnames = substr($_POST['guestnames'], 0, 20);
  $guestnames = htmlentities($guestnames);
  //  Limit it to 2,000 characters.
  $message = substr($_POST['message'], 0, 2000);
  //Convert special characters to HTML entities.
  $message = htmlentities($message);
  //Append message to the RSVP file.
  $f = fopen('rsvp.txt', 'a');
  fwrite($f, "\r\n$date\r\n$guests\r\n$guestnames\r\n$message\r\n");
  fclose($f);
}
//Jump back to whichever page.
header("refresh:3; index.html");
?>
<form class="RSVPform" action="rsvp.php" method="post">
  <div>
    <p>Please select the number of guests and enter their names. Once you're ready, click RSVP and you're all set!</p><br>
    <center>Number of Guests: <select id="selectguest" name="guest_number" onChange="addFields()"></select><br><div id="guest_names" name="guest_names"/></div><br><hr><p align="left"><font size=2>Have dietary preferences? Tell us in the message box below.</font></p><textarea id="text" placeholder="Leave a message for the bride &amp groom!" name="message" id="guest_message" rows="5" cols="48"></textarea><button type="submit" id="RSVP">RSVP</button></center>
  </div>
  </form>

【问题讨论】:

    标签: javascript php dynamic input fwrite


    【解决方案1】:

    您发布的不是$_POST['guestnames'],而是很多$_POST['guestnames_#']

    在您的 HTML 页面上,将 guestnames 输入的名称更改为:

    input.name = "guestnames[]";
    

    在您的 PHP 脚本中,您现在可以访问 $_POST['guestnames']

    这将是一个数组,因此您必须执行类似的操作

    $guestnames = "";
    foreach ($_POST['guestnames'] as $guest)
    {
        $guestnames .= ", ".trim($guest); //trim whitespaces from beginning and end of string.
    }
    
    $guestnames = htmlentities(substr($guestnames, 2)); //strip the first comma and space and run through htmlentities.
    

    【讨论】:

    • 感谢您的评论 Mouser。来宾名称现在正在写入文本文件,但我收到以下警告并且无法重定向回 index.html - 警告:substr() 期望参数 1 是字符串,第 11 行 /rsvp.php 中给出的数组 - 这个指的是 $guestnames = substr($_POST['guestnames'], 0, 20);
    • 好的,我现在已经解决了所有问题。我添加了 $guestnames = substr((string)$_POST['guestnames'], 0, 20); - 现在我没有收到任何警告,它能够加载标题和重定向。感谢您对 Mouser 的所有帮助!
    • @Paul 我明白你为什么会出错。我认为你存储 var 的方式是有缺陷的。如果我发布 2 个家庭成员的姓名:Frank Underwood 和 Mister Underhill,您的代码会将其截断为 Frank Underwood Miste。当您将我的脚本与foreach 一起使用时。存储在$_POST 数组中的所有输入都被正确解析,没有数据被截断。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    相关资源
    最近更新 更多