php code in div id="upload" of xhr.open("POST", $id("upload").action, true); not executing
In my .js file there is this line
xhr.open("POST", $id("upload").action, true);
xhr.setRequestHeader("X_FILENAME", file.name);
xhr.send(file);
in index.php there is a form like this
<form id="upload" action="upload1.ph开发者_StackOverflowp" method="POST" enctype="multipart/form-data">
So if I understand this ajax .open correctly, it should post the file sent to the action in the form id "upload" which is "upload1.php". Now if I go into upload1.php and have
file_put_contents('uploads/' . $fn,file_get_contents('php://input'));
it puts the file in a folder called uploads. But if i put something as simple as
echo "in upload1.php";
it completely skips over it and doesn't output anything. Ideally I want to redirect in upload1.php to redirect to another php file after it places the file in the uploads folder. But even header(""); etc. doesn't work.
Any help or suggestions? I'm new to javascript and ajax, finding it confusing.
it doesnt output anything(at least to the screen) because you have made an xhr request. It basically means that the echo statement is sent to the caller, in your case the js file. So you have to see the response in
var return = xhr.responseText; //return in plain text
Plus, you can use another php files functions and classes, with include_once or require_once method. If you really want to redirect to another page, you can also do it with js code like :
window.location = 'some place';
精彩评论