0

This seems like such a simple thing, but I have not been able to find adequate guidance on it for the life of me. I have only been able to find things about successive AJAX queries regardless of user input, like this or this.

What I'm trying to do is create a survey such that after a user answers a question, the website replaces the question with the next question, and so on.

Here's what I have so far (in a .php file):

In <head>, I have a function for each successive element to call:

<script type="text/javascript">
    function nextPage(url) {
        $("#consent-form").empty();
        $("#consent-form").load(url);
    }
</script>

Maybe that's just not how it's done. I have no idea. Like I said, I have not been able to find adequate help on this. As far as I can tell, empty() should not delete #consent-form, but only its content and children, which is exactly the behavior I want.

This is the initial html and php for the div I want to swap out after each answered question:

<div id="consent-form">

   <?php

        // Check for a valid email address.
        $email = $emailErr = "";

        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            if (empty($_POST["email"])) {
                $emailErr = "* Your email address is required.";
            } else {
                $email = test_input($_POST["email"]);
                if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                    $emailErr = "* Please enter a valid email address.";
                } else {
                    $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
                    $query = "INSERT INTO survey VALUES ('" . $email . "','" . $actual_link . "','','')";
                    mysql_query($query);
                    echo '<script type="text/javascript">'
                        , 'nextPage("survey.php");'
                        , '</script>'
                        ;
                    }
                }
            }

        function test_input($data) {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
        }
     ?>
     <p>
     [Consent form text]
     </p>
     <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
     Please enter your email address: <input type="text" name="email" value="<?php echo $email;?>">
     <span class="error"> <?php echo $emailErr;?></span>
     <br>
     <input type="submit" value="Begin Study">
     </form>
     </div>

As I'm sure you can tell, I'm trying to do some form validation there.

This succcessfully injects the content of the next page ("survey.php") into the consent-form div. "survey.php" includes the following, using the nextPage() function shown above:

$("#csp_form").on('submit', function() {
    nextPage("other.php");
});

But this fails to inject the consent-form div with "other.php".

Now, however, not even the form validation works. According to Firebug, the jquery library has raised some sort of error, even when I comment out all the jquery and javascript functions, and it's stuck in some perpetual loading operation. (This issue has been fixed.)

I am about to start throwing things. I have tried many different other techniques, but none of them worked and I have lost track of them. Hopefully this latest version will be sufficient to get guidance on this.

Sorry all that was so long, but usually people complain that there's too little information, so I wanted to make sure to include everything.

EDIT:

Per request, here's the current full content of survey.php (with the text content changed for privacy purposes):

<script type="text/javascript">
    $("#icecream_form").on('submit', function() {
        nextPage("other.php");
        return false;
    });
</script>

<br>
<h2>What's your favorite ice cream flavor?</h2>
<form method="post" id="icecream_form">
    <input type="radio" name="icecream" value="vanilla"> Vanilla
    <br>
    <input type="radio" name="icecream" value="chocolate"> Chocolate
    <br>
    <input type="radio" name="icecream" value="other"> Something else
    <br>
    <input type="radio" name="icecream" value="none" checked> I don't have a favorite.
    <br>
    <br>
    <input type="submit" name="icecream_submit" value="Go" onsubmit="return(false)">
</form>

After hitting submit, the content from consent-form comes back with an error message from the email form ("Please enter an email address"), implying that the email form was posted again. I cannot comprehend how that is possible if all the stuff from that div had truly been replaced by the stuff in survey.php.

Community
  • 1
  • 1
user124384
  • 400
  • 1
  • 9
  • 22
  • 1
    Add `return false` to the submit handler, to prevent the default form submission. – Barmar Jul 02 '15 at 21:45
  • 1
    What error is the jquery library raising? – Barmar Jul 02 '15 at 21:45
  • 1
    FYI, you don't need to use `.empty()` before `.load()`, since the latter replaces everything in the DIV. – Barmar Jul 02 '15 at 21:46
  • I found out what the deal was with the jQuery library error: apparently it has a bug with Firebug. So I disabled Firebug and now the form validation is working correctly. – user124384 Jul 02 '15 at 22:49
  • I tried adding "return false" to submit, but either I'm doing it wrong or it didn't work. I tried putting just "return false" after calling nextPage(). I tried event.preventDefault() before calling nextPage(). I added onsubmit="return(false)" to the submit button. Am I doing it wrong or is there some other issue? – user124384 Jul 02 '15 at 23:00
  • Thanks for clarifying about .load(). I thought that was the case, but since it didn't work, I thought it might be adding rather than replacing so threw .empty() in there. – user124384 Jul 02 '15 at 23:01
  • `return false;` after `nextPage("other.php");` should do it. – Barmar Jul 02 '15 at 23:04
  • Can you show what `survey.php` returns? – Barmar Jul 02 '15 at 23:07
  • That's one of the things I tried and which didn't work. I'll add info on survey.php in my post. – user124384 Jul 02 '15 at 23:11

1 Answers1

0

I believe the issue was just that I was missing $(document).ready(function () { before $("#icecream_form").on('submit', function() {. This enabled return false; to actually work.

However, I also swapped out the PHP form validation with a jQuery version and deleted the nextPage() function in lieu of just having each page have their own .load(), so those things may also have made a difference.

user124384
  • 400
  • 1
  • 9
  • 22