2
Then, I have a form and send his data to a PHP file via AJAX. I’m doing this normally.
Only now I had the need to send along with the form, plus a Javascript variable from AJAX to PHP. To use the value of this variable as a condition in PHP.
It seems to be a very simple thing, but I’m only seeing amazing solutions on the web. If you can help me, I would be grateful.
Below is my Javascript code and a part of the PHP Code:
$('#resultados-pendentes').click(function(e) {
e.preventDefault();
var formularioPendente = $("#formulario-consulta");
var pendente = "PENDENTE";
var retorno = inserirFormulario(formularioPendente, pendente);
});
function inserirFormulario(dados, condicao) {
$.ajax({
//dataType: "json",
type: "POST",
data: dados.serialize(), //Aqui eu gostaria de enviar minha variável
url: "banco/pagina-minhas-tarefas/interface-resultado-pesquisa.php",
cache: false,
});
}
<?php
<!-- Arquivo PHP QUE RECEBE OS DADOS DO FORMULÁRIO (SÓ UMA PARTE DO CÓDIGO) -->
<?php
require_once("../conexao/conexao-com-banco.php"); // CHAMANDO O ARQUIVO DE CONEXÃO AO BANCO
// CONSULTA GERAL DO BANCO QUE RETORNA O RESULTADO DA CONSULTA DA PÁGINA INTERFACE.PHP
session_start();
//PREENCHE AS VARIÁVEIS COM OS DADOS VINDOS DOS CAMPOS DO FORMULÁRIO
$pegaropcao = $_POST["prioridade"];
$tipodedata = $_POST["data"];
$dataInicial = !empty($_POST["dataInicial"]) ? $_POST["dataInicial"] : null;
$dataFinal = !empty($_POST["dataFinal"]) ? $_POST["dataFinal"] : null;
$cod = $_POST["cod"];
$empresas = !empty($_POST["empresas"]) ? $_POST["empresas"] : "";
$tributacao = !empty($_POST["tributacao"]) ? $_POST["tributacao"] : "";
$atividade = !empty($_POST["atividade"]) ? $_POST["atividade"] : "";
$status = $_POST["status"];
$responsavel = $_SESSION["nome"];
?>
If I just put the variable name there after the comma where I’m sending the form it goes along with the form ? (I mean, on the date).
Thank you!
That’s what I would do too, JS updates the field
hidden
. This way the form is sent without interference from the JS.– fernandosavio
Worth a vote then right? D
– Laércio Lopes
Yes, but this input type="Hidden" option would not be useful for me. Because I send the form in other ways than Submit. For example, there is a <div> that when I click on it, I send the form data as well. and the value of this input type="Hidden" could not be fixed.
– Gato de Schrödinger
So it’s not just adding a field, it can be several fields and different names?
– fernandosavio
It will not be fixed... you will set it in JS... if it depends on another field that you will set in your code.
– Laércio Lopes
@Thiagopetherson, inside of
$('#pendente').val(var)
var would be your variable.– Laércio Lopes
@fernandosavio , I would just like to have a variable that sends a value to be a condition in the php file. Only this value can vary. Understand ? Laércio, I’m analyzing your code and seeing how I can adapt it to mine.
– Gato de Schrödinger
See the edited response if it helps.
– Laércio Lopes
@Laérciolopes , your code was useful to me. Now I will make some adaptations here. Thank you! It served me momentarily. The only however is how I would send, via AJAX, that variable "pending" along with the form for the PHP file.
– Gato de Schrödinger
Thanks @Thiagopetherson!
– Laércio Lopes
That’s exactly what my answer is explaining, putting the input into the HTML form it will be read by the function
serialize()
, just put the correct name attribute and receive this parameter in the PHP file. HTML example:<input type="hidden" name="test" id="pendente">
PHP:$_POST['test']
– Laércio Lopes