show hidden div only if form is filled on submit

tibewww

It has been one week that I'm trying to achieve that, but I don't manage to make it work . . .

I've a result contain in a div, which is show when we submit a form with the information of the user.

I've "link" the result with a contact form to send all the information by email.

I'd like to show this div (of the result, which is call resultKeeper), only after the submitting is filled, as at the moment user can click submit, and the result appear immediately, even the form is not fill. . . but I receive the information by email only if they fill everything .

here is my jquery:

$(function() {
$('#quesForm').on('submit',function(e) { 
e.preventDefault();
// I am fetching the form values you could get them by other selectors too
var uname = $("input[name=senderName]").val(); 
var uemail = $("input[name=senderEmail]").val();
    var msg = $("input[name=message]").val()

$.post('submit.php',{senderName:uname,senderEmail:uemail,message:msg,results:$('#resultKeeper').html(),subject:'Subject of your e-mail'},function(result){
      // result variable contains your response text
      // I guess you trying to update your response 
     // notice I have used html(result) as you have just used html()
 $('#responseMessage').html(result);
});


  // you dont require `return false`
  // you have already did it using e.preventDefault();
});
}); 

My php submit:

<?php $name = $_POST['senderName'];
   $email = $_POST['senderEmail'];
   $message = $_POST['message'];
   $results = $_POST['results'];
   $results = strip_tags($results); // add this to remove html tags and all
   $formcontent="From: $name \n Email: $email \n Phone: $message \n Results: \n      $results";
   $recipient = "[email protected]";
   $subject = "subject";
   $mailheader = "From: $email \r\n";
   mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
   echo "";
   ?>

My html of the form:

   <form action="submit.php" method="POST" id="quesForm">
   <label form="senderName">Your Name</label>
  <input type="text" name="senderName" id="senderName" placeholder="Please type your name" required maxlength="40" /><br/>

   <label form="senderEmail">Your Email Address</label>
  <input type="email" name="senderEmail" id="senderEmail" placeholder="Please type your name" required maxlength="40" /><br/>

<label form="message" style="padding-top: .5em;">Your Message</label>
  <input type="text" name="message" id="message" placeholder="Please type your message"  required maxlength="40" />

 <input id="button"  type="submit" value="Send" class="btnShowResult"  >

Thank you all your help =)

Don Barry

You have a couple of things you have left out :

  1. You are not returning any data from your PHP to JQUERY post
  2. There's no $result in your submission, so there's no need for your php to use it
  3. There's no div or other item in your html named #responseMessage, so i created one

The code below should work

<?php $name = $_POST['senderName'];
$email = $_POST['senderEmail'];
$message = $_POST['message'];
//$results = $_POST['results'];
//$results = strip_tags($results); // add this to remove html tags and all
$formcontent="From: $name \n Email: $email \n Message: $message \n";
$recipient = "[email protected]";
$subject = "subject";
$mailheader = "From: $email \r\n";
//mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
print $formcontent;
?>

//note that i commented out the mail because i have not configured smtp so i didnt want it to "die"

For your html and jquery

<script src="http://code.jquery.com/jquery-2.1.0.min.js" type="text/javascript"></script>
<form action="submit.php" method="POST" id="quesForm">
<label form="senderName">Your Name</label>
<input type="text" name="senderName" id="senderName" placeholder="Please type your name" required maxlength="40" />
<br/>
<label form="senderEmail">Your Email Address</label>
<input type="email" name="senderEmail" id="senderEmail" placeholder="Please type your name" required maxlength="40" /><br/>

<label form="message" style="padding-top: .5em;">Your Message</label>
<input type="text" name="message" id="message" placeholder="Please type your message"  required maxlength="40" />
<input id="button"  type="submit" value="Send" class="btnShowResult"  >
<div id="responseMessage"></div>
<script type="text/javascript">
$(function() {
$('#quesForm').on('submit',function(e) { 
e.preventDefault();
// I am fetching the form values you could get them by other selectors too
var uname = $("input[name=senderName]").val(); 
var uemail = $("input[name=senderEmail]").val();
var msg = $("input[name=message]").val()
$.post('submit.php', {senderName:uname,senderEmail:uemail,message:msg,results:$('#resultKeeper').html(),subject:'Subject of your e-mail'},function(result){
  // result variable contains your response text
// I guess you trying to update your response 
// notice I have used html(result) as you have just used html()
$('#responseMessage').html(result);
});
});
});
</script>

UPDATED JAVASCRIPT

