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