dflow Posted April 16, 2012 Share Posted April 16, 2012 im trying to post var the_data2 as well <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="ro"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-2" /> <title>Example Ajax POST</title> <script type="text/javascript"><!-- // create the XMLHttpRequest object, according browser function get_XmlHttp() { // create the variable that will contain the instance of the XMLHttpRequest object (initially with null value) var xmlHttp = null; if(window.XMLHttpRequest) { // for Forefox, IE7+, Opera, Safari, ... xmlHttp = new XMLHttpRequest(); } else if(window.ActiveXObject) { // for Internet Explorer 5 or 6 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } return xmlHttp; } // sends data to a php file, via POST, and displays the received answer function ajaxrequest(php_file, tagID) { var request = get_XmlHttp(); // call the function for the XMLHttpRequest instance // create pairs index=value with data that must be sent to server var the_data = 'page_id='+document.getElementById('txt2').innerHTML; var the_data2 = 'page_id2='+document.getElementById('txt22').innerHTML; request.open("POST", php_file, true); // set the request // adds a header to tell the PHP script to recognize the data as is sent via POST request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); request.send(the_data,the_data2); // calls the send() method with datas as parameter // Check request status // If the response is received completely, will be transferred to the HTML tag with tagID request.onreadystatechange = function() { if (request.readyState == 4) { document.getElementById(tagID).innerHTML = request.responseText; } } } --></script> </head> <body> <h5 style="cursor:pointer;" onclick="ajaxrequest('process-addpage.php', 'context')"><u>Click</u></h5> <div id="txt2" style="display:none;"><?php echo $page_id=33333;?></div> <div id="txt22" style="display:none;"><?php echo $page_id2=55555;?></div> <div id="context">Here will be displayed the response from the php script.</div> </body> </html> Quote Link to comment Share on other sites More sharing options...
kicken Posted April 16, 2012 Share Posted April 16, 2012 You need to build what looks like a query string and pass that to .send() eg: // create pairs index=value with data that must be sent to server var the_data = 'page_id='+encodeURIComponent(document.getElementById('txt2').innerHTML); var the_data2 = 'page_id2='+encodeURIComponent(document.getElementById('txt22').innerHTML); var post_data = the_data + '&' + the_data2; //..rest of code request.send(post_data); Quote Link to comment Share on other sites More sharing options...
dflow Posted April 16, 2012 Author Share Posted April 16, 2012 You need to build what looks like a query string and pass that to .send() eg: // create pairs index=value with data that must be sent to server var the_data = 'page_id='+encodeURIComponent(document.getElementById('txt2').innerHTML); var the_data2 = 'page_id2='+encodeURIComponent(document.getElementById('txt22').innerHTML); var post_data = the_data + '&' + the_data2; //..rest of code request.send(post_data); kool thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.