file upload ajax php post [duplicate]










0
















This question already has an answer here:



  • jQuery AJAX file upload PHP

    5 answers



  • jQuery Ajax File Upload

    21 answers



How do I use AJAX, Javascript and PHP to POST data AND a file?



I can post the data inputs "name" and "lname", but not the file "myfile".



I have a input page called "input.php" and a post page called "postit.php".



input.php



My html:



<form method="post" name="form" action="postit.php" enctype="multipart/form-data">
<input type="text" name="name" id="name" placeholder="Name" >
<input type="text" name="lname" id="lname" placeholder="lname">
<input type="file" name="myfile" id="myfile" placeholder="myfile">

<button type="button" onclick="myFunction()">Save</button><br>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>


My Javascript:



function myFunction() 
var name = document.getElementById("name").value;
var lname = document.getElementById("lname").value;
var myfile = document.getElementById("myfile").value;
$.ajax(
type : "POST",
url : "postit.php",
data : name : name, lname : lname, myfile: myfile ,
success: function(res)
setTimeout(function () location.reload(), 400);

);



postit.php



<?php 
$name = $_POST['name'];
$lname = $_POST['lname'];
$myfile = $_FILES['myfile']['name'];
?>









share|improve this question















marked as duplicate by charlietfl javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 '18 at 14:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • See this topic stackoverflow.com/questions/23980733/…

    – Muhammet Can TONBUL
    Nov 12 '18 at 14:54











  • Create new formdata obj var formData = new FormData($("#FormID")[0]); append the file to formdata formData.append('myfile', $('#myfile')[0].files[0]); set data : formdata on your ajax setuo

    – Masivuye Cokile
    Nov 12 '18 at 14:56











  • #Muhammet Can TONBUL - That answer is only about file transfer, and not about data AND file transfer.

    – user2868139
    Nov 13 '18 at 8:00











  • #Masivuye Cokile - How do I implement your answer in my code?

    – user2868139
    Nov 13 '18 at 8:01















0
















This question already has an answer here:



  • jQuery AJAX file upload PHP

    5 answers



  • jQuery Ajax File Upload

    21 answers



How do I use AJAX, Javascript and PHP to POST data AND a file?



I can post the data inputs "name" and "lname", but not the file "myfile".



I have a input page called "input.php" and a post page called "postit.php".



input.php



My html:



<form method="post" name="form" action="postit.php" enctype="multipart/form-data">
<input type="text" name="name" id="name" placeholder="Name" >
<input type="text" name="lname" id="lname" placeholder="lname">
<input type="file" name="myfile" id="myfile" placeholder="myfile">

<button type="button" onclick="myFunction()">Save</button><br>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>


My Javascript:



function myFunction() 
var name = document.getElementById("name").value;
var lname = document.getElementById("lname").value;
var myfile = document.getElementById("myfile").value;
$.ajax(
type : "POST",
url : "postit.php",
data : name : name, lname : lname, myfile: myfile ,
success: function(res)
setTimeout(function () location.reload(), 400);

);



postit.php



<?php 
$name = $_POST['name'];
$lname = $_POST['lname'];
$myfile = $_FILES['myfile']['name'];
?>









share|improve this question















marked as duplicate by charlietfl javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 '18 at 14:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • See this topic stackoverflow.com/questions/23980733/…

    – Muhammet Can TONBUL
    Nov 12 '18 at 14:54











  • Create new formdata obj var formData = new FormData($("#FormID")[0]); append the file to formdata formData.append('myfile', $('#myfile')[0].files[0]); set data : formdata on your ajax setuo

    – Masivuye Cokile
    Nov 12 '18 at 14:56











  • #Muhammet Can TONBUL - That answer is only about file transfer, and not about data AND file transfer.

    – user2868139
    Nov 13 '18 at 8:00











  • #Masivuye Cokile - How do I implement your answer in my code?

    – user2868139
    Nov 13 '18 at 8:01













0












0








0









This question already has an answer here:



  • jQuery AJAX file upload PHP

    5 answers



  • jQuery Ajax File Upload

    21 answers



How do I use AJAX, Javascript and PHP to POST data AND a file?



I can post the data inputs "name" and "lname", but not the file "myfile".



I have a input page called "input.php" and a post page called "postit.php".



input.php



My html:



<form method="post" name="form" action="postit.php" enctype="multipart/form-data">
<input type="text" name="name" id="name" placeholder="Name" >
<input type="text" name="lname" id="lname" placeholder="lname">
<input type="file" name="myfile" id="myfile" placeholder="myfile">

<button type="button" onclick="myFunction()">Save</button><br>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>


My Javascript:



