2

got a strange problem today. So I was trying to insert data into database using AJAX so the page wouldn't need to reload to see changes. But when i try to insert data, dont know how to put it, the insert.php file does not get "triggered", e.g the line "Hello Im insert.php" is not shown. But the ajax code works fine, success block gets triggered.

form:

<form action="insert.php" method="post" class="form-inline" id="forma">
    <input type="text" class="form-control" name="inputName" id="inputName" placeholder="Name">
    <input type="text" class="form-control" name="inputPrice" id="inputPrice" placeholder="Price">
    <input type="text" class="form-control" name="inputStore" id="inputStore" placeholder="Store">
    <input type="text" class="form-control" name="inputDate" id="inputDate" placeholder="Date"></br>
    <button id="submit" type="submit" class="btn btn-success">Submit</button>
</form>

jQuery:

$('#forma').submit(function() {
    var name = $("#inputName").val();
    var price = $("#inputPrice").val();
    var store= $("#inputStore").val();
    var date = $("#inputDate").val();

    $.ajax({
        type: "POST",
        url: "insert.php",
        data: {
            inputPavadinimas: name,
            inputKaina: price,
            inputParduotuve: store,
            inputData: date,
        },
        success: function() {
            $("#success").show();
            alert('success');
        },
        error: function(){
            alert('error');
        }
    });
    return false;    
});

insert.php:

    <?php
    error_reporting(0);
    require 'db/connection.php';

    echo 'Hello Im insert.php';

    if(!empty($_POST)){
        print_r($_POST);
        if(isset($_POST['inputName'],$_POST['inputPrice'],$_POST['inputStore'],$_POST['inputDate'])){
        $name = trim($_POST['inputName']);
        $price = trim($_POST['inputPrice']);
        $store = trim($_POST['inputStore']);
        $date = trim($_POST['inputDate']);

        if(!empty($name) && !empty($price) && !empty($store) && !empty($date)){
            $insert = $db->prepare("INSERT INTO prekes(name, price, store, date) VALUES (?, ?, ?, ?)");
            $insert->bind_param('ssss',$name, $price, $store, $date);

            if($insert->execute()){
                echo 'INSERTED';
            }
        }
    }
};
3
  • either record is not inserted or only echo values are not printed? which one is your problem? Commented Jun 27, 2015 at 21:19
  • your isset statement doesn't evaluate true because you're not passing those parameters Commented Jun 27, 2015 at 21:35
  • what this character }; last line at insert.php. this is valid character for php? Commented Jun 27, 2015 at 22:28

3 Answers 3

2

To alter the data from the PHP script echo you need to change your success function like so.

    success: function(data) {
        $("#success").show();
        alert(data);
    },

success Type: Function( Anything data, String textStatus, jqXHR jqXHR ) A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter or the dataFilter callback function, if specified; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.

Here you can find more information on jQuery.ajax()

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

Comments

1

Try

$('#forma').submit(function(event) {
    var name = $("#inputName").val();
    var price = $("#inputPrice").val();
    var store= $("#inputStore").val();
    var date = $("#inputDate").val();

    $.ajax({
        type: "POST",
        url: "insert.php",
        data: {
            inputPavadinimas: name,
            inputKaina: price,
            inputParduotuve: store,
            inputData: date,
        },
        success: function(data) {
            $("#success").html(data);
            $("#success").show();
            alert(data);
        },
        error: function(data){
            alert('error');
        }
    });
    event.preventDefault();   
});

added $("#success").html(data);, success client side is not same with success on insert.php

And change this part (insert.php):

       if(!empty($name) && !empty($price) && !empty($store) && !empty($date)){
            $insert = $db->prepare("INSERT INTO prekes(name, price, store, date) VALUES (?, ?, ?, ?)");
            $insert->bind_param('ssss',$name, $price, $store, $date);

            if($insert->execute()){
                echo 'INSERTED';
            }else{
                echo 'SOMETHING WRONG';
            }
        }else{
            echo 'ALL DATA CAN'T EMPTY';
        }

Comments

0

it might be this line:

error_reporting(0)

this line cause any fatal error to stop the script, but the page will return empty response with status code 200 (OK) which will cause the ajax to trigger the success event even tough an error happened in the server side

Comments

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.