Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I am currnetly programming a file hosting website in PHP and I am slowly integrating AJAX into the website.
Here is my problem:
The user uploads the file.
The server processes the file and stores a $_SESSION['code'] variable.
After the upload is complete, the server supplies a link that allows the user to view their link codes.
Once they click the link, the page will return the information stored in the $_SESSION['code'] variable. The normal link which opens a new page works flawless. However, when I try to return that information using AJAX, the $_SESSION['code'] variable is showing up as empty.
Here are the files I'm using:
[b]upload.php[/b]
This page will process the upload, rename the file, move it, store some database information, and set the session variable $_SESSION['code']
This page contains a script tag with ajax.js as the src.
When the upload is complete, it will display a link that allows the user to execute the getLinkCodes function
getLinkCodes('linkcodes.php', 'maincontainer');
[b]ajax.js[/b]
[syntax="javascript"]function getLinkCodes(datasource, divID){
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject){
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}
if (XMLHttpRequestObject) {
var obj = document.getElementById(divID);
obj.innerHTML = "Loading...";
XMLHttpRequestObject.open("GET", datasource);
XMLHttpRequestObject.onreadystatechange = function(){
if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200){
obj.innerHTML = XMLHttpRequestObject.responseText;
}
}
XMLHttpRequestObject.send(null);
}
}
linkcodes.php - This will actually display the link codes so that the user can copy and paste them into a forum without any further editing.
This is the page that the XMLHttpRequest actually returns.
Code: Select all
<script type="text/javascript" src="templates/base.js"></script>
<script src="templates/members/multifile_compressed_image.js"></script>
<script type="text/javascript" charset="utf-8">
<!--
var img_files = {};
try
{
img_files = [<?php echo $_SESSION['code']; ?>];
}
catch ( error ) { img_files = {}; }
function write_tags ( type, where )
{
if ( typeof where == 'undefined' ) return false;
var target = $(where);
if ( !target ) return false;
var code = '';
var cntr = 1;
var str = new Array();
for ( var i = 0; i < img_files.length; ++i )
{
var file = img_files[i];
switch ( type )
{
case 'link_bb':
{
if ( file.is_image == 1 ){
code += '[url=' + file.url + '][img]'%20+%20file.thumb_url%20+%20'[/img][/url] ';
if ((cntr%4) == 0){
str.push(code);
code = '';
}
cntr++;
}else{
code += '[url=' + file.url + ']' + file.name + '[/url]';
str.push(code);
code = '';
}
} break;
case 'link_html':
{
if ( file.is_image )
var code = '<a href="' + file.url + '">\n <img src="' + file.thumb_url + '" alt="' + file.name + '" />\n</a>\n';
else
var code = '<a href="' + file.url + '">' + file.name + '</a>\n';
str.push(code);
}
break;
case 'direct_bb':
{
var code = file.is_image ? '[img]'%20+%20file.d_url%20+%20'[/img]' : '[url=' + file.d_url + ']' + file.name + '[/url]';
str.push(code);
}
break;
case 'direct_html':
{
var code = file.is_image ? '<img src="' + file.d_url + '" alt="' + file.name + '" />' : '<a href="' + file.d_url + '">' + file.name + '</a>';
str.push(code);
}
break;
case 'direct_url':
{
str.push ( file.d_url );
}
break;
}
}
if (code != '' && type == 'link_bb'){
str.push(code);
}
target.value = str.join("\r\n");
}
function switch_bb_case ( bb_case )
{
var target = $('link_codes');
function toUpper(str){alert(str);}
if ( bb_case == 'upper' )
{
target.value = target.value.replace ( /\[(\/)?img\]/gm, '[$1IMG]' );
target.value = target.value.replace ( /\[(\/)?url\]/gm, '[$1URL]' );
target.value = target.value.replace ( /\[url=/gm, '[URL=' );
}
else
{
target.value = target.value.replace ( /\[(\/)?IMG\]/gm, '[$1img]' );
target.value = target.value.replace ( /\[(\/)?URL\]/gm, '[$1url]' );
target.value = target.value.replace ( /\[URL=/gm, '[url=' );
}
}
function imgInit ( )
{
write_tags('link_bb','link_codes');
}
addLoadEvent ( imgInit );
-->
</script>
<div class="middle1">
<div align="center">
Linking codes: <span class="link" onclick="write_tags('link_bb','link_codes');return false;">BB format</span> - <span class="link" onclick="write_tags('link_html','link_codes');return false;">HTML format</span>
Direct link: <span class="link" onclick="write_tags('direct_bb','link_codes');return false;">BB format</span> -
<span class="link" onclick="write_tags('direct_html','link_codes');return false;">HTML format</span> -
<span class="link" onclick="write_tags('direct_url','link_codes');return false;">Just the direct URLs</span>
</p>
<textarea rows="15" cols="100" id="link_codes" name="link_codes" style="padding: 2px;"></textarea>
</div>
</div>
As I said, the linkcodes.php is the "text" that the getLinkCodes function returns. However, The javascript in the linkcodes.php page does not load at all because I'm guessing it's not passed back as text.
Please let me know what you guys think and thanks for the help in advance.
feyd | Please use[/syntax]
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
EDIT: Sorry about the code tag errors. It was early and I was 1/2 a sleep. I'll make sure I get the right with my next post.