0

Hello as part of my website project I am writing an item input form, the website itself is very simple, the user will:

  1. Write item name
  2. Select some values
  3. Write item description in textarea
  4. Upload item image

the page will collect this info using JS and then sent to PHP page where it will be checked and then inserted into database. I am not sure what is wrong with the code --php page gives no errors-- as it is not responding after submission, tried resolving by process of elimination, deleting the image part in JS seems to make the button respond though no output is given.

<!DOCTYPE HTML>
<html>
<header>
<title>  Results </title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>

</header>

<body>
<form method="post" enctype="multipart/form-data">
<fieldset>
<legend>Add item form.. </legend>
<p> fill out the below details </p>
<p> Item name will be publicly viewed by users: <input type="text" id="itemID" name="itemName" placeholder="Enter item name.." /> </p> 
location: 
<select id="locSelect"> //4 options for every select
    <option id="mountainID" value="mountain"> Mountain </option>


</select>

Category:
<select id="catSelect">
    <option id="appliancesID" value="appliances"> Appliances </option>

</select>

Price range:
<select id="rangeSelect">
    <option id="range1ID" value="range1"> 0-$50 </option>
</select>

<p> <input type="textarea" id="descriptionID" name="descriptionName" style="height:185px;width:400px;" placeholder="Please enter any item relevant info in this area..." /> </p>
<p> Upload Image: <input type="file" id="itemImageID" name="itemImage" /> </p>


<p> <input type="button" id="addID" name="add" value="Add item" onClick="runAdd();" /> </p>

</fieldset>
</form>

<div id="addResult"></div>
<script type="text/javascript">
function runAdd()
{
    var item = $("#itemID").val();

    var location = document.getElementById("locSelect"); //id of select
    var selectedLoc = location.options[location.selectedIndex].text;

    var category = document.getElementById("catSelect");
    var selectedCat = category.options[category.selectedIndex].text;

    var range = document.getElementById("rangeSelect");
    var selectedRange = range.options[range.selectedIndex].text;

    var textArea = document.getElementById("descriptionID").value;

    var itemImg = document.getElementById("itemImageID");

    $.post("itemDatabase.php", {
    itemDB: item,
    locationDB: selectedLoc,
    categoryDB: selectedCat,
    rangeDB: selectedRange,
    textareaDB: textArea,
    itemImgDB: itemImg  },

    function(data)
    {

        if(data == "int echoed by php page"){
            $("#addResult").html("item/text is blank..");
            //php will echo back to addResult <div> if input is not set
        }


       }
    );

}
</script>
</body>
</html>

I believe I am sending the textarea, select and image parts wrongly, as php echos nothing for them when they are empty. I looked up codes online though they were separate from my case usually involving AJAX or PHP exclusively.

<?php 
session_start(); //for userID, different page stores it
$connection = mysql_connect("localhost", "root", ""); 
$db = mysql_select_db("project_database");


if (isset($_POST['add'])){

if(empty(trim($_POST['itemDB']))) { // if(!($_POST['itemDB'])) not working
echo "0"; }
else { $item = mysql_real_escape_string($_POST['itemDB']); }


if(isset($_POST['locationDB'])){
$location = $_POST['locationDB'];
} else {
echo "1"; }

if(isset($_POST['categoryDB'])){
$category = $_POST['categoryDB'];
} else {
echo "2"; }

if(isset($_POST['rangeDB'])){
$range = $_POST['rangeDB'];
} else {
echo "3"; }

if(empty(trim($_POST['textareaDB']))) {
echo "4"; }
else { $description = mysql_real_escape_string($_POST['textareaDB']); }


if(getimagesize($_FILES['itemImgDB']['tmp_name']) == FALSE)
{
    echo "5";
}   
else 
{
$image = addslashes($_FILES['itemImgDB']['tmp_name']);
$image = file_get_contents($image);
$image = base64_encode($image); 
} 

$query = "INSERT INTO item (item_name, item_description, item_price_range,    item_img, item_location, user_id, category_id) VALUES ('".$item."', '".$description."', '".$range."', '".$image."', '".$location."',    '".$_SESSION["userID"]."', '".$category."')";
$itemAdded = mysql_query($query);
if($itemAdded) {
echo " Item " .$item. " has been added successfully "; }
else { echo " something went wrong "; }

}
?>

category_Id and user_id are foreign keys, Image is stored as BLOB in database (checked code working on different page)

I understand some functions are deprecated, but this is how I was instructed to complete my task, I am using notepad.

I have posted most of the code to ensure I understand what is wrong or how I can improve it in the future, I appreciate any pointers or tips just to get me on the right path at least so I can fix this.

Thank you again for any help in advance.

  • can you include your html as well? – shafeen Aug 09 '15 at 21:07
  • included, edited repeating parts – Carlos Sakr Aug 09 '15 at 21:14
  • have you tried debugging you PHP server side to check if this line: `if (isset($_POST['add'])){` returns a true value? – shafeen Aug 09 '15 at 21:56
  • how to check it's return value? – Carlos Sakr Aug 09 '15 at 22:03
  • any debugger will let you check it, XDebug is a pretty common debugger that is used if you use eclipse or intellij – shafeen Aug 09 '15 at 22:07
  • Also, are you sending a post variable with the name "add" ? If not, then the if statement `if (isset($_POST['add'])){` will not be invoked. Notice in your `$.post` function, you are sending `{ itemDB: item, locationDB: selectedLoc, categoryDB: selectedCat, rangeDB: selectedRange, textareaDB: textArea, itemImgDB: itemImg },` but you aren't sending any variable named `add`, – shafeen Aug 09 '15 at 22:08
  • ah so I should get the button Id and post it, var add = document.getElementById("addID"); is correct? – Carlos Sakr Aug 09 '15 at 22:18
  • If you need the addID to check that it is an add (according to your logic), then yes, go ahead and try that, but I think you should try removing that if statement `if (isset($_POST['add'])){` first and see your php file works as it should, and then go ahead and post the button id like you wanted to. – shafeen Aug 09 '15 at 22:26
  • ok then, thank you, I am fixing most errors php file seems to work, but the image is causing the problems, is the image code in javascript correct? (sending it via $.post) the page gets stuck, but if comment it out it echos some results – Carlos Sakr Aug 09 '15 at 22:29

1 Answers1

2

To answer your question about uploading images via AJAX, this was not possible before but it is now possible via FormData objects (but it is a relatively new feature and is not compatible with older browsers, if that matters to you).

You might either want to upload the image separately (old fashioned), or I suggest you look into using FormData.

This link below should be helpful (I hope):
https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects

Hope this helps :)

shafeen
  • 2,431
  • 18
  • 23
  • This is also another link that illustrates the use of formdata with jQuery: http://stackoverflow.com/questions/2320069/jquery-ajax-file-upload – shafeen Aug 09 '15 at 22:51