0

Hello i am trying to update data taken from modal form with ajax. I submit the data correctly but for some reason they do not get updated in database.

My Javascript code:

<script>
    jQuery(document).ready(function ($) {
        var modalId = '<?php echo $row['csid']?>';

        var feedback = null;
        var homepage_teaser = null;
        var review_comment = null;
        var review_visible_website = null;
        var dataString = null;

        var review_rating = '<?php echo $row['rating']?>';
        var booking_csid = '<?php echo $row['csid']?>';
        var property_id = '<?php echo $row['currentPropertyId']?>';
        var review_customer_id = '<?php echo $row['cusid']?>';

        $("#saveButton_"+modalId).click(function () {

            feedback = $('#feedback_'+ modalId).val();
            homepage_teaser = $('#teaser_'+ modalId).val();
            review_comment = $('#feedbackcomment_'+ modalId).val();
            review_visible_website = $('#checkbox_'+ modalId).val();

            dataString = 'feedback='+ feedback + '&homepage_teaser='+ homepage_teaser + '&review_comment=' + review_comment + '&review_visible_website=' + review_visible_website+ '&review_rating=' + review_rating+ '&booking_csid=' + booking_csid+ '&property_id=' + property_id + '&review_customer_id=' + review_customer_id;
            $.ajax({
                type: "POST",
                url: "ajax/feedback_overview_update.php",
                data: dataString,
                cached: false,
                success: function () {
                    alert("Updated: " + dataString);
                },
                error: function () {
                    alert("failure");
                }
            });
            console.log("Modal Submited: " + modalId);
            console.log("Modal Data: " + dataString);
        });
    });
</script>

and the php script:

try{
        $handler = new PDO("mysql:host=$dbhost;dbname=$dbname", "$dbuser", "$dbpass");
        $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $handler->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES utf8mb4 ");
        $handler->exec("SET CHARACTER SET UTF-8 ");
    }catch(Exception $e){
        echo $e->getMessage();
        die();
    }

    $feedback = $_GET['feedback'];
    $homepage_teaser = $_GET['homepage_teaser'];
    $review_comment = $_GET['review_comment'];
    $review_visible_website = $_GET['review_visible_website'];
    $review_rating = $_GET['review_rating'];
    $booking_csid = $_GET['booking_csid'];
    $property_id = $_GET['property_id'];
    $review_customer_id = $_GET['review_customer_id'];


    $updatefeedback = $handler->prepare("UPDATE reviews SET feedback = ?, homepage_teaser = ?, review_comment = ?, review_visible_website = ?, review_rating = ?, review_checked = 'yes', review_checked_date = NOW() WHERE booking_csid = ? AND property_id = ? AND review_customer_id = ?");

    $updatefeedback->bindValue(1, $feedback);
    $updatefeedback->bindValue(2, $homepage_teaser);
    $updatefeedback->bindValue(3, $review_comment);
    if ($review_visible_website == 'on') {
        $updatefeedback->bindValue(4, 'yes');
    } else {
        $updatefeedback->bindValue(4, 'no');
    }



    $updatefeedback->bindValue(5, $review_rating);
    $updatefeedback->bindValue(6, $booking_csid);
    $updatefeedback->bindValue(7, $property_id);
    $updatefeedback->bindValue(8, $review_customer_id);

    $updatefeedback->execute();

    if ($updatefeedback) {
        echo "Success";
    } else {
        echo "Failure";
    }

And here is the dataString value:

Modal Data: feedback=Sehr schön&homepage_teaser=this is homepage&review_comment=this is feedback comment&review_visible_website=on&review_rating=3.25&booking_csid=1679&property_id=87&review_customer_id=22

But still they are not updated. What am i doing wrong?

4
  • you are using $_GET in php. replace $_GET with $_POST Commented Jun 24, 2016 at 13:15
  • still does not update data Commented Jun 24, 2016 at 13:18
  • Your short still does not update comments are not really helpfull at all. Give us some debug output or informations if you want further help. We can't execute your script on our side ... Commented Jun 24, 2016 at 13:30
  • still does not update means that i get 200 OK as response in the .php file, i get the output as expected but the records in the mysql database are not updated Commented Jun 24, 2016 at 13:31

3 Answers 3

3

You are using type: "POST", in your ajax request and trying to accessing it using $_GET.

