JS: referring page?

dfmchfhf

New Member
Messages
3
Reaction score
0
Points
0
Is there a (simple) way to get JavaScript to return the page that referred the code?

I have some of code to add input to a MySQL database (through AJAX), and I want to make it use the same .js file to handle everything.
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
you can use
Code:
location.href="http://site.you.want.to.go.com/";
 

djcustom

New Member
Messages
17
Reaction score
0
Points
0
Try using

Code:
[COLOR=blue][COLOR=green]page = request.getHeader("referer");[/COLOR][/COLOR]
location.href = page;
 

t2t2t

New Member
Messages
690
Reaction score
0
Points
0
He didn't want to go back to the page the user got to that page from, he wants to enter the referring URL to MYSQL database for storing.

Though it would be easier to be just done through php code, if you want to use javascript method, One way is to get the page before current one from (window.)history array.

Btw djcustom, your script probably would fail on first line (tried request.getHeader("referer") in console):
ReferenceError: request is not defined source=request.getHeader("referer");
 

dfmchfhf

New Member
Messages
3
Reaction score
0
Points
0
Right now, what I have is:
PHP:
<div id="messages"></div>
<hr>
<div id="form_part1"></div>
<?php
echo "<input type='hidden' name='ref' id='ref' value='" . basename($_SERVER['PHP_SELF']) . "'>";
?>
<div id="form_part2"></div>

with the </form> tag in part2;
Is it possible to collapse the entire form portion into JS?
 

t2t2t

New Member
Messages
690
Reaction score
0
Points
0
Can you tell more exactly what you want done, do you want JS to write the form?

(Btw if you want to save from where did form get submitted from, just use $_SERVER['HTTP_REFERER'] in PHP when saving data)
 

dfmchfhf

New Member
Messages
3
Reaction score
0
Points
0
(part of) the JS file:
Code:
function subMsg(name, subj, cmt, ref)
{
  //... (some code skipped)
  mTimer = setTimeout('udtCmt(\''+ref+'\');', 1);
}

function udtMsg(ref)
{
  GetXmlHttpObject();
  if (xmlHttp!=null)
  {
    url="getMsgs.php?q="+ref+"&sid="+Math.random();
    xmlHttp.onreadystatechange=state_Change;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
  }
  clearInterval(mTimer);
}

function state_Change()
{
  if (xmlHttp.readyState==4)
  {
    toDisp=xmlHttp.responseText;
    toDisp=toDisp+"\n<form name=\"messages\">\n<table><tr><td>Name:</td><td><input type=\"text\" name=\"name\" id=\"name\" autocomplete=\"off\" tabIndex=\"5\" accesskey=\"C\" maxlength=30 size=30 /></td></tr>\n<tr><td>Subject:</td><td><input type=\"text\" name=\"subj\" id=\"subj\" autocomplete=\"off\" tabIndex=\"6\" maxlength=100 size=70 /></td></tr>\n<tr><td>Messages:</td><td><textarea name=\"message\" id=\"message\" tabIndex=\"7\" rows=5 cols=50></textarea></td></tr></table>\n";
    document.getElementById('form_part1').innerHTML=toDisp;
    toDisp="<input type=\"button\" name=\"send_msg\" id=\"send_msg\" value=\"Submit\" tabIndex=\"8\" onClick=\"javascript:subCmt(document.getElementById(\'name\').value, document.getElementById(\'subj\').value, document.getElementById(\'message\').value, document.getElementById(\'ref\').value)\;\" />\n\n</form>";
    document.getElementById('form_part2').innerHTML=toDisp;
  }
}

(part of) getMsgs.php:
PHP:
echo "<table border='0' align='center' width='100%' style='table-layout:fixed;'>\n";
echo "<tr><td width='20px'></td><td></td></tr>\n";

while($row = mysql_fetch_array($query_sel))
{
  echo "<tr><td colspan='2'><b>Subject: </b>" . $row['pSubj'] . "</td></tr>";
  echo "<tr><td colspan='2'><b>Posted </b>" . $row['pDate'] . " <b>by</b> " . $row['pName'] . "</td></tr>";
  echo "<tr><td></td><td>" . $row['pMsgs'] . "</td></tr>";
  echo "<tr><td colspan='2'><hr></td></tr>\n";
}

(part of) referring page:
PHP:
<p><div id="form_part1">JavaScript is not enabled on your computer.</div>
<?php
  echo "<input type='hidden' name='ref' id='ref' value='" . basename($_SERVER['PHP_SELF']) . "'>"
?>
<div id="form_part2"></div></p>
 

nightscream

New Member
Messages
948
Reaction score
0
Points
0
I'm not sure what you mean but you can refer to a page in js with window.location = "URL"
 
Top