I've got a button which triggers a function on click
<input type="button" value="Change Stuff" onClick="Change()" />
This is the said function :
function Change()
{
var z = confirm("You are about to change something!");
if (z){
var value1 = prompt("Change Value1 : ");
var value2 = prompt("Change Value2 : ");
var value3 = prompt("Change Value3 : ");
$.ajax({
url: ('ajax-call.php'),
type: 'POST',
data: {
id: '<?php echo $_GET["id"]; ?>',
value1: value1,
value2: value2,
value3: value3
},
dataType: 'script'
});
} else {
return false;
}
}
The data beeing called by AJAX
<?php
include "connect.php";
$id = $_POST["id"];
$value1 = $_POST["value1"];
$value2 = $_POST["value2"];
$value3 = $_POST["value3"];
$currentDate = date("Y-m-d");
$sql = $conn->query("UPDATE tbl_table1 SET datum='". $currentDate ."',value1='". $value1 ."', value2='". $value2 ."',value3='". $value3 ."',einstellung='". 2 ."' WHERE id =" . $id) or die('Error Updating table_1 - Error Message : ' .mysqli_error($conn));
if ($conn->affected_rows == 0){
echo '<script type="text/javascript"> alert(\'No data found!\'); </script>';
} else {
echo '<script type="text/javascript"> alert(\'Success!\'); </script>';
$result = $conn->query("UPDATE tbl_table2 SET status='" . 1 ."' WHERE id =" . $id) or die('Status could not be set - Error Message : ' .mysqli_error($conn));
}
mysqli_free_result($sql);
mysqli_free_result($result);
?>
Not sure if important but also in my code (reload to see ticked checkbox) :
$(document).ajaxStop(function(){
location.reload(true);
});
My only problem is : the alerts never appear. I tried to reconstruct it on a test page with given variable values and not $_POST's and it alerted when I called the site directly.
I don't know much about AJAX and this is actually my first time working with it..So my question would
be, is AJAX blocking alert() or is there a mistake in my code?
The Main idea behind this was : Check if the id is in the table, if it is update some values and then inform that a match was found. Then update table2. Else just alert that the id is not in the table.
you should be use callbak function for ajax request. After that , According to response , In callback , you should call alert function
if u reload page that will reset ur page to default. u should use
ajax setting like that:
$.ajax({
url: ('ajax-call.php'),
type: 'POST',
data: {
uid: '<?php echo $_GET["id"]; ?>',
value1: value1,
value2: value2,
value3: value3
},
dataType: 'script',
success: function(response){
alert(response);
}
});
try like this
function Change()
{
var z = confirm("You are about to change something!");
if (z){
var value1 = prompt("Change Value1 : ");
var value2 = prompt("Change Value2 : ");
var value3 = prompt("Change Value3 : ");
$.ajax({
url: ('ajax-call.php'),
type: 'POST',
data: {
id: '<?php echo $_GET["id"]; ?>',
value1: value1,
value2: value2,
value3: value3
},
dataType: 'script'
}).done(function (msg) {
alert( msg );
}
document.location = "cableList.php";
});;
} else {
return false;
}
}
PHP
<?php
include "connect.php";
$id = $_POST["id"];
$value1 = $_POST["value1"];
$value2 = $_POST["value2"];
$value3 = $_POST["value3"];
$currentDate = date("Y-m-d");
$sql = $conn->query("UPDATE tbl_table1 SET datum='". $currentDate ."',value1='". $value1 ."', value2='". $value2 ."',value3='". $value3 ."',einstellung='". 2 ."' WHERE id =" . $id) or die('Error Updating table_1 - Error Message : ' .mysqli_error($conn));
if ($conn->affected_rows == 0){
echo 'No data found!';
} else {
echo 'Success';
$result = $conn->query("UPDATE tbl_table2 SET status='" . 1 ."' WHERE id =" . $id) or die('Status couldn't be set - Error Message : ' .mysqli_error($conn));
}
mysqli_free_result($sql);
mysqli_free_result($result);
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.