10

This might have been asked before, but i have search on here and on google and every answer I have read doesnt work.

The question I have to solve is make a form with first name, last name, email and a image. Then pass the data into a database and upload the file also to a database. Currently my code doesnt do anything after I press submit. Before I added the file box it would insert the data into my database.

HTML

<form id="myForm" method ="post" enctype="multipart/form-data">
    First Name: <input type="text" name="fname" id="fname"> <br>
    Last Name: <input type="text" name="lname" id="lname"> <br>
    Email:  <input type="text" name="email" id="email"> <br>
    Image: <input type="file" name="image" id="image"> <br>
    <button type="button" name="btnSubmit" id="btnSubmit"> Submit </button>
</form>

AJAX/JS

$("#btnSubmit").click(function(){
     var formData = new FormData($(this)[0]);
     $.ajax({
        type: 'POST',
        url: 'form2.php',
        data: formData,
         success: function (data) {
           alert(data)
         },
      });
  });

PHP

$upload = basename($_FILES['image']['name']);
$type = substr($upload, strrpos($upload, '.') + 1);
$size = $_FILES['image']['size']/1024; 

if ($_FILES["image"]["error"] > 0)
{
    echo "Error: " . $_FILES["image"]["error"] . "<br>";
}
else
{
    echo "Upload: " . $upload . "<br>";
    echo "Type: " . $type . "<br>";
    echo "Size: " . $size . " kB<br>";
}

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
echo "You Entered <br />";
echo "<b>First Name:</b> ". $fname . "<br />";
echo "<b>Last Name:</b> ". $lname . "<br />";
echo "<b>Email:</b> ". $email . "<br />";
5
  • stackoverflow.com/questions/9622901/… Commented Nov 1, 2013 at 22:18
  • Welcome to Stack Overflow. When asking questions, it helps to explain what you mean specifically. For example, don't just say something "doesn't work". What does this tell? How does it help to fix the problem? Also it's a good idea to explain why you're doing something, as someone might be able to tell you if you're going about something the wrong way, but might be acceptable if it was for something else. For more information, see How to Ask. Thank you! Commented Nov 1, 2013 at 22:46
  • Qantas 94 Heavy, is that better explanation? Commented Nov 1, 2013 at 23:04
  • 1
    Alex , I'm just improved your code and fixed the js issues , you can pull the code from this link github.com/AstmDesign/phpAjaxUploader Commented May 3, 2016 at 15:07
  • 2
    why you people always Mark as duplicate questions. this is not duplicate question. vote up Commented Sep 1, 2016 at 12:59

3 Answers 3

1

Forms by default submit to wherever they're told. In order to stop this, you need to prevent it. Your js should look something like this:

$("form#data").submit(function(event){
    event.preventDefault();
    ...
});
Sign up to request clarification or add additional context in comments.

Comments

1

change the submit type button to button type and then use AJAX like this:

<button type="button" name="btnSubmit" id="btnSubmit"> Submit </button>

you need to change the jQuery Code to :

$("#btnSubmit").click(function(){
     var formData = new FormData($("#myForm"));
     $.ajax({
        type: 'POST',
        url: 'form2.php',
        data: formData,
         success: function (data) {
           alert(data)
         },
      });
  });  

and also change the code here if ($_FILES["file"]["error"] > 0) to if ($_FILES["image"]["error"] > 0)

5 Comments

I added that and changed my jquery, but still no dice in storing to my database, also shouldnt the alert pop up also?
are you connecting to your database properly ? and what's the code you are using to store the data into db ?
I am connecting properly, I have not changed the connection code since I added the file box. Before I had the form data adding to my data base or just the file but can't get both. I have a feeling my ajax isnt going to my php file.
check out again. i have changed the ajax code
I tried it, still nothing. THe network tab only shows my form with a GET method, there is no action when i press submit>?
0

This line

if ($_FILES["file"]["error"] > 0)

Should be

 if ($_FILES["image"]["error"] > 0)

2 Comments

what does your browsers console show as a response?
nothing it doesnt show any response on the console

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.