Click here to Skip to main content
14,975,411 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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];
          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},//$('form').serialize(),
          contentType: false,
          processData: false,
          success: function(data){
        return true;
        return false;

Server side
      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>";
        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 />";
            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>";
              //echo $_FILES["i"]["name"] . " already exists <br/>";
              move_uploaded_file($_FILES["i"]["tmp_name"], "Vehicles/" . $_FILES["i"]["name"]);
                  $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){

        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> ";//"<a href = 'MainPage.php' align = 'center'> Go back to mainpage </a>" Use Hyperlink to go to main page

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.
Kornfeld Eliyahu Peter 15-Dec-19 4:50am
It seems all good - as you already stated... The question is: does the data stored in DB?
Qassim Ali 15-Dec-19 5:09am
No nothing happens which is weird. I tried submitting it multiple times but it never updates in the database, i will try to double check might have missed something.
Kornfeld Eliyahu Peter 15-Dec-19 5:38am
I can spot some problems in the JQuery code...
* method should be type
* data should be serialized to string
* remove processData
Qassim Ali 15-Dec-19 5:51am
I assumed since I am sending the information to server side the method should be post but if that is not the case can you elaborate more on the first two points and link me maybe to some source on why and how to do them?
Kornfeld Eliyahu Peter 15-Dec-19 5:54am
The parameter name is not 'method' but 'type' - the value is correctly post
Kornfeld Eliyahu Peter 15-Dec-19 5:55am
In the data parameter do not pass object but string...
Qassim Ali 15-Dec-19 7:36am
I tried doing all the steps you told me about and this is how my client side ajax request looks like
          url: 'ajax.php',
          type: 'post',
          data: JSON.stringify({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}),//$('form').serialize(),
          contentType: false,
          success: function(data){

the database still doesn't get updated.
Kornfeld Eliyahu Peter 15-Dec-19 7:49am
Now the question is: does the data arrives to the server? What $_POST contains?
Qassim Ali 15-Dec-19 8:09am
So when I add JSONG.stringify to my data it doesnt show in the console but when I remove it it shows. Am I not serializing my data to string properly?
Kornfeld Eliyahu Peter 15-Dec-19 8:29am
What do you mean by 'console'? Have you try the 'stringify' method out of the context of the ajax call?
Qassim Ali 15-Dec-19 8:38am
You mean make the object outside of the ajax call and then use it inside? can you write me a sample on how that would look and how to access it on php side because I am not familiar with objects in PHP.
Qassim Ali 15-Dec-19 5:25am
So I messed around a bit more and it looks like the problem is
 contentType: false,
          processData: false,
in my client side code. When I include it the data is not being sent for whatever reason, but if I remove it I get an error saying (TypeError: 'slice' called on an object that does not implement interface Blob). I am hoping this information can clear some things up.
i = document.getElementById("i").files[0]; convert to byte or bytestring then send
Qassim Ali 15-Dec-19 8:40am
How can I convert it to byte?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900