I am trying to use Ajax to insert data into database with POST method. But php can not recognize the post variables.
我试图使用Ajax使用POST方法将数据插入数据库。但是php无法识别post变量。
ajax:
AJAX:
function createUser() {
var _id=document.getElementById('new_id').value;
var _name=document.getElementById('new_name').value;
var params = "id="+_id+"&name="+_name;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
//xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function() {
console.log(xmlhttp);
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if(xmlhttp.responseText=="")
alert("New User is created!");
else
document.body.innerHTML=xmlhttp.responseText;
}
};
xmlhttp.open("POST","resource/create_profile.php",true);
xmlhttp.send(params);
}
php:
PHP:
<?php
$id = $_POST["id"];
$name = $_POST["name"];
$conn = oci_connect(....);
$query = "....";
$result = oci_parse($conn, $query);
oci_execute($result);
oci_close($conn);
?>
if i uncomment the request header content type then nothing happens, no error is shown. if i comment it then php shows error. i am giving a screenshot.
如果我取消注释请求标头内容类型,则没有任何反应,不会显示错误。如果我评论它然后PHP显示错误。我正在给截图。
http://imgur.com/CdKO84V
what am i supposed to do? if i use get method then it is working good but i can not use it since i need to upload file.
我应该做些什么?如果我使用get方法然后它工作正常,但我不能使用它,因为我需要上传文件。
1 个解决方案
#1
1
At last I have found the solution! I had to add the setRequestHeader() method below the open method. So in the end the code looks like:
最后我找到了解决方案!我必须在open方法下面添加setRequestHeader()方法。所以最后代码看起来像:
xmlhttp.open("POST","resource/create_profile.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
#1
1
At last I have found the solution! I had to add the setRequestHeader() method below the open method. So in the end the code looks like:
最后我找到了解决方案!我必须在open方法下面添加setRequestHeader()方法。所以最后代码看起来像:
xmlhttp.open("POST","resource/create_profile.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");