0

When i submit form it doesnot update value in database mysql.Its a form written in php. here is my php and html. I want that the form should not reload and it must submit the changes in database without reloading the page and show preloader for 1 sec on submitting form. HTML,PHP AND ACTION of form: Here action is the current page where this form is avalilable

detail_customer.php

<?php


$server = "localhost";
$username = "root";
$pass = "";
$dbname = "stinkspolitics_pl";

$conn = mysqli_connect($server, $username, $pass, $dbname);



if (isset($_GET['detail_customer'])) {
    $quest_id = $_GET['detail_customer'];

    $get_quest = "SELECT * FROM questions WHERE quest_id = '$quest_id'";
    $getting_quest = mysqli_query($conn, $get_quest);

    while ($row = mysqli_fetch_assoc($getting_quest)) {
        $quest_title = $row['quest_title'];
        $category_id = $row['category_id'];
    }
}


if (isset($_POST['submit'])) {

    $quest_t = $_POST['quest_t'];

    $update = "UPDATE questions SET quest_title = '$quest_t' WHERE quest_id = '$quest_id'";
    $run_update = mysqli_query($conn, $update);
    if ($run_update) {
        echo 'hello';
    }
}

?>


<div class="recent-orders cust_det ">
    <h2> Customer Detail</h2>
    <div class="customer_detail">
        <form id="form-submit" action="./inc/detail_customer.php
        " method="POST" class="c_form animate__animated animate__fadeIn">



            <div class='alert alert-success'>
                <strong>Success!</strong> Your question has been submitted.
            </div>


            <div class='alert alert-danger'>
                <strong>Sorry!</strong> Your question has not been submitted.
            </div>



            <div class="row">
                <div class="c_detail">
                    <label for="" class="form-labels">Name</label>
                    <input type="text" name="cat_id" value="<?php echo $category_id  ?>" id="cat_id">
                </div>
                <div class="c_detail">
                    <label for="" class="form-labels">Contact</label>
                    <input type="text" name="quest_t" value="<?php echo $quest_title  ?>" id="quest_t">
                </div>
                <div class="c_detail">
                    <label for="" class="form-labels">City</label>
                    <input type="text" name="" id="">
                </div>
            </div>
            <div class="row">
                <div class="c_detail">
                    <label for="" class="form-labels">Name</label>
                    <input type="text" name="" id="">
                </div>

                <div class="c_detail">
                    <label for="" class="form-labels">Contact</label>
                    <input type="text" name="" id="">
                </div>
                <div class="c_detail">
                    <label for="" class="form-labels">City</label>
                    <input type="text" name="" id="">
                </div>
            </div>
            <div class="row">
                <input name="submit" type="hidden" />
                <input class="btn-primary submit-btn" type="submit" name="" value="Submit">
            </div>
        </form>
    </div>
</div>

JS Code index.js

$("#form-submit").on("submit", function () {
    // e.preventDefault();
    var form = $(this);
    var formData = form.serialize();

    $.ajax({
      type: "POST",
      url: form.attr("action"),
      data: formData,
      success: function (data) {
        $(".alert-success").show();
        $(".alert-success").fadeOut(4000);
        console.log(data);
      },
      error: function (data) {
        $(".alert-danger").show();
        $(".alert-danger").fadeOut(4000);
        console.log(data);
      },
    });
    return false;
  });

Ajax Success Response But not updating data in mySQL

<div class="recent-orders cust_det ">
    <h2> Customer Detail</h2>
    <div class="customer_detail">
        <form id="form-submit" action="./inc/detail_customer.php" method="POST"
            class="c_form animate__animated animate__fadeIn">



            <div class='alert alert-success'>
                <strong>Success!</strong> Your question has been submitted.
            </div>


            <div class='alert alert-danger'>
                <strong>Sorry!</strong> Your question has not been submitted.
            </div>



            <div class="row">
                <div class="c_detail">
                    <label for="" class="form-labels">Name</label>
                    <input type="text" name="cat_id" value="<br />
<b>Warning</b>:  Undefined variable $category_id in <b>C:\xampp\htdocs\admin_panel\inc\detail_customer.php</b> on line <b>62</b><br />
" id="cat_id">
                </div>
                <div class="c_detail">
                    <label for="" class="form-labels">Contact</label>
                    <input type="text" name="quest_t" value="<br />
<b>Warning</b>:  Undefined variable $quest_title in <b>C:\xampp\htdocs\admin_panel\inc\detail_customer.php</b> on line <b>66</b><br />
" id="quest_t">
                </div>
                <div class="c_detail">
                    <label for="" class="form-labels">City</label>
                    <input type="text" name="" id="">
                </div>
            </div>
            <div class="row">
                <div class="c_detail">
                    <label for="" class="form-labels">Name</label>
                    <input type="text" name="" id="">
                </div>

                <div class="c_detail">
                    <label for="" class="form-labels">Contact</label>
                    <input type="text" name="" id="">
                </div>
                <div class="c_detail">
                    <label for="" class="form-labels">City</label>
                    <input type="text" name="" id="">
                </div>
            </div>
            <div class="row">
                <input name="submit" type="hidden" />
                <input class="btn-primary submit-btn" type="submit" name="" value="Submit">
            </div>
        </form>
    </div>
</div>

3 Answers3

1

The condition if (isset($_POST['submit'])) { will never evaluate to true, since there is no input element in the form with name="submit" (the button with name='submit' does not send the attribute by default).

Either change the condition:

if (isset($_POST['quest_t'])) { ...

Or, include an input element with name='submit', for example:

<input name="submit" type="hidden" />

Also, make sure to move the $_POST check at the beginning of the file and ensure that no other code will be evaluated in the PHP file (e.g. the rest of the HTML code) if a POST request has been received.

TimBrownlaw
  • 5,457
  • 3
  • 24
  • 28
Kostas Minaidis
  • 4,681
  • 3
  • 17
  • 25
  • I have editted question with new code and it still not works :( – Muhammad Uzair Apr 30 '22 at 23:43
  • An input with type submit is still not sending the name=submit key value pair – Kostas Minaidis Apr 30 '22 at 23:58
  • Add an input with type="hidden" and name="submit" and check again @MuhammadUzair – Kostas Minaidis Apr 30 '22 at 23:59
  • Still not working – Muhammad Uzair May 01 '22 at 00:01
  • Code updated above with changes. – Muhammad Uzair May 01 '22 at 00:02
  • There is already a Submit button in the Form with a Blank Name. So why would adding a "hidden" input with a name of "submit" be a good idea? Just change the existing submit input name="" to name="submit". – TimBrownlaw May 02 '22 at 01:11
  • 1
    An input element with the `type` attribute set to submit, does not actually submit the name key/value pair. Since the PHP file was checking to see whether a name="submit" is being submitted, I suggested that he try this name attribute on another type of input element just to receive the key/value pair on the backend. This was meant as a quick debuggin advice. Further refactoring is needed on many parts of the code, but we have to start somewhere. @TimBrownlaw – Kostas Minaidis May 02 '22 at 07:08
0

Just return false after at the end of the method.

jQuery has its own way of ensuring "preventDefault()" and it is just returning false from the submit handler.

Full background on this here:

event.preventDefault() vs. return false

Blunt Jackson
  • 616
  • 4
  • 17
0

Issue has been solved. By changing path and then putting path of js file again.