开发者

php code in div id="upload" of xhr.open("POST", $id("upload").action, true); not executing

开发者 https://www.devze.com 2023-04-13 01:33 出处:网络
In my .js file there is this line xhr.open(\"POST\", $id(\"upload\").action, true); xhr.setRequestHeader(\"X_FILENAME\", file.name);

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';
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号