dtvnbe1428 2014-02-04 22:27
浏览 108
已采纳

在javascript控制台中从php输出数组

i have a simple ajax script that sends 3 variables to an external php script, it then adds them into an array and sends the array back, and i want it to output it in the javascript console, so that i can check that the variables are being passed back successfully, however when i run the script nothing appears in the console, only that

XHR finished loading: "http://localhost/blank/scripts/ajax/profile_password_submit.php". 

Here is the ajax

$("#pro_content_password").submit(function() {

    var url = "scripts/ajax/profile_password_submit.php"; // the script where you handle the form input.
    var js_array=new Array();
    $.ajax({
           type: "POST",
           url: url,
           data: $("#pro_content_password").serialize(), // serializes the form's elements.
           success: function(data){

                js_array=data;

                console.log(js_array);

             },
             dataType: 'json'
         });

    return false; // avoid to execute the actual submit of the form.
});

Here is the external php script

    session_start();
  include '../../connect.php';

  $user_id = "";
  $user_id = $_SESSION['userId'];
  echo $user_id;
 if(empty($_SESSION['userId'])){

     echo "user id session not set";
     exit;
  }


$old_password = $_POST['pro_content_password_old'];
$new_password = $_POST['pro_content_password_new'];
$new_password1 = $_POST['pro_content_password_verify'];


$password_array = array("old"=>$old_password,"new"=>$new_password, "new1"=>$new_password1);


echo json_encode($password_array);

Any ideas? Also i am using Google Chrome console

  • 写回答

3条回答 默认 最新

  • donglu9898 2014-02-06 14:31
    关注

    Here is the final working version now, its a lot different to my original but it works a lot better

        <script type="text/javascript">
    function submit_profile_password(){
        //var results = document.getElementById("results");
        var result_error = document.getElementById("pro_content_error_password");
        var old_error = document.getElementById("pro_password_old_comment");
        var new_error = document.getElementById("pro_password_new_comment");
        var new1_error = document.getElementById("pro_password_new1_comment");
    
        var oldPass = document.getElementsByName("pro_content_password_old")[0].value;
        var newPass = document.getElementsByName("pro_content_password_new")[0].value;
        var new1Pass = document.getElementsByName("pro_content_password_verify")[0].value;
        var vars = "oldPass="+oldPass+"&newPass="+newPass+"&new1Pass="+new1Pass;
    
        var hr = new XMLHttpRequest();
        hr.open("POST", "scripts/ajax/profile_password_submit.php", true);
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        hr.onreadystatechange = function() {
            if(hr.readyState == 4 && hr.status == 200) {
                var data = JSON.parse(hr.responseText);
                //results.innerHTML = "";
    
                for(var obj in data){
                    if(obj == "old"){
                        old_error.innerHTML = "";
                        old_error.innerHTML = data[obj];
    
                    }else if(obj == "new"){
                        new_error.innerHTML = "";
                        new_error.innerHTML = data[obj];
    
                    }else if(obj == "new1"){
                        new1_error.innerHTML = "";
                        new1_error.innerHTML = data[obj];
    
                    }else if(obj == "result"){
                        result_error.innerHTML = "";
                        result_error.innerHTML = data[obj];
    
                    }
    
                    //alert("Key = "+obj+"value = "+data[obj]+"");
    
                }
            }
        }
        hr.send(vars);
        //results.innerHTML = "requesting...";
        return false; 
    }
    
    
    
    
    
    </script>
    

    Thanks all for the help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?