noname
noname

Reputation: 305

Form submits null value

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.

Upvotes: 1

Views: 1089

Answers (1)

gavgrif
gavgrif

Reputation: 15489

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.

Upvotes: 1

Related Questions