JorgeD.
JorgeD.

Reputation: 9

Form action and ajax query for custom name image upload and data sent to MySQL

firts of all, thank you for all the help you have been giving me allways with your answers. I'm really new with coding, but more or less I get through and I'm creating a website from 0 (www.mundopedales.com)

But today I'm really stuck with a form I want it to do two action, sending data to MySQL db and uploading a image file with a custom name. I'm able to run both but not at the same time, if one works the other one doesn't.

Form action runs this file where I can send the input text for the custom name and the image:

<form action="upload.php" id="send-bike" method="post" enctype="multipart/form-data">
<input type='text' name='newname' id='newname' placeholder='Image filename'/>
<input type="file" name="fileToUpload" id="fileToUpload">

Button :

<input id="postData" type="button" value = "post">

Ajax code is this and is where I get curious action hapening:

$('#postData').click(function (e) {
e.preventDefault();
var newname = $('#newname').val();
var dataString = 'newname=' + newname;
$.ajax({
    type: 'POST',
    url: 'sendbike.php',
    data: dataString,
    success: function () {  
        $('#send-bike').submit();
        //window.location.assign('http://www.mundopedales.com/crear-bicicletas.php');//if I untag this line I change upload.php to work and then sendbike.php runs....
        }
    });    

});

Don't understand why that makes that change and how I can do to make both run at the same time.

Thanks in advance and sorry for my poor English (made in Spain...)

Upvotes: 0

Views: 153

Answers (1)

Logan Wayne
Logan Wayne

Reputation: 5991

Use .submit() of jQuery instead.

$('#send-bike').submit(function(){

    var formData = new FormData($(this)[0]); /* DATA FROM THIS FORM */

    $.ajax({
        type: 'POST',
        url: 'sendbike.php',
        data: formData,
        contentType: false,
        processData: false,
        success: function () {  
            alert('success uploading!');
        }
    });

    return false;

});

Just process the submitted data to sendbike.php.

Upvotes: 1

Related Questions