-1

I have this assignment I am trying to solve, I have a piece of code I am trying to run to save my class details on a textfile but the form doesnt write to the text file .. below is the code, i'm a beginner please try enlighten me thank you..

<?php
if(isset($_POST['submit']))
{
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $age = $_POST['age'];
    $sex = $_POST['sex'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $zip = $_POST['zip'];
    $grade = $_POST['grade'];
    $email = $_POST['email'];
    $file = fopen("schooldetails.txt","a") or die("file not open");
    $s = $fname.",".$lname.$age.$sex.$address.$city.$zip.$grade.$email."\n";
    fputs($file,$s) or die ("cannot write data");
    fclose($file);
}
?>

This above is the php code, while this below is the html code, please help me out on what I might be missing, thank you..

<form action="success.php" method="post">
<fieldset>

<!-- Form Name -->
<legend>Form Name</legend>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="fname">First Name </label>  
  <div class="col-md-4">
  <input id="fname" name="fname" type="text" placeholder="" class="form-control input-md" required="">

  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="lname">Last Name</label>  
  <div class="col-md-4">
  <input id="lname" name="lname" type="text" placeholder="" class="form-control input-md">

  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="age">Age</label>  
  <div class="col-md-4">
  <input id="age" name="age" type="text" placeholder="" class="form-control input-md" required="">

  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="sex">Sex</label>  
  <div class="col-md-4">
  <input id="sex" name="sex" type="text" placeholder="" class="form-control input-md" required="">

  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="address">Address</label>  
  <div class="col-md-4">
  <input id="address" name="address" type="text" placeholder="" class="form-control input-md" required="">

  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="city">City</label>  
  <div class="col-md-4">
  <input id="city" name="city" type="text" placeholder="" class="form-control input-md" required="">

  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="email">Email Address</label>  
  <div class="col-md-4">
  <input id="email" name="email" type="text" placeholder="" class="form-control input-md" required="">

  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="zip">Zip</label>  
  <div class="col-md-4">
  <input id="zip" name="zip" type="text" placeholder="" class="form-control input-md" required="">

  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="grade">Grade</label>  
  <div class="col-md-4">
  <input id="grade" name="grade" type="text" placeholder="" class="form-control input-md" required="">

  </div>
</div>

<!-- Button -->
<div class="form-group">
  <label class="col-md-4 control-label" for="submit"></label>
  <div class="col-md-4">
    <button id="submit" name="submit" class="btn btn-primary">submit</button>
  </div>
</div>

</fieldset>
</form>
Nassy
  • 1
  • at first look, the code seems good. What have you tried to debug ? We surely won't do it for you. What is exactly in `$_POST` ? What request is sent by the browser (use dev tools off the browser) – Pierre Apr 03 '20 at 15:16
  • Add [error reporting](http://stackoverflow.com/questions/845021/) to the top of your file(s) _while testing_ right after your opening PHP tag for example. Even if you are deveopling on a server configured as LIVE you will now see any errors. ` – RiggsFolly Apr 03 '20 at 15:20

1 Answers1

0

Your POST data probably doesn't contain POST[submit'], try changing

if(isset($_POST['submit']))

to

if(isset($_POST))
Mike Volmar
  • 1,927
  • 1
  • 22
  • 31