I'm trying to pass a variable from an external JavaScript file to PHP , getting its value again, but it gives an error when I try to pass to PHP in> and make the request of POST in Ajax . Anyway I tried, the variable does not pass .
I get the POST from it and write to a session, then I give var_dump
to that session and it always NULL .
When I put in to test a variable of any value in the PHP of POST , the $_SESSION
gets NULL and it appears that there was no request, even having a refresh on the page.
Excerpt from 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 ------------ (one part php)
(Here takes the variable and transforms it 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>