18910140161

html - saving form data to text file with php - Stack Overflow

顺晟科技

2022-10-19 11:24:36

267

I've been stuck with this and have been searching solutions but it still doesn't seem to work could someone help :))

<?php
if(isset($_POST['submit_btn']))
{
$file = fopen("data.txt", "a");

fwrite($file,"in game name :");
fwrite($file, $ingname."\n");
fwrite($file,"firstname :");
fwrite($file, $name."\n");
fwrite($file,"discord name :");
fwrite($file, $dname."\n");
fwrite($file,"main lengend :");
fwrite($file, $mlengend."\n");
fwrite($file,"secondary lengend :");
fwrite($file, $slengend."\n");
fwrite($file,"third legend :");
fwrite($file, $tlengend."\n");
fclose($file);
}
?>

this is the php code in the file config.php

<div class="container">
        <div class="title">Registration</div>
        <form action="/config.php" method="POST">

            <div class="user-details">
                <div class="input-box">
                    <span class="details">In game name</span>
                    <input type="text" name="ingname" placeholder="" required>
                </div>
                <div class="input-box">
                    <span class="details">Firstname</span>
                    <input type="text" name="name" placeholder="" required>
                </div>
                <div class="input-box">
                    <span class="details">Discord username</span>
                    <input type="text" name="dname" placeholder="" required>
                </div>
                <div class="input-box">
                    <span class="details">Main legend</span>
                    <input type="text" name="mlengend" placeholder="" required>
                </div>
                <div class="input-box">
                    <span class="details">Secondary legend</span>
                    <input type="text" name="slengend" placeholder="" required>
                </div>
                <div class="input-box">
                    <span class="details">Third legend</span>
                    <input type="text" name="tlengend" placeholder="" required>
                </div>
            </div>

            <div class="region">
                <p class="questionR">What is your region?</p>
                <select name="select">
                    <option value="Europe" name="regiondata" selected>Europe</option> 
                    <option value="North-America" >North-America</option>
                    <option value="South-America" >South-America</option> 
                    <option value="Africa" >Africa</option>
                    <option value="Asia" >Asia</option> 
                    <option value="Australia" >Australia</option> 
                  </select>
            </div><br>

            <div class="kdascreen">
                <form>
                    <span class="kdaQuest">Please provide a screenshot of your overall stats</span><br>
                    <input type="file" id="kdaUser" name="kdaFile">
                </form>
            </div>
            <br>
            <div class="platform">
                <input type="radio" name="plat" id="dot-1">
                <input type="radio" name="plat" id="dot-2">
                <input type="radio" name="plat" id="dot-3">
                <input type="radio" name="plat" id="dot-4">
                <span class="platform-choice">On what platform do you play on?</span>
                <div class="platform-category">
                    <label for="dot-1">
                        <span class="dot one"></span>
                        <span class="plat">PSN</span>
                    </label>
                    <label for="dot-2">
                        <span class="dot two"></span>
                        <span class="plat">Xbox</span>
                    </label>
                    <label for="dot-3">
                        <span class="dot three"></span>
                        <span class="plat">PC - Mouse and keyboard</span>
                    </label>
                    <label for="dot-4">
                        <span class="dot four"></span>
                        <span class="plat">PC - Controller</span>
                    </label>
                </div>
            </div>

            <div class="rulesTOS">
                <input type="radio" name="choiceMade" id="pot-1">
                <input type="radio" name="choiceMade" id="pot-2">
                <span class="readrules">Have you read the <a href="rules.html" target="_blank">rules</a> and agreed with the <a href="https://tos.ea.com/legalapp/WEBTERMS/US/en/PC/" target="_blank">TOS?</a></span>
                <div class="choice">
                    <label for="pot-1">
                        <span class="pot one"></span>
                        <span class="choiceMade">Yes</span>
                    </label>
                    <label for="pot-2">
                        <span class="pot two"></span>
                        <span class="choiceMade">No</span>
                    </label>
                </div>
            </div>

            <br>
            <div class="button">
                <input type="submit" name="submit_btn" value="submit"/>
            </div>
        </form>
    </div>

this is the form in the file registreren.html I've been messing around and even made this file a php file still didn't seem to work I think I might have made a stupid mistake that might have made it no to work.

#edit

I added the $_POST["xxx"] and gave it reading an writing premissions but still can't seem to save the data to the data.txt file.

<?php
if(isset($_GET['submit_btn']))
{
$file = fopen("data.txt", "a");

fwrite($file,"in game name :");
fwrite($file, $_POST["ingname"]."\n");
fwrite($file,"firstname :");
fwrite($file, $_POST["name"]."\n");
fwrite($file,"discord name :");
fwrite($file, $_POST["dname"]."\n");
fwrite($file,"main lengend :");
fwrite($file, $_POST["mlegend"]."\n");
fwrite($file,"secondary lengend :");
fwrite($file, $_POST["slegend"]."\n");
fwrite($file,"third legend :");
fwrite($file, $_POST["tlegend"]."\n");
fclose($file);

chmod("data.txt", 7777);
}
?>
||||
<?php
echo"waiting for submit..."."\n";
if(isset($_POST['submit_btn']))
{
    echo"submitted";
    $file = fopen("data.txt", "a") or die("could not open file");
    echo"in the file";
                                                                                 
    $INGNAME=$_POST['ingname'];
    $NAME=$_POST['name'];
    $DNAME=$_POST["dname"];
    $MLEGEND=$_POST["mlegend"];
    $SLEGEND=$_POST["slegend"];
    $TLEGEND=$_POST["tlegend"];

    fwrite($file,"in game name :");
    fwrite($file, $INGNAME."\n");
    fwrite($file,"firstname :");
    fwrite($file, $NAME."\n");
    fwrite($file,"discord name :");
    fwrite($file, $DNAME."\n");
    fwrite($file,"main lengend :");
    fwrite($file, $MLEGEND."\n");
    fwrite($file,"secondary lengend :");
    fwrite($file, $SLEGEND."\n");
    fwrite($file,"third legend :");
    fwrite($file, $TLEGEND."\n");
    fclose($file);

}
?>

