vendredi 1 décembre 2017

On form, how to get multiple checked checkbox value and add to mysql with php without reload and show confirmation message in div?

I'm trying to get multiple checked checkbox value data added to mysql with php without reloading the form page and show confirmation message in div on form page. At the moment, showing on div works but data is not being sent.

I already have done another similar one which I had almost the same code that had the input as text area so I changed that part and it works but the this one is not working. Could anyone give me help here?

form is in vote.php:

<html>
<form action="vote_received.php" method="post" target="" id="vote-submit">

<input type="hidden" name="recieved-date" id="todayDate" />

<input type="hidden" name="user-occup" id="user-occup" value="<?php $_SESSION['occupation']; ?>" />

<input type="checkbox" name="voting[]" value="How might we improve driver facilities such as lunch rooms or break rooms?" id="voting_1">
<label for="voting_1">How might we improve driver facilities such as lunch rooms or break rooms?</label>

<input type="checkbox" name="voting[]" value="How might be increase driver security at night time?" id="voting_2">
<label for="voting_2">How might be increase driver security at night time?</label>

<input type="checkbox" name="voting[]" value="How might we change the on-call communication with management?" id="voting_3">
<label for="voting_3">How might we change the on-call communication with management?</label>

<input type="checkbox" name="voting[]" value="How might we enhance the passenger conflict management system?" id="voting_4">
<label for="voting_4">How might we enhance the passenger conflict management system?</label>

<br><br>
<input type="submit" name="submit" value="Submit" class="btn align-right"></form>

<script>
$("#vote-submit").submit(function(event) {
  /* stop form from submitting normally */
  event.preventDefault();

  /* get some values from elements on the page: */
  var $form = $(this),
    $messages = $("input[name='voting[]']"),
    $occup = $form.find('input[name="user-occup"]'),
    $submit = $form.find('button[type="submit"]'),
    message_value = $messages.val(),
    url = $form.attr('action');

  var posting = $.post(url, { submission : message_value});

  posting.done(function(data) {
    /* Put the results in a div */
    $("#vote_success").html('<h2>THANK YOU!</h2><p>Thank you for your voting. Meeting invitations will be send out on December 7th, 2017.');
    /* Hide form */
    $form.hide();
  });
});
</script>
</html>

the vote_received.php is:

<?php
session_start();
if(isset($_POST['vote-submit'])) {
$voteArray=$_POST['voting'];
$conn = mysqli_connect($servername, $username, $password, $database);

if(is_null($voteArray)) {
    echo("<p>You didn't select any topic.</p>\n");
    } else {
       $N = count($voteArray);

        for($i=0; $i < $N; $i++) {
            $var1 = $voteArray[$i];
            $jobTitle = $_SESSION['occupation'];

            $sql = "INSERT INTO vote_response (occupation, voting, 
created) VALUES('$jobTitle', '$var1', now())";

            $success = mysqli_query($conn, $sql);

            if (!$success) {
              die("Couldn't enter data: ".$conn->error);
            }
              echo $var1;
              $conn->close();
        }
   }
 }
?>

Thank you very much!




Aucun commentaire:

Enregistrer un commentaire