$(function(){
var jQuiz = {
    answers: { q1: 'c'},
    questionLenght: 1,
    checkAnswers: function() {
        var arr = this.answers;
        var ans = this.userAnswers;
        var resultArr = []
        for (var p in ans) {
            var x = parseInt(p) + 1;
            var key = 'q' + x;
            var flag = false;
            if (ans[p] == 'q' + x + '-' + arr[key]) {
                flag = true;
            }
            else {
                flag = false;
            }
            resultArr.push(flag);
        }
        return resultArr ;
    },
    init: function(){
        $('.btnNext').click(function(){
            if ($('input[type=radio]:checked:visible').length == 0) {

                return false;
            }
            $(this).parents('.questionContainer').fadeOut(500, function(){
                $(this).next().fadeIn(500);
            });
            var el = $('#progress');
            el.width(el.width() + 300 + 'px');
        });
        $('.btnPrev').click(function(){
            $(this).parents('.questionContainer').fadeOut(500, function(){
                $(this).prev().fadeIn(500)
            });
            var el = $('#progress');
            el.width(el.width() - 300 + 'px');
        })
        $('.btnShowResult').click(function(){
            var arr = $('input[type=radio]:checked');
            var ans = jQuiz.userAnswers = [];
            for (var i = 0, ii = arr.length; i < ii; i++) {
                ans.push(arr[i].getAttribute('id'))
            }
        })
        $('.btnShowResult').click(function(){
            $('#progress').width(300);
            $('#progressKeeper').hide();
            var results = jQuiz.checkAnswers();
            var resultSet = '';
            var trueCount = 0;
            for (var i = 0, ii = results.length; i < ii; i++){
                if (results[i] == true) trueCount++;
                resultSet += '<div id="questionanswrs"> Question ' + (i + 1) + ' is ' + results[i] + '</div>'

            }
            resultSet += '<div class="totalScoreok"></br>Your total score is ' + trueCount * 1 + ' / 20</div>'
            //$('#resultKeeper').html(resultSet).show(); 

                if (trueCount > 15) {
resultSet += '<div class="totalScore"></br>results goes here</div>'
            //$('#resultKeeper').html(resultSet).show();
                                }

                                  if (trueCount <= 15 && trueCount >10)  {
resultSet += '<div class="totalScore"></br>results goes here</div>'
            //$('#resultKeeper').html(resultSet).show();            
                    }   

                     if (trueCount <=10 && trueCount >5)  {
resultSet += '<div class="totalScore"></br>results goes here</div>'
            //$('#resultKeeper').html(resultSet).show();            
                    }   

                 if (trueCount <= 5) {
resultSet += '<div class="totalScore"></br>results gose here</div>'
            //$('#resultKeeper').html(resultSet).show();
                                }
    var uname = $("input[name=senderName]").val(); 
    var uemail = $("input[name=senderEmail]").val();
    var msg = $("input[name=message]").val()
    if ((uname != null && uname!="")&& (uemail!=null && uemail!="" && uemail.indexOf("@")!=-1) && (msg !=null && msg!="")){     
            console.log("msg'" + msg + "'");
    }else{
        resultSet="Error message. Please complete your form";
    }
        $('#resultKeeper').html(resultSet).show();              
        })
    }
};


jQuiz.init();
})

Collected from the Internet

Please contact debug[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to show hidden div after hitting submit button in form?

From Dev

Display a hidden div after a html form submit to show output

From Dev

Displaying a 'hidden' form once the first form is filled and submit button clicked

From Dev

Displaying a 'hidden' form once the first form is filled and submit button clicked

From Dev

Submit only filled fields in form php

From Dev

Show message in div in submit form

From Dev

jquery form submit - show div and submit

From Dev

show a div after a html form submit

From Dev

How to show result only when all form fields are filled?

From Dev

How to hide the one div on form submit and show another div?

From Dev

CSS-only show hidden div, hide the "show" link upon showing the hidden div

From Dev

Show HTML5 validation only on form submit

From Dev

Bootstrap jQuery, how to show hidden DIV when child form fields are populated AND clear them when DIV is hidden?

From Dev

css or jquery on hover show only the hidden children element of current div

From Dev

HTML Submit Button not firing when form filled

From Dev

Javascript to show a hidden div

From Dev

Javascript/JQuery: On form submit, don't reload full page (only a div), and still submit form data

From Dev

How to show unbeforeunload alert when form is filled?

From Dev

Show a form filled with Javascript variables in jquery mobile

From Dev

how to show hidden form on load

From Dev

Show Hidden Form Field with Jquery

From Dev

Show Hidden Form Field with Jquery

From Dev

Get first hidden form and show it

From Dev

Upload files, show filenames on form immediately, BUT files get only uploaded to server om Form submit

From Dev

Use jQuery to show a hidden div only when a certain div gets inserted dynamically

From Dev

Change hidden value and submit form with onclick for href

From Dev

Ajax submit in hidden form validation not executed

From Dev

Submit Orbeon hidden form field data

From Dev

Ajax show form after submit

Related Related

  1. 1

    How to show hidden div after hitting submit button in form?

  2. 2

    Display a hidden div after a html form submit to show output

  3. 3

    Displaying a 'hidden' form once the first form is filled and submit button clicked

  4. 4

    Displaying a 'hidden' form once the first form is filled and submit button clicked

  5. 5

    Submit only filled fields in form php

  6. 6

    Show message in div in submit form

  7. 7

    jquery form submit - show div and submit

  8. 8

    show a div after a html form submit

  9. 9

    How to show result only when all form fields are filled?

  10. 10

    How to hide the one div on form submit and show another div?

  11. 11

    CSS-only show hidden div, hide the "show" link upon showing the hidden div

  12. 12

    Show HTML5 validation only on form submit

  13. 13

    Bootstrap jQuery, how to show hidden DIV when child form fields are populated AND clear them when DIV is hidden?

  14. 14

    css or jquery on hover show only the hidden children element of current div

  15. 15

    HTML Submit Button not firing when form filled

  16. 16

    Javascript to show a hidden div

  17. 17

    Javascript/JQuery: On form submit, don't reload full page (only a div), and still submit form data

  18. 18

    How to show unbeforeunload alert when form is filled?

  19. 19

    Show a form filled with Javascript variables in jquery mobile

  20. 20

    how to show hidden form on load

  21. 21

    Show Hidden Form Field with Jquery

  22. 22

    Show Hidden Form Field with Jquery

  23. 23

    Get first hidden form and show it

  24. 24

    Upload files, show filenames on form immediately, BUT files get only uploaded to server om Form submit

  25. 25

    Use jQuery to show a hidden div only when a certain div gets inserted dynamically

  26. 26

    Change hidden value and submit form with onclick for href

  27. 27

    Ajax submit in hidden form validation not executed

  28. 28

    Submit Orbeon hidden form field data

  29. 29

    Ajax show form after submit

HotTag

Archive