0

Why does my Ajax/jQuery not get the POST value of my input type text in my php file. I always keep getting an error of Undefined index: c_post, I tried looking for solution and even trying to fix it myself but nothing is working and its almost a day. Here is my HTML code. the only thing its inserting in the database and able to Retrieve is the input type="file".

<form method="POST" enctype="multipart/form-data">

  <input type="text" id="c_post" name="c_post" value="Originals" class="form-control">

  <span style="font-weight: bold;">Select Category<span style="color:red;">*</span></span><br>
  <div id="hideme">
    <span style="font-weight: bold;">Select Image Files <span style="color:red;">*</span></span><br>
    <input type="file" name="multiple_files" id="multiple_files" multiple /><br>
    <span class="text-muted">Only .jpg, png, .gif file allowed</span>
    <span id="error_multiple_files"></span>
  </div>

</form>

Here is my Ajax

$.ajax({
  url: "upload.php",
  method: "POST",
  data: form_data,
  contentType: false,
  cache: false,
  processData: false,
  beforeSend: function() {
    $('#error_multiple_files').html('<br /><label class="text-primary">Uploading...</label>');
  },
  success: function(data) {
    $('#error_multiple_files').html('<br /><label class="text-success">Uploaded</label>');
    load_image_data();
  }
});

data: form_data is my
var form_data = new FormData();

Upload.php

//upload.php

include('database_connection.php');
// include_once '../assets/conn/dbconnect.php';
// $radioVal = $_POST["MyRadio"];
// $req = $_REQUEST['test'];

// $inquery = mysqli_query($con,"INSERT into tbl_image (category) VALUES ('$radioVal')");


if(count($_FILES["file"]["name"]) > 0)
{
 //$output = '';
 sleep(3);
 for($count=0; $count<count($_FILES["file"]["name"]); $count++)
 {


  $c_post = $_POST['c_post'];
  $file_name = $_FILES["file"]["name"][$count];
  $tmp_name = $_FILES["file"]['tmp_name'][$count];
  $file_array = explode(".", $file_name);
  $file_extension = end($file_array);
  $mtrand = mt_rand(1, 5000);
  if(file_already_uploaded($file_name, $connect))
  {
   $file_name = $file_array[0] . '-'. rand() . '.' . $file_extension;
  }
  $location = 'files/' . $file_name;
  if(move_uploaded_file($tmp_name, $location))
  {
   $query = "
   INSERT INTO tbl_image (image_name, image_description, category) 
   VALUES ('$file_name', '$mtrand', '$c_post')
   ";
   $statement = $connect->prepare($query);
   $statement->execute();
  }
 }
}

function file_already_uploaded($file_name, $connect)
{

 $query = "SELECT * FROM tbl_image WHERE image_name = '".$file_name."'";
 $statement = $connect->prepare($query);
 $statement->execute();
 $number_of_rows = $statement->rowCount();
 if($number_of_rows > 0)
 {
  return true;
 }
 else
 {
  return false;
 }
}

?>

I just want to add a category and I am not sure why even a simple post won't just let me allow it to happen

This was the project I am trying to make and trying to make different changes to fit my preference

Pedram
  • 12,941
  • 7
  • 35
  • 64
  • where did you defined form_data in JavaScript? did you looked at browser console also to see if there is any error? did you reviewed browser ajax request to see if the parameters being sent or not? – Akam Jan 12 '20 at 06:40
  • I defined it before the $.ajax .. full script can be seen here: https://jsfiddle.net/38bey12g/ | And Ive checked the networks tab and upload.php gives me an error of "Undefined index: c_post". console show error tho. – James DelaCruz Jan 12 '20 at 06:46
  • try to replace `data: form_data` with `data: new FormData()` – Akam Jan 12 '20 at 07:20
  • tried that, and since its already declared as a variable before the $.ajax im not sure how it will change anything. btw doing so, causes the ajax to not be triggered – James DelaCruz Jan 12 '20 at 08:18
  • Hi again, first add `id="myform"` to the form tag `
    `, then edit `new FormData()` to `new FormData(myform)` please do it as I mentioned.
    – Akam Jan 12 '20 at 08:38
  • Your code is vulnerable to SQL injection. You should use parameter binding. – Dharman Jan 12 '20 at 09:36
  • THANKS! After doing so Its now Working ! :) @Akam – James DelaCruz Jan 12 '20 at 09:42
  • Does this answer your question? [How to use FormData for ajax file upload](https://stackoverflow.com/questions/21044798/how-to-use-formdata-for-ajax-file-upload) – Akam Jan 12 '20 at 10:37

0 Answers0