View Full Version : Sending $string data without a form

03-30-2008, 11:44 AM

I use PHP and have no clue with javascript.
is there a way of sending $string data to a new page using javascript instead of a form. On the new page I wish to pick up that data with:

$prevpage = $_REQUEST['sentdata'];
echo $prevpage;


03-30-2008, 11:49 AM
Read http://in2.php.net/session including examples.

03-30-2008, 12:06 PM
I will take that as a no then:(

03-30-2008, 12:20 PM
yes there is, it's a technology called AJAX. it's just a long answer for a short problem.

<script type="text/javascript">
function sendString(sendTo, value){
var xhr = XMLHttpRequest?(new XMLHttpRequest()):(new ActiveXObject("Microsoft.XMLHttp"));
xhr.open("get", sendTo + "?sentdata=" + value, true);
xhr.onreadystatechange = function(){};

add onclick="sendString('url to page to send to', 'string to send');" to whatever button it is that you want to have send the string. this uses the "Get" method of sending information, so you would use $_GET['sentdata'] to get the value of it, but if you change "get" in the function (above) to "post" you can use $_POST['sentdata'] instead.

//Script from above goes here
<body ...>
<input type="text" id="myString" value="" />
<input type="button" value="Send" onclick="sendString('mypage.php', document.getElementById('myString').value);" />

would be a simple example of how to use this.

03-30-2008, 12:24 PM
can this work on a hyper link rather than a button?

03-30-2008, 01:33 PM
<a href="javascript:void(0)" onclick="...">My link</a>
just make the contents of onclick with the same from the one on the button

03-30-2008, 01:43 PM
<a href="javascript:void(0)" onclick="...">My link</a>

<a href="#" onclick="...;return false;">My link</a> is the proper way.