Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/261039-syntax-help-add-vars-to-requestsend/
Share on other sites

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);

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.