【问题标题】:Loading csv values from a file into web form as DEFAULTS using PHP使用 PHP 将文件中的 csv 值作为默认值加载到 Web 表单中
【发布时间】:2018-07-04 19:56:13
【问题描述】:

下面附加的代码有效,它要求输入两个项目,然后在提交时,将值放入 php 服务器上的 csv 文件中。

我想加载最后一个先前的结果,现在是 csv 文件中的最后一行,作为下一轮的默认值返回到表单中(因此 2 个字段填充了值,用户可以按提交或进行更改,然后按 SUBMIT 提交新更改的值)。如果你知道一些简单的编码来添加到代码中,完成这项工作,我很乐意看到它......谢谢

BELOW 是提交时调用自身的文件“FormTest-002.php”...

<?php
if($_POST['formSubmit'] == "Submit")
{
    $errorMessage = "";

    if(empty($_POST['formGum']))
    {
        $errorMessage .= "<li>You didn't enter your favourite gum</li>";
    }
    if(empty($_POST['formName']))
    {
        $errorMessage .= "<li>You didn't enter your name</li>";
    }

    $varGum = $_POST['formGum'];
    $varName = $_POST['formName'];

    if(empty($errorMessage)) 
    {
        $fs = fopen("gumdata.csv","a");
        fwrite($fs,$varName . ", " . $varGum . "\n");
        fclose($fs);        
        header("Location: SubmitSuccessful.html");
        exit;
    }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>MyGUM Form</title>
</head>

<body>
        <b>v1.01/18jan25/fj</b>

    <?php
        if(!empty($errorMessage)) 
        {
            echo("<p>There was an error with your form:</p>\n");
            echo("<ul>" . $errorMessage . "</ul>\n");
        } 
    ?>
    <form action="FormTest-002.php" method="post">
        <p>
            What is your favorite Gum?<br>
            <input type="text" name="formGum" maxlength="35" value="<?=$varGum;?>" />
        </p>
        <p>
            What is your name?<br>
            <input type="text" name="formName" maxlength="35" value="<?=$varName;?>" />
        </p>                
        <input type="submit" name="formSubmit" value="Submit" />
    </form>
</body>
</html>

【问题讨论】:

标签: php forms csv web defaults


【解决方案1】:

对于写入和读取 csv 文件,PHP 具有原生功能。使用fgetcsv()fputcsv()

【讨论】:

    【解决方案2】:

    好的,我希望有人会觉得这很有用!它有效并且 是 CSV 写入和读取的良好起点,并使用最后输入的值作为下一个条目的 STARTING DEFAULTS。

    我仍然想更改函数以使用实际的 CSV 函数 fgetcsv() 和 fputcsv(),但它可以工作!

    <?php
    ###
    $ver="1.07e/17jan27";
    ###
    #To use:
    #  -By Fred Johnston and put onto StackExchange.com 18jan27
    #  -If you find this helpful, please UPVOTE me on stackexchange.com... God Bless
    #  -Put this FormTest-005.php into /var/www/html/whatever
    #  -Create a blank text file called gumdata.csv in same folder
    #  -Create an index.php in same folder with contents below
    #  -Set all wide open permissions FOR TEST... "sudo chmod 777 *"
    #  -Browse to the folder eg http://127.0.0.1/whatever
    #Contents of index.php follow... (remove # (COMMENT) at start of each line)
    #<HTML>
    #<HEAD>
    #<TITLE>Gummy Page</TITLE>
    #</HEAD>
    #<body>
    #<A HREF="./FormTest-005.php">FromTest-005.php (GummyForm TEST)</A><BR>
    #</BODY>
    #</HTML>
    #
    ###
    ### HERE WE GO... First pass skips this section, as form NOT submitted yet
    ### ...after form submitted, THIS section then runs when called 2nd time
    ###
    if($_POST['formSubmit'] == "Submit")
    {
        $errorMessage = "";
    
        if(empty($_POST['formGum']))
        {
            $errorMessage .= "<li>You didn't enter your favourite gum</li>";
        }
        if(empty($_POST['formName']))
        {
            $errorMessage .= "<li>You didn't enter your name</li>";
        }
    
        $varGum = $_POST['formGum'];
        $varName = $_POST['formName'];
    
        if(empty($errorMessage)) 
        {
            $fs = fopen("gumdata.csv","a");
            fwrite($fs,$varName . ", " . $varGum . "\n");
            fclose($fs);        
            header("Location: SubmitSuccessful.html");
            exit;
        }
    }
    ?>
    
    <?php
    ###
    ### This section runs, and reads in and displays CURRENT csv file
    ### ...and will list the contents of it as it gets more rows
    ### ...it also sets variables so they can populate as DEFAULTS!!
    ###
    $row = 1;
      echo "==========================================="."<br>";
      echo $ver . "<br>";
      echo "CONTENTS OF gumdata.csv...<br>";
      echo "==========================================="."<br>";
    if (($handle = fopen("gumdata.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);
            if ($row <> 1){
              echo "======= ======= =======" . "<br>";
            }
            echo "$num fields in line $row:"."<br>";
            $row++;
            for ($c=0; $c < $num; $c=$c+2) {
                echo "&nbsp;&nbsp;&nbsp;&nbsp;Name:" . $data[$c] . "<br>";
                echo "&nbsp;&nbsp;&nbsp;&nbsp;Gum:" . $data[$c+1] . "<br>\n";
                $varName = $data[$c];
                $varGum = $data[$c+1];
            }
        }
        echo "==========================================="."<br>";
        echo "==========================================="."<br>";
        fclose($handle);
    }
    ?>
    
    <?php
    ### 
    ### The next section is the display of the form, populated with the LAST previously entered
    ### values, so can be used as base for the next entry added.  Once submitted, THIS
    ### file FormTest-005.php is RE-RAN and the top section then does a STUPID CHECK and if
    ### all ok, then writes the two entered fields into the gumdata.csv file
    ###
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    <html>
    <head>
        <title>MyGUM Form</title>
    </head>
    
    <body>
        <?php
            if(!empty($errorMessage)) 
            {
                echo("<p>There was an error with your form:</p>\n");
                echo("<ul>" . $errorMessage . "</ul>\n");
            } 
        ?>
    
            <?php
              echo $ver; #show version
            ?>
    
    
        <form action="FormTest-005.php" method="post">
            <p>
                What is your favorite Gum?<br>
                <input type="text" name="formGum" maxlength="35" value="<?=$varGum;?>" />
            </p>
            <p>
                What is your name?<br>
                <input type="text" name="formName" maxlength="35" value="<?=$varName;?>" />
            </p>                
            <input type="submit" name="formSubmit" value="Submit" />
        </form>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-18
      • 2017-07-25
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多