0

I am trying to submit my form with ajax and cant seem to get it to work. I have tried different methods but data is still not sent to database. Below is my code

form

<form action='sendquestion.php' method='post' id="sendQform" enctype="multipart/form-data">
    <input type='text' name='searchQuestion' id='searchQuestion'placeholder='Enter your question'><br>
    <input type="text" name="user" id="user" value="<?php echo ''.$_SESSION['log_id'].'';?>" style="display: none">
    <input type="text" name="qDate" id="qDtae" value="<?php echo date('d/M/Y');?>" style="display: none">
    <input type="text" name="status" id="status" value="0" style="display: none">
    <label for='sendSearchQuestion'><i class='fa fa-send'></i></label>
    <input type="submit" name="sendSearchQuestion" id="sendSearchQuestion" value="Send">
</form>

Ajax

$('#sendQform').submit(function(e) {
    e.preventDefault();
    var $form = $(this), url = $form.attr("action");

    var posting = $.post(url, {searchQuestion: $('#searchQuestion').val(), user: $('#user').val(), status: $('#status').val(), qDate: $('#qDate').val()});
    posting.done(function(data) {
        alert("success");
    });
});

PHP

<?php
session_start();
require_once ("db.php");
$db = new MyDB();

if (isset($_POST['sendSearchQuestion']))
{
    $question = strip_tags(@$_POST['searchQuestion']);
    $askid = htmlentities($_POST['user'], ENT_QUOTES, 'UTF-8');
    $status = strip_tags(@$_POST['status']);
    $date = htmlentities($_POST['qDate'], ENT_QUOTES, 'UTF-8');

    $sql = $db->prepare("INSERT INTO userquestions (userid, question, status, dates) VALUES (?, ?, ?, ?)");
    $sql->bindParam(1, $askid, SQLITE3_INTEGER);
    $sql->bindParam(2, $question, SQLITE3_TEXT);
    $sql->bindParam(3, $status);
    $sql->bindParam(4, $date);

    $result = $sql->execute();

    if ($result)
    {
        echo "true";
    }
    else
    {
        echo "false";
    }
}
?>

Why is this not working. Thanks.

diagold
  • 493
  • 1
  • 7
  • 28
  • 1
    [Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server?](http://jayblanchard.net/basics_of_jquery_ajax.html) – Jay Blanchard Aug 10 '17 at 16:02
  • 3
    `$_POST['sendSearchQuestion']` will never be set – Rory McCrossan Aug 10 '17 at 16:03
  • 1
    What debugging have you done? What _is_ happening? Just saying that something "doesn't work" doesn't really help us help you. – Patrick Q Aug 10 '17 at 16:03
  • `value=""` looks wrong. Why are you echoing single-quotes here? – Jay Blanchard Aug 10 '17 at 16:04
  • I'm not sure if a space here matters `id='searchQuestion'placeholder='Enter your question'` – Jay Blanchard Aug 10 '17 at 16:05

1 Answers1

1

You logic test is for sendSearchQuestion and your fild is searchQuestion

Change

if (isset($_POST['sendSearchQuestion']))

to

if (isset($_POST['searchQuestion']))