function myFunction() 
var name = document.getElementById("name").value;
var lname = document.getElementById("lname").value;
var myfile = document.getElementById("myfile").value;
$.ajax(
type : "POST",
url : "postit.php",
data : name : name, lname : lname, myfile: myfile ,
success: function(res)
setTimeout(function () location.reload(), 400);

);



postit.php



<?php 
$name = $_POST['name'];
$lname = $_POST['lname'];
$myfile = $_FILES['myfile']['name'];
?>









share|improve this question

















This question already has an answer here:



  • jQuery AJAX file upload PHP

    5 answers



  • jQuery Ajax File Upload

    21 answers



How do I use AJAX, Javascript and PHP to POST data AND a file?



I can post the data inputs "name" and "lname", but not the file "myfile".



I have a input page called "input.php" and a post page called "postit.php".



input.php



My html:



<form method="post" name="form" action="postit.php" enctype="multipart/form-data">
<input type="text" name="name" id="name" placeholder="Name" >
<input type="text" name="lname" id="lname" placeholder="lname">
<input type="file" name="myfile" id="myfile" placeholder="myfile">

<button type="button" onclick="myFunction()">Save</button><br>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>


My Javascript:



function myFunction() 
var name = document.getElementById("name").value;
var lname = document.getElementById("lname").value;
var myfile = document.getElementById("myfile").value;
$.ajax(
type : "POST",
url : "postit.php",
data : name : name, lname : lname, myfile: myfile ,
success: function(res)
setTimeout(function () location.reload(), 400);

);



postit.php



<?php 
$name = $_POST['name'];
$lname = $_POST['lname'];
$myfile = $_FILES['myfile']['name'];
?>




This question already has an answer here:



  • jQuery AJAX file upload PHP

    5 answers



  • jQuery Ajax File Upload

    21 answers







javascript php ajax post file-upload






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 9:59







user2868139

















asked Nov 12 '18 at 14:51









user2868139user2868139

253




253




marked as duplicate by charlietfl javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 '18 at 14:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by charlietfl javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 '18 at 14:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • See this topic stackoverflow.com/questions/23980733/…

    – Muhammet Can TONBUL
    Nov 12 '18 at 14:54











  • Create new formdata obj var formData = new FormData($("#FormID")[0]); append the file to formdata formData.append('myfile', $('#myfile')[0].files[0]); set data : formdata on your ajax setuo

    – Masivuye Cokile
    Nov 12 '18 at 14:56











  • #Muhammet Can TONBUL - That answer is only about file transfer, and not about data AND file transfer.

    – user2868139
    Nov 13 '18 at 8:00











  • #Masivuye Cokile - How do I implement your answer in my code?

    – user2868139
    Nov 13 '18 at 8:01

















  • See this topic stackoverflow.com/questions/23980733/…

    – Muhammet Can TONBUL
    Nov 12 '18 at 14:54











  • Create new formdata obj var formData = new FormData($("#FormID")[0]); append the file to formdata formData.append('myfile', $('#myfile')[0].files[0]); set data : formdata on your ajax setuo

    – Masivuye Cokile
    Nov 12 '18 at 14:56











  • #Muhammet Can TONBUL - That answer is only about file transfer, and not about data AND file transfer.

    – user2868139
    Nov 13 '18 at 8:00











  • #Masivuye Cokile - How do I implement your answer in my code?

    – user2868139
    Nov 13 '18 at 8:01
















See this topic stackoverflow.com/questions/23980733/…

– Muhammet Can TONBUL
Nov 12 '18 at 14:54





See this topic stackoverflow.com/questions/23980733/…

– Muhammet Can TONBUL
Nov 12 '18 at 14:54













Create new formdata obj var formData = new FormData($("#FormID")[0]); append the file to formdata formData.append('myfile', $('#myfile')[0].files[0]); set data : formdata on your ajax setuo

– Masivuye Cokile
Nov 12 '18 at 14:56





Create new formdata obj var formData = new FormData($("#FormID")[0]); append the file to formdata formData.append('myfile', $('#myfile')[0].files[0]); set data : formdata on your ajax setuo

– Masivuye Cokile
Nov 12 '18 at 14:56













#Muhammet Can TONBUL - That answer is only about file transfer, and not about data AND file transfer.

– user2868139
Nov 13 '18 at 8:00





#Muhammet Can TONBUL - That answer is only about file transfer, and not about data AND file transfer.

– user2868139
Nov 13 '18 at 8:00













#Masivuye Cokile - How do I implement your answer in my code?

– user2868139
Nov 13 '18 at 8:01





#Masivuye Cokile - How do I implement your answer in my code?

– user2868139
Nov 13 '18 at 8:01












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus