Link to home
Start Free TrialLog in
Avatar of joaocc
joaocc

asked on

ajaxForm plugin - dynamic id

I am using the ajaxform plugin to upload some images, and at the same time I need to send an id to update the database.


To explain better, my files:

upload.html
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">

						<select id="selectorPubImg">
							<option value="1">image 1</option>
							<option value="2">image 2</option>
							<option value="3">image 3</option>
							<option value="4">image 4</option>
						</select>

	<input type="file" size="60" name="myfile">
	<input type="submit" value="Enviar">
	
</form>

Open in new window

my js file:
$(document).ready(function()
{
 
    var options = {
    data: {id:$('#selectorPubImg').val()}, 
    beforeSend: function() 
    {
        $("#progress").show();
        //clear everything
        $("#bar").width('0%');
        $("#message").html("");
        $("#percent").html("0%");

    },
    uploadProgress: function(event, position, total, percentComplete) 
    {
        $("#bar").width(percentComplete+'%');
        $("#percent").html(percentComplete+'%');
 
    },
    success: function() 
    {
        $("#bar").width('100%');
        $("#percent").html('100%');
 
    },
    complete: function(response) 
    {
        $("#message").html("<font color='green'>"+response.responseText+"</font>");
    },
    error: function()
    {
        $("#message").html("<font color='red'> ERROR: unable to upload files</font>");
 
    }
 
}; 
 
     $("#myForm").ajaxForm(options);

});

Open in new window


The problem with this is that "data: {id:$('#selectorPubImg').val()}, " will always be  "1" since i am storing it in a document.ready(). Is there a way to update that value with the current value of the selection list?

Thank you guys
ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
and then to update the id:

$(document).ready(function() {
    $('#selectorPubImg').change(function() {
        MyProject.options.data.id = $('#selectorPubImg').val();
    });
...
});

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joaocc
joaocc

ASKER

Thanks, it works perfect. Thanks also Julian, i used tagit version since it was the first and it worked, i am sure yours also work just fine. you guys are great. ;)
Not begrudging tagit the points but the correct way is to use the constructs that the ajaxform provides.

In this case you set the data you want to send in the beforeSubmit.
Avatar of joaocc

ASKER

thank julian, i will look into it.
I agree, my version works but Julian's is better :) It's cleaner and using it how ajaxForm was designed.  If I came across this solution I would want to use his.
Avatar of joaocc

ASKER

When I opened the email this morning I just "copy paste" the first expert comment and it worked right way. After I saw the julian solution and to be honest I didnt try it out since the tagit solution worked so well.

Then I was told that Julian solution is the "right way" to do it, and I am sure that's true, but since tagit solution was the first one I saw...

I am totally ok of spliting the points if after my explanation moderators feel like that.

btw, Julian and tagit thank you again for the help, not only in this question but in many others.

Sorry for any inconvenience.
No problem at all. Glad to help :-)
My point here was to highlight that there was another solution posted. To be honest I think if you came across this post and read to the bottom you would find this out anyway but Im just putting it out there by requesting attention to see how everyone else feels
You are welcome - and I don't mind if you accept tagit's solution as assisted with full points and mine as accepted with 0 - don't want to make this about the points.

Just for future readers of the post I think the beforeSubmit answer should be the right one.

The stackoverflow guys are giving us stick because we don't have a rigourous system of accepting correct answers - so just trying to do my bit to keep EE on top.

@tagit - thanks for the support mate - appreciate it.
No problem mate. I'm all for EE and keeping it the best :-)