So in the end I figured out that the issue I was having was premission based. I made some changes to the php file. but all I had to do was make the directory it was in not read-only read-only option

||||

I have reviewed my code and found some typos and misplacements in my html code.

---html code---

<form action="config.php" method="post">

            <div class="user-details">
                <div class="input-box">
                    <span class="details">In game name</span>
                    <input type="text" name="ingname" autocomplete="off" required>
                </div>
                <div class="input-box">
                    <span class="details">Firstname</span>
                    <input type="text" name="name" autocomplete="off" required>
                </div>
                <div class="input-box">
                    <span class="details">Discord username</span>
                    <input type="text" name="dname" autocomplete="off" required>
                </div>
                <div class="input-box">
                    <span class="details">Main legend</span>
                    <input type="text" name="mlegend" autocomplete="off" required>
                </div>
                <div class="input-box">
                    <span class="details">Secondary legend</span>
                    <input type="text" name="slegend" autocomplete="off" required>
                </div>
                <div class="input-box">
                    <span class="details">Third legend</span>
                    <input type="text" name="tlegend" autocomplete="off" required>
                </div>
            </div>

            <div class="region">
                <p class="questionR">What is your region?</p>
                <select name="regiondata">
                    <option value="Europe" selected>Europe</option>
                    <option value="North-America">North-America</option>
                    <option value="South-America">South-America</option>
                    <option value="Africa">Africa</option>
                    <option value="Asia">Asia</option>
                    <option value="Australia">Australia</option>
                </select>
            </div><br>

            <div class="kdascreen">
                <span class="kdaQuest">Please provide a screenshot of your overall stats</span><br>
                <input type="file" id="kdaUser" name="kdaFile">
            </div>
            <br>
            <div class="platform">
                <input type="radio" id="dot-1" name="button" value="PSN">
                <input type="radio" id="dot-2" name="button" value="Xbox">
                <input type="radio" id="dot-3" name="button" value="PC">
                <input type="radio" id="dot-4" name="button" value="PC-Controller">
                <span class="platform-choice">On what platform do you play on?</span>
                <div class="platform-category">
                    <label for="dot-1">
                        <span class="dot one"></span>
                        <span class="plat">PSN</span>
                    </label>
                    <label for="dot-2">
                        <span class="dot two"></span>
                        <span class="plat">Xbox</span>
                    </label>
                    <label for="dot-3">
                        <span class="dot three"></span>
                        <span class="plat">PC - Mouse and keyboard</span>
                    </label>
                    <label for="dot-4">
                        <span class="dot four"></span>
                        <span class="plat">PC - Controller</span>
                    </label>
                </div>
            </div>

            <div class="rulesTOS">
                <input type="radio" name="choiceMade" id="pot-1">
                <input type="radio" name="choiceMade" id="pot-2">
                <span class="readrules">Have you read the <a href="rules.html" target="_blank">rules</a> and agreed with
                    the <a href="https://tos.ea.com/legalapp/WEBTERMS/US/en/PC/" target="_blank">TOS?</a></span>
                <div class="choice">
                    <label for="pot-1">
                        <span class="pot one"></span>
                        <span class="choiceMade">Yes</span>
                    </label>
                    <label for="pot-2">
                        <span class="pot two"></span>
                        <span class="choiceMade">No</span>
                    </label>
                </div>
            </div>
            <br>
            <div class="button">
                <input type="submit" class="button" value="Save">
            </div>
        </form>

There was an extra <form> tag in the the <form> tag, this made it the reason it was not sending any information to my php or to the text file.

I have also reworked my php file for it to work properly with all the tags excluding the file one (still working on that part).

---php code---

<?php
    extract($_REQUEST);
    $file = fopen("data.txt", "a");
                                                                                 
    $INGNAME=$_POST['ingname'];
    $NAME=$_POST['name'];
    $DNAME=$_POST['dname'];
    $MLEGEND=$_POST['mlegend'];
    $SLEGEND=$_POST['slegend'];
    $TLEGEND=$_POST['tlegend'];
    $SCREESHOT=$_POST['kdaFile'];
    if(isset($_POST['button'])){
        $PLAT=$_POST['button'];
    }
    $REGIONDATA=$_POST['regiondata'];

    fwrite($file,"In game name :");
    fwrite($file, $INGNAME."\n");
    fwrite($file,"Firstname :");
    fwrite($file, $NAME."\n");
    fwrite($file,"Discord name :");
    fwrite($file, $DNAME."\n");
    fwrite($file,"Main lengend :");
    fwrite($file, $MLEGEND."\n");
    fwrite($file,"Secondary lengend :");
    fwrite($file, $SLEGEND."\n");
    fwrite($file,"Third legend :");
    fwrite($file, $TLEGEND."\n");
    fwrite($file,"Platform :");
    fwrite($file, $PLAT."\n");
    fwrite($file,"Region :");
    fwrite($file, $REGIONDATA."\n");
    fwrite($file, "kdafile :");
    fwrite($file, $SCREESHOT."\n");
    fwrite($file, "-----END-----"."\n");
    fclose($file);
    header("location: registreren.html");
?>

In the end I didn't use the if(isset($_POST['submit_btn'])) as I figured out it was really necessary as I could you the extract methode wich basically worked the same and made me write less ;)).

All the code above is my final coding and shouldn't contain any error (I hope it doesn't because everything works for me except the file saving part but that is something I'm working on).

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航