Pass Javascript/Jquery variable to PHP

Asked

Viewed 414 times

0

I’m trying to pass a file variable Javascript external to PHP, picking up her value again, but it makes a mistake when I try to pass to PHP and request the POST in Ajax. In every way I’ve tried, the variable doesn’t raisin.

I’ll get the POST and I record in one session, then I give a var_dump in this session and appears NULL at all times.

When I put to test a variable with any value in the file PHP of POST, to $_SESSION stays NULL and it seems there was no requisition, even though there was a refresh on the page.

Excerpt from the archive date js.:

 saveBtn.on("click", function() {
      var inputName = $("input[name=mat]").val();
      var inputDate = $("input[name=date]").val();
      var inputNotes = $("textarea[name=notes]").val();
      var inputTag = $("select[name=tags]")
        .find(":selected")
        .text();

----------------ag-monitoria.php------------ ( a php part )

( Here takes the variable and turns into a session )

if(isset($_POST['dataform'])) {

  $_SESSION['materia'] = $_POST['tags'];
  $_SESSION['data'] = $_POST['data'];
  $_SESSION['obs'] = $_POST['notes'];
}

Follow the form in HTML ---

    <form id="addEvent" method="post">
      <h2>Data</h2>
      <input type="date" name="date" id="date" required>
               <div class='col-sm-4'>
                </div>
      <h2>Observações</h2>
      <textarea placeholder="Observações" name="notes" cols="30" rows="10"></textarea>
      <h2>Matéria</h2>
      <select name="tags" id="mat">
          <?php 

          $add = new USER();

                      $stmt = $add->runQuery("SELECT * FROM materias ORDER BY id_mat");
        $stmt->execute();
        $m=$stmt->fetchAll(PDO::FETCH_ASSOC);
        foreach($m as $mat) {
          $materia = $mat['materia'];
          ?>
            <option  value="<?php echo $materia;?>"><?php echo $materia;?></option>   
            <?php }?>
        </select>
        <br>
        <br>
        <script type="text/javascript">
$(document).ready(function () { 


$('#addEvent').on('submit', function() {
   event.preventDefault();
    $.ajax({
        url: 'ag-monitoria.php',
        type: 'post',
        data: $('#addEvent').serialize(),
        success: function(data) {

      }

    });

    return false;

});
});
</script>
        <input type="submit" href="javascript:;" class="o-btn js-event__save md-trigger md-setperspective" data-modal="modal-18" value="AGENDAR" id="dataform" name="dataform">
    </form>
  • You signed in to PHP using the method session_start() ? Other than that, add the part of PHP code where you save the session so we can better understand the logic.

  • Yes, in all PHP files it loads a class method where it starts the session, since the page is from a panel where a logged in user.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.