将php中的变量发送到ajax函数

时间:2016-02-02 10:55:38

标签: php ajax

如何将变量推送到php文件中的ajax函数(女巫指的是ajax) 如果我的PHP中有$v。如果一切正常,那就是php文件中的echo "ok"。那我该怎么做呢

if ($_POST){
    for ($j=0; $j< count($row);$j++){       
                mysql_query("INSERT INTO `".PREFIX_USR."delivery_address` (`id_personl_data`,`country`,`province`,`city`,`street`,`house`,`apartment`)
                        VALUES ( '12','".$row[$j][0]."','".$row[$j][1]."','".$row[$j][2]."','".$row[$j][3]."','".$row[$j][4]."','".$row[$j][5]."') ");
         var count = query['id'];  // just an example of what i need to push   
        } 

            echo "ok";
    }

js

(function(){            
        function changeData(){
        var name = d.getElementById('new_name').value,
            surname = d.getElementById('new_surname').value,
            email = d.getElementById('new_email').value,
            telephone = d.getElementById('new_phone').value,
            robot = d.getElementById('spam_change').value,
            xml = eventsObj.getXmlHttp();           
            var arr = [].map.call(document.querySelectorAll('.parent_clone'), function(block) {
              return [].map.call(block.querySelectorAll('.left_info_address'), function(inp) {
                return inp.value;
              });
            });            
            for (var i = 0; i<arr.length-1;i++){
                arr[i].push('/');
            }            
            console.log(arr);           
            if(name === "" || surname === "" || email === "" || (telephone === "")){            
                alert("fill the fields");                       
            }
            else {           
                xml.open("POST",path_ajax_files+"edit_personal_data.php",true);
                xml.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                xml.send("name="+encodeURIComponent(name)+
                         "&surname="+encodeURIComponent(surname)+
                         "&email="+encodeURIComponent(email)+
                         "&telephone="+encodeURIComponent(telephone)+
                         "&robot="+encodeURIComponent(robot)+
                         "&arr="+encodeURI(arr));
                xml.onreadystatechange = function(){
                    if(xml.readyState === 4){
                        if(xml.status === 200){
                            if(xml.responseText !== ""){
                                alert(xml.responseText);
                                if(xml.responseText === "ok"){
                                    alert("data will be changed");                                                                                                                                  
                                }                                                                                    
                            } else {
                                alert('try again later');
                            }
                        }
                    }
                };            
            }                        
        }        
        eventsObj.addEvent(saveData, "click", changeData, false);   
})(); 

0 个答案:

没有答案