Hello another multiple form submit question

Hello, I’ve searched the forum and I couldn’t find and answer that i could understand. I’m trying to do a form with lots of text, checkboxes, and a comment box at the end of each text.
And i want individuals to save their progress while they complete this huge form, so I’ve added a save/submit button also at the end of each text so they dont loose progress. Also and echo with the value they inputed in each text.

I’m really new into coding in general but i just liked php as soon as I started trying to understand it.

Here is my code its a one page php:

<?php

include_once 'connect4.php';

$sql_read = 'SELECT * FROM document1';
$gsent = $pdo->prepare($sql_read);
$gsent->execute();
$result = $gsent->fetchall();

//var_dump($result);

if (isset($_POST['post1'])){
     
     $email = $_POST['email'];
     $select01 = $_POST['select01'];
     $comment01 = $_POST['comment01'];
     

     $sql_read  = 'INSERT INTO document1 (email,select01,comment01) VALUES (?,?,?)';
     $sentence_add = $pdo->prepare($sql_read);
     $sentence_add->execute(array($email,$select01,$comment01));

     header('location:document-1.php');
}

if (isset($_POST['post2'])){
     
  $select02 = $_POST['select02'];
  $comment02 = $_POST['comment02'];
  

  $sql_read  = 'INSERT INTO document1 (select02,comment02) VALUES (?,?)';
  $sentence_add = $pdo->prepare($sql_read);
  $sentence_add->execute(array($select02,$comment02));

  header('location:document-1.php');
}

?>

<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>

lots of text, text, more text, more text, more text, more text.....

<form method="post">
              <input type="email" placeholder="Email" name="email" required/>

<div class="col md-6">
  <input type="radio" name="select01" value="Accepted" checked>
  <label for="huey">Accept</label><br>
  <input type="radio" name="select01" value="">
  <label for="huey">Reject</label>
  <br><br>
  <textarea name="comment01" rows="5" cols="75" placeholder="Comment . . . "></textarea><br>
  <input type="submit" name="post1" value="Save Answers"/>
  <br>
  </form>
  </div>

                  <div class="col-md-6">
                  <?php foreach($result as $data); ?>
                       <div class="alert alert-success" role="alert">
                          Answer: <?php echo '<b>&ensp;&ensp;Checkbox :&ensp;</b>', $data['select01'];
                                        echo '<b>&ensp;&ensp;Comment :&ensp;</b>', $data['comment01']; ?>
                       </div>
                  </div>

lots of text, text, more text, more text, more text, more text.....

<form method="post" name="form2">
<input type="radio" name="select02" value="Accepted" checked>
  <label for="huey">Accept</label><br>
  <input type="radio" name="select02" value="">
  <label for="huey">Reject</label>
  <br><br>
  <textarea name="comment02" rows="5" cols="75" placeholder="Comment . . . "></textarea><br>
  <input type="submit" name="post2" value="Save Answers"/>
  <br>
  </form>
  </div>

                  <div class="col-md-6">
                  <?php foreach($result as $data); ?>
                       <div class="alert alert-success" role="alert">
                          Answer: <?php echo '<b>&ensp;&ensp;Checkbox :&ensp;</b>', $data['select02'];
                                        echo '<b>&ensp;&ensp;Comment :&ensp;</b>', $data['comment02']; ?>
                       </div>
                  </div>

</body>
</html>

Form 2 dosen’t work, and when the table is empty i get errors insted of values. Any suggestion, material to read, ideas to add stuff that could make my form nicer? Thanks a lot.

You will want to learn about Sessions.

1 Like

You would also want to use a data-driven design, so that you are not writing out hard-coded content and php code for every different step.

Because you need to (should) record the datatime when a user starts answering these questions and if a user can ever answer these questions more than once, you need to store the user id and the datatime in a questionnaire_instance table. This will define an instance id. You would store this instance id in each row in the table holding the replies.

The table holding the replies should NOT have columns ending in sequential numbers. You would instead have a step column that holds the 1, 2, … values. If there will always only be a select and a comment value, you would have columns named select and comment. If there can ever be other values, you would instead have a field column and a value column and insert a new row for each field/value pair.

Organizing the data as above will also eliminate any errors you are getting because there either will or won’t be data for each step.

1 Like

Thanks a lot. Checking everything u said. Thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service