Change all $_GET[]; to $_POST[]; in your php

$feedback = $_POST['feedback'];
$homepage_teaser = $_POST['homepage_teaser'];
$review_comment = $_POST['review_comment'];
$review_visible_website = $_POST['review_visible_website'];
$review_rating = $_POST['review_rating'];
$booking_csid = $_POST['booking_csid'];
$property_id = $_POST['property_id'];
$review_customer_id = $_POST['review_customer_id'];

Change dataString as below

dataString = {'feedback': feedback , 'homepage_teaser': homepage_teaser ,
'review_comment': review_comment ,
'review_visible_website':review_visible_website,
'review_rating':  review_rating, 'booking_csid':  booking_csid,
'property_id':  property_id , 'review_customer_id': review_customer_id};
Sign up to request clarification or add additional context in comments.

5 Comments

If still not updated then print_r($_POST) or see browser console if your dataString are going through ajax or not
how exactly do i see if it goes through ajax or not?
Write this in the first line of your php script: print_r($_POST); die();. Then you should see all posted data from ajax request.
guess what the problemm was: I was doing this: dataString = {'feedback': feedback +','+ 'homepage_teaser': homepage_teaser }; thanks a lot!!!
Glad it work now! If you want to, take a look at my answer. I shortend your whole code a bit more. There is much unneeded code. ;)
2

There are two issues with your code. The correct way to pass data in AJAX is using key value pairs. Like this

{"key":"value"}

You're sending the following POST data in AJAX. (This method can be used when you're using GET method to submit data and appending string along with URL)

'feedback='+ feedback + '&homepage_teaser='+ homepage_teaser + '&review_comment=' + review_comment + '&review_visible_website=' + review_visible_website+ '&review_rating=' + review_rating+ '&booking_csid=' + booking_csid+ '&property_id=' + property_id + '&review_customer_id=' + review_customer_id;

That should be replaced with key/value pairs code. Like this:

data: {'feedback':feedback,'homepage_teaser':homepage_teaser} //add other params same like this

Second issue is you're trying to retrive data as $_GET as it should be $_POST, as you're posting data as AJAX POST. So replace your following PHP Code:

    $feedback = $_POST['feedback'];
    $homepage_teaser = $_POST['homepage_teaser'];
    $review_comment = $_POST['review_comment'];
    $review_visible_website = $_POST['review_visible_website'];
    $review_rating = $_POST['review_rating'];
    $booking_csid = $_POST['booking_csid'];
    $property_id = $_POST['property_id'];
    $review_customer_id = $_POST['review_customer_id'];

1 Comment

Try to echo above variables in PHP script and see if you're actually getting data in PHP or not. If you're getting data in PHP there must be something wrong with SQL connection/command.
2

Shrink your code. Why so much overhead? Use an object to pass the data to ajax function.

$(function() {
    var csid = '<?php echo $row['csid']?>',
        review_rating = '<?php echo $row['rating']?>',
        property_id = '<?php echo $row['currentPropertyId']?>',
        review_customer_id = '<?php echo $row['cusid']?>';

    $("#saveButton_" + csid ).click(function() {
        $.ajax({
            type: "POST",
            url: "ajax/feedback_overview_update.php",
            cached: false,
            data: {
                feedback: $('#feedback_' + csid).val(),
                homepage_teaser: $('#teaser_' + csid).val(),
                review_comment: $('#feedbackcomment_'+ csid).val(),
                review_visible_website: $('#checkbox_'+ csid).val(),
                review_rating: review_rating,
                booking_csid: csid,
                property_id: property_id,
                review_customer_id: review_customer_id
            },
            success: function() {
                alert("Updated");
            },
            error: function () {
                alert("failure");
            }
        });

        console.log("Modal Submited: " + csid);
    });
});

And in your PHP, change $_GET to $_POST, because your ajax has type: 'POST'

$feedback = $_POST['feedback'];
$homepage_teaser = $_POST['homepage_teaser'];
$review_comment = $_POST['review_comment'];
$review_visible_website = $_POST['review_visible_website'];
$review_rating = $_POST['review_rating'];
$booking_csid = $_POST['booking_csid'];
$property_id = $_POST['property_id'];
$review_customer_id = $_POST['review_customer_id'];

1 Comment

Is your SQL-Connection working? Make a print_r($_POST) in your php script and see if the posted data is there.

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.