0

How can i push variable to ajax function in php file (witch refers to ajax ) if i have $v in my php . if all is ok it's echo "ok" in php file .So what should i do next

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);   
})(); 
13
  • Not clear enough. Please elaborate. Which ajax function? Commented Feb 2, 2016 at 10:56
  • add some data to question. i need to get id from DB and push it back to ajax function so i could store it's value in some hidden input Commented Feb 2, 2016 at 10:59
  • i know how to GET and id , but i don't know how to get it back to js script so i could .innerHTML on input Commented Feb 2, 2016 at 11:01
  • just make it a template and echo it into the right place? Commented Feb 2, 2016 at 11:01
  • instead of echoing ok just echo id in the form of json if there are multiple ids you want back like this {"id": 1} or if multiple then somthing like this {1,2,3,4} Commented Feb 2, 2016 at 11:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.