0
I have a form which is validated via Javascript. It is working perfectly, it validates, sends pro .php and he gives me a echo saying it was successful. However I wanted this echo be given on the same page as a alert, or change the display of a div existing. How do I bring this PHP output to the same page? I saw that I should use Ajax, but I did not understand very well, I do not know what does what in AJAX code.
PHP code:
<?php
$pergunta = $_POST ['pergunta'];
$resposta = $_POST ['resposta'];
$dificuldade = $_POST ['dificuldade'];
$desafio = $_POST ['desafio'];
switch ($dificuldade) {
  case '1':
    $dificuldade = "Facil";
    break;
  case '2':
    $dificuldade = "Medio";
    break;
  case '2':
    $dificuldade = "Dificil";
    break;
}
try{
 $conexao = new PDO ("mysql:host=localhost;       dbname=teocratico;charset=utf8","root","");
 } catch (PDOException $erro){
   echo "Não foi possível conectar ao banco ". $erro->getMessage();
 }
try {
 $insert =  $conexao-> prepare ("INSERT INTO perguntas (pergunta, resposta,         dificuldade, desafio) VALUES (
       :pergunta,
       :resposta,
       :dificuldade,
       :desafio
        )");
  $insert-> bindParam (':pergunta', $pergunta);
  $insert-> bindParam (':resposta', $resposta);
  $insert-> bindParam (':dificuldade', $dificuldade);
  $insert-> bindParam (':desafio', $desafio);
  $insert-> execute();
  echo "Pergunta enviada com Sucesso";
} catch (Exception $erro) {
 echo "Não foi possivel enviar os dados" . $erro->getMessage();
}
?>
Ajax code:
  $('#form_pergunta').submit(function(e) {
   e.preventDefault();
   const pergunta = $('input[name="pergunta"]').val();
   const resposta = $('input[name="resposta"]').val();
   const dificuldade = $('input[name="dificuldade"]')
   const desafio = $('select[name="desafio"]').val();
   $.ajax({
       url: 'envia.php', // caminho para o script que vai processar os dados
   type: 'POST',
   data: {pergunta: pergunta, resposta: resposta, dificuldade: dificuldade,    desafio: desafio},
       success: function(response) {
           $('#resp').php(response);
       },
       error: function(xhr, status, error) {
           alert(xhr.responseText);
       }
    });
   return false;
});
						
See if this link helps you http://answall.com/questions/174883/formul%C3%A1rio-de-contato-com-ajax-sem-refresh
– LocalHost
If you don’t want ajax, you can simply put the script that receives the form on the same page. At the top of the page you put a check to see if there was a POST. if( (isset($_POST['name'])) and (!Empty($_POST['name'])){ // performs the function of receiving and saving data or sending email // if everything goes right echo "<div>worked</div>"; }
– Vítor André
Tiago, if possible from a quick search in "jQuery Ajax via Load", man it is very simple to use in short the command is $("#div_result"). load("codigo.php", {"name_date1": date1, "name_date2": date2}); the data is sent to PHP in load itself and in the result div_echo will be returned with the result.
– Cleverson