将动态文本输入传递给PHP以写入文本文件 [英] Pass dynamic text input to PHP in order to write to text file

查看:152
本文介绍了将动态文本输入传递给PHP以写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

建立了我的第一个网站,但无法弄清这最后一​​点。我的RSVP表单具有以下用户输入:1)下拉选择菜单中的访客数量2)访客名称3)文本区域。用户选择号码后,来宾姓名文本框将动态出现,以输入姓名。完成所有操作后,用户单击RSVP,PHP代码将其写入文本文件。

building my very first website but can't figure out this last bit. My RSVP form has the following user inputs: 1) number of guests in a drop down select menu 2) Guest names 3) text area. After user selects the number, Guest name text boxes dynamically appear for name input. Once all is done, user clicks RSVP and PHP code writes it to text file.

问题:访客编号和文本区域写得很好,但动态的访客姓名框未显示

Problem: Guest number and text area writes fine but dynamic Guest name boxes don't get passed to PHP.

该如何解决?一直在尝试一切。

How should I solve this? Been trying everything. Any help is much appreciated.

<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>

推荐答案

您不是发布 $ _ POST ['guestnames'] ,而是发布了许多 $ _ POST ['guestnames _#']

You are not posting $_POST['guestnames'] but a lot of $_POST['guestnames_#'].

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

On your HTML page change the name of the guestnames inputs to:

input.name = "guestnames[]";

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

In your PHP script you can now access $_POST['guestnames'].

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

This will be an array so you have to do something like

$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.

这篇关于将动态文本输入传递给PHP以写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