0

UPDATE 10/16
Updated HTML
Updated AJAX

Attached screenshot screenshot2

Issue: The form technically "submits", however a) my form no longer refreshes itself which tells me my php file is not being called/executed properly and b) the info is not being posted to the DB. I think this has to do something with the conten-type: false, but I am not completely sure...

Let me start by saying, I've read and read about how to go about doing this. Some posts I've read this can't be done and then others prove them wrong. I tried to implement some examples, but for some reason all of the examples laid out do not work for me. I thought I'd see if someone can solve my specific issue.

Essentially, I have a semi-html/jquery form that I post via AJAX. I did this because a) I essentially have 3 separate forms (not shown in this example) on the page and b) I need to return the same form to the page without reloading the page...

My problem is that when I select my image and click on my button, the ajax DOES NOT send the image to PHP, although it does send the other fields. What am I doing wrong here? Any updates to my code would be most useful as again, I've attempted to implement several different answers in the past with no luck.

Any assistance would be MUCH MUCH appreciated. I am on the cusp of finishing this project and this is one of two major barriers for me.

html (please forgive the inline styles...I haven't yet finished my CSS files)

<div style="position: relative; float: left; width:275px;">
<div id="valuebox" style="position: relative; float: left; width:275px; border: solid #0096D6; border-width: 1px; padding: 10px;">
<H2>Step 3: Enter Value Level Data</H2>
 <form enctype="multipart/form-data">
 <span style="position: relative; float: left; display: inline-block; margin-top: 7px; font: 12px Lucida Grande,Helvetica,Arial,Verdana,sans-serif; padding-right: 60px;">
<p>Add Value Challenger Screenshot:</p>
<input id="file" type="file" name="valueimage">
</span>
<span style="float: left; clear: right; margin-top:8px; padding-top: 10px; width: 235px;">
<label class="fieldlabel"><span>Value Lift (%):</span></label></br>
 <input id="valuelift" type="text" name="valuelift" class="textfieldshadowsmall" style="width: 150px;">
</span>
<span style="position: relative; float: left; margin-top: 25px; font: 12px Lucida Grande,Helvetica,Arial,Verdana,sans-serif;">
<input id="valuesignificant" type="checkbox" name="valuesignificant" value="1">Significant?
</span>
<span style="position: relative; float: left; margin-top: 25px; font: 12px Lucida Grande,Helvetica,Arial,Verdana,sans-serif;">
<input id="valuewinningcreative" type="checkbox" name="valuewinningcreative" value="1">Winning Creative?
</span>
 </form>
</div>
<span style="position: relative; float: left; margin-top: 25px; font: 12px Lucida Grande,Helvetica,Arial,Verdana,sans-serif;">
<a href="#" id="valuesubmit" />+ add another value</a>
</span>
</form>
</div>

jquery/ajax

$(function(){
  $('#valuesubmit').click(function(event) {
var formData = new FormData($('form')[0]);

$.ajax({
    url: 'post_value_dummy.php',  //Server script to process data
    type: 'POST',
    xhr: function() {  // Custom XMLHttpRequest
        var myXhr = $.ajaxSettings.xhr();
        //if(myXhr.upload){ // Check if upload property exists
           // myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
        //}
        return myXhr;
    },
    // Form data
    enctype: 'multipart/form-data',
    data: formData,
    //Options to tell jQuery not to process data or worry about content-type.
    cache: false,
    contentType: false,
    processData: false
});
});
});

php

//This is the directory where images will be saved
$target = "/screenshots/";
$target = $target . basename($_FILES[valueimage][name]);

$picchallengervalue=($_FILES['valueimage']['name']);


$con=mysqli_connect("x","y","z","a");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


$sql="INSERT INTO value (valueimage, valuelift, valuesignificant, valuewinningcreative, variableid)
VALUES
('$picchallengervalue','$_POST[valuelift]','$_POST[valuesignificant]','$_POST[valuewinningcreative]','$_POST[variableid]')";


//some php that sends the same form back to the browser - code not necessary to show

if(move_uploaded_file($_POST[valueimage][tmp_name], $target))
{
echo "ok";
}
user2828701
  • 307
  • 1
  • 3
  • 18
  • possible duplicate of [How can I upload files asynchronously with jQuery?](http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery) – Ray Nicholus Oct 16 '13 at 03:27
  • 1
    Take a look at the related questions column to the right. – Musa Oct 16 '13 at 03:27
  • Like I mentioned in my post, I tried to implement some of the other examples, but with no luck. I'm hoping you could look at my specific issue and help provide customized help – user2828701 Oct 16 '13 at 03:41
  • Have you tried the code from google search ??? It's very easy . – Anand Solanki Oct 16 '13 at 06:58
  • Show what you tried from those questions – Musa Oct 16 '13 at 18:35
  • @Musa I just updated my post if you can take a look. It is the exact solution that the admin pointed me to...but still no luck – user2828701 Oct 17 '13 at 00:14

1 Answers1

5

Try this

$(function(){
    $('#valuesubmit').click(function(event) {
        var formData = new FormData($('form')[0]); // okay I just saw the form, assuming there is only one form on the page
        $.ajax({
            url: 'post_value_dummy.php',  //Server script to process data
            type: 'POST',
            /* This is just looks like bloat
            xhr: function() {  // Custom XMLHttpRequest
                var myXhr = $.ajaxSettings.xhr();
                //if(myXhr.upload){ // Check if upload property exists
                   // myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
                //}
                return myXhr;
            },*/
            // Form data
            // enctype: 'multipart/form-data',  <-- don't do this       
            data: formData,
            //Options to tell jQuery not to process data or worry about content-type.
            //cache: false, post requests aren't cached
            contentType: false,
            processData: false
        });
    });
});
Musa
  • 96,336
  • 17
  • 118
  • 137
  • I am getting the following error: TypeError: $(...)[0] is undefined formData.append('valueimage', $('#valueimage')[0].files[0]); – user2828701 Oct 17 '13 at 13:38
  • (MUSA) - Thanks for sticking with me...I really appreciate it. I added another screenshot to my original post...it is right under my original screenshot. Basically - that is what I see on my post tab within firebug. As you can see there is a bit of mumbo-jumbo where the image is supposed to be sent and then below are the three other fields I was attempting to post...the DB was not added to, nor was my image uploaded...maybe something is up with my PHP? I only say/ask that because nothing is returned to the screen as was intended... – user2828701 Oct 18 '13 at 02:29
  • @user2828701 It works for me, what version of jQuery are you using? – Musa Oct 18 '13 at 12:08
  • SUCCESS!!! Gosh - I had an outdated version of jquery (as you suggested)..hence the reason why I was originally not able to implement any of the recommendations I saw on stack earlier. Thanks so much for your help! You are a life saver – user2828701 Oct 18 '13 at 13:42
  • i actually have 3 forms on the page. I changed the form we've been working on to look for the form id (val) to FormData($('#val')[0]); ...when I attempted to do the same for other forms, I am getting an error. TypeError: Argument 1 of FormData.constructor is not an object. the other forms are: var formData = new FormData($('#var')[1]); FormData($('#var')[0]); & FormData($('#exp')[0]); – user2828701 Oct 18 '13 at 14:39
  • 1
    @user2828701 IDS must be unique, use a different id for each form. – Musa Oct 18 '13 at 15:18
  • Got it! My ids were unique, the problem was that when I looked at the DOM, one of my form tags was missing. Thanks again for your patience and assistance! – user2828701 Oct 18 '13 at 19:08