1

I am using echo to grab a form from another page like this

<div id = "consult-form-wrapper">
   <?php include('consultation.php') ?>
</div>

The actual form

<div id="acordeon">
<div class="panel-group" id="accordion">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" href="#collapseThree">
          Consult Form
        </a>
      </h4>
  </div>
        <form name ="consult-form" method="post" id="consult-form">
            <div class = "col-md-8">
                <div id="collapseThree" class="panel-collapse collapse">
                  <div class="panel-body">
                        <h5>Why did you sign up?</h5>
                         <textarea class="form-control" name = "q1" id="q1"></textarea>
                      </div>

                        <h5>What are your goals?</h5>
                         <textarea class="form-control" name = "q2" id="q2"> </textarea>

                    <div class="footer text-center">
                        <button type="submit" class="btn btn-fill btn-success" onClick="submitconsult()"
                            >Submit</button>
                         <button id="reset" class="btn btn-fill btn-danger" type="reset">Reset</button>
                    </div>
                </div>

            </div>
        </form>
  </div>
</div>
</div>

The sql code

$comment = $_POST['q1'];
$id2=$_SESSION['new_id'];
$id4 = '1026';

try {
        $pbr = $conn->prepare("UPDATE `memberInfo` mi
                       INNER JOIN `loginInfo` AS li
                       ON li.userID = mi.UserID
                       SET `q1` = ?
                       WHERE mi.userID = ?");
        //mysqli_real_escape_string($conn, $q1);
        $pbr->bind_param("ss", $comment, $id2);
        $pbr->execute();

javascript code

function submitconsult() {
    var data = $('#consult-form').serialize();
    $.ajax({
        type: "POST",
        url: "postconsult.php",
        data: data,
        cache: false,
        success: function(html) {
        //alert(html);
        //document.getElementById('regform').reset();
        }
        });
}

If I navigate directly to consultation.php it posts fine but if I try doing it from the page I am echo'ing it from it just submits as empty. I thought that maybe it wasn't grabbing the session id ($id2) so I hardcoded the id ($id4) and still the same problem.

1 Answer 1

1

you are already submitting the form once with the submit button

<button type="submit" class="btn btn-fill btn-success" onClick="submitconsult()">Submit</button>

and then again through the submitconsult() function. You need to alter the function to prevent the first submission so that you can do the Ajax one:

function submitconsult(evt) {
  evt.preventDefault();....

or simply remove the submit type from the button and move the button out of the form and then call the Ajax function without automatically triggering the form submission.

Sign up to request clarification or add additional context in comments.

4 Comments

The submit listener should be on the form, not the submit button. Forms can be submitted without clicking the submit button.
I added the one line and the parameter but it still doesnt work.
Change the listener to: onclick="submitconsult(event)". Inline listeners don't pass the event object the way that ones attached using addEventListener do. Simpler to put the listener on the form: onsubmit="submitconsult();return false" and kill two birds with one stone.
I just amended my post - another way to do it is to "..remove the submit type from the button and move the button out of the form and then call the Ajax function as te onclick event without automatically triggering the form submission."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.