www.webdeveloper.com
Results 1 to 4 of 4

Thread: Ajax post

  1. #1
    Join Date
    Jan 2014
    Posts
    1

    Exclamation Ajax post

    Can you help me understand how to post data from javascript to PHP
    I have javascript code with two functions

    Code:
    function one() {
    
    	var req = getXmlHttp()  
           	var statusElem = document.getElementById('tam_status') 
    	
    	req.onreadystatechange = function() {  
           
    		if (req.readyState == 4) { 
               			statusElem.innerHTML = req.responseText 
    
    			if(req.status == 200 && req.status < 300) { 
           	document.getElementById('tam_status').innerHTML = req.responseText
    			}
    			}
    	}
    
    	var status = "1";
    	document.getElementById('status_t').innerHTML=status;
          	req.open('GET', 'views/page1.php?s='+status, false);  
    	req.send(null);  
    	}
    First function works correct. I need similar function, which will send data to another php page. Second function will be reloaded every 5 seconds, for sending fresh data.
    Code:
    function two() {.....
    var req = getXmlHttp()  
           	var statusElem = document.getElementById('tam_status') 
    	
    	req.onreadystatechange = function() {  
           
    		if (req.readyState == 4) { 
               			statusElem.innerHTML = req.responseText 
    
    			if(req.status == 200 && req.status < 300) { 
           	document.getElementById('tam_status').innerHTML = req.responseText
    			}
    			}
    	}
    
    	var status = "1";
    	document.getElementById('status_t').innerHTML=status;
          	req.open('GET', 'views/page2.php?s='+status, false);  
    	req.send(null); 
    }
    var tm=setInterval('two();',5000);
    Problem:
    After function two() is started, data from function one() became missed and don't work more.
    I just started to learn it and need help.
    What I am doing wrong? Can somebody explain me? Thanks for the help.
    При срабатывании Функции two(), данные которые передала функция one () теряются и больше не передаются.
    Я новичок в этом, пытаюсь разобраться, но никак не пойму, что я неправильно делаю...

  2. #2
    Join Date
    Jun 2009
    Posts
    18
    One solution is to push the variable to php:
    Code:
    function myJavascriptFunction() { 
      var javascriptVariable = "John";
      window.location.href = "myphpfile.php?name=" + javascriptVariable; 
    }
    Another workaround is to prepare your js within php:

    HTML Code:
    <script>
    var whatever = "<?= $phpVar ?>";
    </script>

  3. #3
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    rus: нихрена толком не понятно объясни нормально что ты хочешь сделать этими ф-циями
    en: it's not clear what you're trying to do with those functions so pls describe your goal
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,360
    When posting... you need to specify POST, you have [CODE}req.open('GET', 'views/page1.php?s='+status, false);
    req.send(null); [CODE]
    you need to add in an attribute of a POST function like the enctype, so it should look something like
    Code:
    req.open("POST", 'views/page1.php?s='+status, false);  
    req.setRequestHeader("Content-Type", "multipart/form-data");	
    req.send(theform);
    where theform is a reference to your web form
    you would also need to use javascript to halt the default action of the form and then allow AJAX to make the form post in the background

    If you really want to take the pain out of XMLHttp, then look in the MDN site for the script that covers Posting and downloading.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles