Recover data after Submit

Asked

Viewed 789 times

0

Good morning Guys, I have a form that validates in php and not in javascript, and when an error occurs I run a history.back() to return to the form. That returns a blank form. Does anyone have any idea why I should not lose this data?

EDIT: I have a validation based on a note (1 to 10). There are 30 questions. I put only one as an example. I run this if. You can also do this validation in javascript. But I don’t know how to do it. Can you help me thank you.

HTML

<div class="large-2 columns">
   <label>Nota<br>                           
       <input required="" type="radio" value="1" name="14_pesquisa">1 
       <input required="" type="radio" value="2" name="14_pesquisa">2 
       <input required="" type="radio" value="3" name="14_pesquisa">3 
       <input required="" type="radio" value="4" name="14_pesquisa">4 
       <input required="" type="radio" value="5" name="14_pesquisa">5 
       <input required="" type="radio" value="6" name="14_pesquisa">6 
       <input required="" type="radio" value="7" name="14_pesquisa">7 
       <input required="" type="radio" value="8" name="14_pesquisa">8 
       <input required="" type="radio" value="9" name="14_pesquisa">9 
       <input required="" type="radio" value="10" name="14_pesquisa">10
   </label>
</div>

PHP

if(($pesquisa14 >= 9 and $pesquisa1 != 0)) {

ok.....

else{

    $retorno = "<SCRIPT LANGUAGE='JavaScript' TYPE='text/javascript'>
                                    alert('Algo deu errado. Tente novamente');
                                    history.back();
                                </SCRIPT>";

                            echo $retorno;

}
  • 1

    Do not send the page. Use ajax... do you know how to do this? Or fill the fields in PHP to open the already filled page

  • I’ll edit my question so you understand better.

  • I recommend working the form validation and reading the PHP data ($_POST) on the same page as this form and so if any error occurs you can set all the data typed in the form again. In case of success you make a redirect to the successful page before rendering the form.

  • Fabio, I’m using a "form" case for the form, and "save" to save in the bank. No save do if. In MVC how would you do this?

  • Sergio, I don’t have much knowledge in ajax :( can show an example?

  • by MVC vc tb can play the form action for the same controller q generates the form. In all form inputs you will have to put the output from the form, something like this: &#xA;<input required="" type="radio" value="1" name="14_pesquisa" <?php echo (isset($_POST['14_pesquisa']) && $_POST['14_pesquisa'] == 1)?'checked':'';?>>1 &#xA; understood?

Show 1 more comment

2 answers

0

Code is simple example to use ajax Submit

File (post.php):

<?php
if (!empty($_POST)) {
    echo $_POST['texto'];
}
?>

File (index.html):

<html>
<head>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>

<body>
    <form id="ajax_form">
        <p>Texto:</p>
        <p><textarea name="texto" rows="10" cols="30" placeholder="Descreva um comentario" /></textarea></p>
        <p><input type="submit" name="enviar"/></p>
    </form>
    <hr><p>Ajax:</p>
    <textarea id="log" rows="20" cols="70"></textarea>
</body>

<script>
$(document).ready(function(){
    $('#ajax_form').submit(function(){
        var dados = $(this).serialize();
        $.ajax({
            type: "POST",
            url: "post.php",
            data: dados,
            success: function(testlog) {
                $('textarea#log').text(testlog);
            }
        });
        return false;
    });
});
</script>

</html>

-1

you can check the form to see if any radio has been marked or not

//javascript

function checaPesquisa() {
    sel = document.getElementByName("14_pesquisa").checked;
    if(!sel ){
        alert("Escolha uma opção");
        return false;
    }else{
        return true;
    }
}

and on your send button put:

onClick = "return checaPesquisa()" 
  • 2

    Will, the problem is not whether it was marked or not. For this I have the required html. It’s a value check I need.

Browser other questions tagged

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