Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript输入字段不工作_Javascript_Php_Html - Fatal编程技术网

Javascript输入字段不工作

Javascript输入字段不工作,javascript,php,html,Javascript,Php,Html,我试图用javascript填充一个输入字段,所有的脚本都不工作,我只看到php的回声,但没有看到用于填充输入字段的javascript部分 这是我的密码 if($_SERVER['REQUEST_METHOD'] == 'POST') { $card = $_POST['card']; echo json_encode(array('card_response' => get_cc_type($card))); //exit; } 这是我的剧本,但我不知道为什么它不起作用 <s

我试图用javascript填充一个输入字段,所有的脚本都不工作,我只看到php的回声,但没有看到用于填充输入字段的javascript部分

这是我的密码

 if($_SERVER['REQUEST_METHOD'] == 'POST') {
$card = $_POST['card'];
echo json_encode(array('card_response' => get_cc_type($card)));
//exit;
}
这是我的剧本,但我不知道为什么它不起作用

<script type="text/javascript">
document.getElementById('button').addEventListener('click', function(){
var card_value = document.getElementById('card').value; // get the input value
var xmlhttp = new XMLHttpRequest(); // create the xmlhttprequest object
var params = 'card=' + card_value; // create the query string
var php_url = document.URL; // this is the URL of the PHP, in this example, i'll just setup it the PHP on the same page.
xmlhttp.open('POST', php_url, true); // set as POST

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        // handle the response from server here
        var response = xmlhttp.responseText;
        response = JSON.parse(response);
        document.getElementById('cardtype').value = response.card_response; // set the value
    }
}
xmlhttp.send(params); // execute
 });
 </script>

document.getElementById('button')。addEventListener('click',function(){
var card_value=document.getElementById('card').value;//获取输入值
var xmlhttp=new XMLHttpRequest();//创建XMLHttpRequest对象
var params='card='+card_value;//创建查询字符串
var php_url=document.url;//这是php的url,在本例中,我将在同一页面上设置它。
open('POST',php_url,true);//设置为POST
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
//在这里处理来自服务器的响应
var response=xmlhttp.responseText;
response=JSON.parse(response);
document.getElementById('cardtype')。value=response.card\u response;//设置值
}
}
xmlhttp.send(params);//执行
});

我只在我的页面中看到了这些东西----{“card_response”:“VIP”}

在jquery上,首先用警报检查

alert(xmlhttp.toSource());
alert(response.toSource());

alert(response.card_response);
这些警报将告诉您确切获得的值。例如,xmlhttp.status是否为200


另外,检查Firebug,jquery是否出现故障?

语法错误:JSON解析错误:无法识别的令牌“您可以提供JSON吗?”如果($\u服务器['REQUEST\u方法']='POST'){$card=$\u POST['card'];echo JSON\u编码(数组('card\u response'=>获取抄送类型('card));退出;}firebug只是告诉我,在JSON数据响应=JSON.parse(响应)的第2行第1列的SyntaxError:JSON.parse:unexpected字符@ManuelPerez只需在php代码中的“头('Content-type:application/json');”之前插入头类型,“echo json_encode(array('card_response'=>get_cc_type($card))”)。在变量“response”上使用alert-to-source,因为您需要JSON,所以必须在PHP代码和Javascript Ajax中提及内容类型。如何提及在哪里?我刚刚接触php。我已经有了这个xmlhttp.setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);这将有助于您和header('Content-Type:application/json');在将回显响应发送回ajax之前