doupu3635 2015-02-28 05:41
浏览 25
已采纳

Javascript输入字段不工作

Im trying to fill an input field, using a javascript, all the script is not working, i only see the echo of the php but not the javascript part that is intended to fill the input field.

this is my code

 if($_SERVER['REQUEST_METHOD'] == 'POST') {
$card = $_POST['card'];
echo json_encode(array('card_response' => get_cc_type($card)));
//exit;
}

and this my script, but i don't know why its not working

<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>

i only see this stuff in my page --- {"card_response":"VIP"}

  • 写回答

1条回答 默认 最新

  • douxian6008 2015-02-28 06:55
    关注

    On jquery first check with an alert

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

    These alerts will tell you what values you are getting exactly.for eg. xmlhttp.status is 200 or not.

    Also, check in Firebug, is jquery malfunctioning?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver