I am working on a project to create a Car Showroom System, and I want to make a page where a staff user can edit a vehicle and then asks him to confirm whether he/she wants to save the changes or not. So far I feel like I have gone through all the steps correctly but I am not sure as I am still new to AJAX, I set up my code to ask confirmation from user and if user confirms it updates the vehicle information. This is my code:-
Client Side
<pre>function confirm_edit(){
if(confirm("Save changes?") === true){
var vehicleNo = document.getElementById("vID").value;
var Manufacturer = document.getElementById("Manufacturer").value;
var Model = document.getElementById("Model").value;
var modelYear = document.getElementById("modelYear").value;
var State = document.getElementById("State").value;
var Color = document.getElementById("Color").value;
var Type = document.getElementById("Type").value;
var salePrice = document.getElementById("salePrice").value;
var rentPrice = document.getElementById("rentPrice").value;
var Stock = document.getElementById("Stock").value;
var Description = document.getElementById("Description").value;
var i = document.getElementById("i").files[0];
$.ajax({
url: 'ajax.php',
method: 'post',
data: {vehicleNo: vehicleNo, Manufacturer : Manufacturer, Model: Model, Model_Year: modelYear, State: State, Color: Color, Type: Type, Sale_Price: salePrice, Rent_Price: rentPrice, Stock: Stock, i: i, text: Description},
contentType: false,
processData: false,
success: function(data){
console.log(data);
}
});
return true;
}else{
return false;
}
}
</script>
Server side
<pre><?php
extract($_POST);
if(isset($Manufacturer))
{
if (trim($Manufacturer) == "")
echo "<span style = 'color:red'> Missing Vehicle Manufacturer </span>";
else if(trim($Model) == "")
echo "<span style = 'color:red'> Missing Vehicle Model </span>";
else if(trim($Model_Year) == "")
echo "<span style = 'color:red'> Missing Vehicle Model Year </span>";
else if(trim($State) == "")
echo "<span style = 'color:red'> Missing State of Vehicle </span>";
else if(trim($Color) == "")
echo "<span style = 'color:red'> Missing Color of Vehicle </span>";
else if(trim($Sale_Price) == "")
echo "<span style = 'color:red'> Missing the Sale Price of the Vehicle </span>";
else if(trim($Rent_Price) == "")
echo "<span style = 'color:red'> Missing the Rent Price of the Vehicle </span>";
else if(trim($Stock) == "")
echo "<span style = 'color:red'> Missing Stock of Vehicle </span>";
else if(trim($text) == "")
echo "<span style = 'color:red'> Missing Description of Vehicle </span>";
else
{
if ((($_FILES["i"]["type"] == "image/gif") || ($_FILES["i"]["type"] == "image/jpeg")|| ($_FILES["i"]["type"] == "image/pjpeg") || ($_FILES["i"]["type"] == "image/gif") || ($_FILES["i"]["type"] == "image/png")) && ($_FILES["i"]["size"] < 200000000000000))
{
if ($_FILES["i"]["error"] > 0)
echo "Return Code: " . $_FILES["i"]["error"] . "<br />";
else
{
if (file_exists("Vehicles/" . $_FILES["i"]["name"])){
die($_FILES["i"]["name"] . " already exists <br/>");
echo "<a href = 'add_movie(1).php' align = 'center'> <p style='text-align:center'>Add another Movie </p></a>";
}
else
{
move_uploaded_file($_FILES["i"]["tmp_name"], "Vehicles/" . $_FILES["i"]["name"]);
try{
require('connection.php');
$Description = nl2br($text);
$img = $_FILES['i']['name'];
$usql = "UPDATE vehicles SET License_Plate_N = '$License_Plate', Manufacturer = '$Manufacturer',
Model = '$Model', Model_Year = '$Model_Year', State = '$State', Color = '$Color', Type = '$Type',
Sale_Price = '$Sale_Price', Rent_Price = '$Rent_Price', Stock = '$Stock', Image = '$img', Description = '$Description'";
echo "<span style = 'color:green'><p style='text-align:center'> Vehicle edited successesfully!</p></span> <br/>";
$update = $db->exec($usql);
$db = null;
}
catch(PDOException $e){
die($e->getMessage());
}
}
}
}
else
echo "size or type does not match.";
}
echo "<a href = 'Edit_Vehicle.php' align = 'center'> <p style='text-align:center'>Edit another Vehicle </p></a>";
echo "<p style='text-align:center'>Add Hyper link here to go to main page </p> ";
}
?>
What I have tried:
It looks like it is working fine but I am not sure, I tried checking my console log to see if I am getting any errors but the data is being sent fine and the console log is not showing any errors.