BRG BRG - 5 months ago 17
SQL Question

Jquery Ajax does not update values in mysql

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?

Answer

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};
Comments