Returning javascript with php

thenewprogrammer

New Member
Messages
45
Reaction score
0
Points
0
Hey, wondering how i would return javascript from testing php page. Like for uploading, if the page with upload button is on is upload.php and the testing is on upload-file.php. How would i return javascript from upload-file.php. When i put javascript in upload-file.php it only works for that page but wont show it to the user on the upload.php file.

I want to return this message to the user

Code:
if(file_exists($file)){

 echo "<script type=\"text/javascript\">alert('This file already exists: Please rename your file');</script>";

 }
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
I believe you're looking for AJAX. In particular, load upload-file.php in a hidden iframe, and set the target of the form to the iframe's name.
 

kbjradmin

New Member
Messages
512
Reaction score
2
Points
0
misson's method would work. you could also do it something like this...

this is the code i use when i need to do ajax:

Code:
// initialize the ajax request object
ajax = new Object();
ajax.response = null;

/******************************************************************************
*
* Ajax Request Function -
* Makes an AJAX server request and returns the results.
*
* Use the format:
*     ajaxRequest(url, method, info);
* Where `url` is the address of the script
*     eg. http://www.yourdomain.com/ajax.php,
* `method` is the request method
*     eg. "POST" or "GET",
* and `info` is an array of values to be passed to the script
*     eg. { name:'john', email:'you@yours.com' }
*
******************************************************************************/

ajax.request = function (url, method, info) {
    if (typeof url == 'undefined') {
        return false;
    }    
    var xmlhttp;
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (typeof info == 'undefined') {
        xmlhttp.open(method, url, false);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.send(null);
        ajax.response = xmlhttp.responseText;
        return true;
    }
    var query = '';
    for (var i in info) {
        query += '&' + i + '=' + info[i];
    }
    query = query.substring(1);
    if (method == 'POST') {
        xmlhttp.open("POST", url, false);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.send(query);
        ajax.response = xmlhttp.responseText;
    } else if (method == 'GET') {
        xmlhttp.open("GET", url + '?' + query, false);
        xmlhttp.send(null);
        ajax.response = xmlhttp.responseText;
    }
}



/******************************************************************************
*
* Get Ajax Response Function -
* Gets the response from an ajax request.
*
******************************************************************************/

ajax.getResponse = function (reset) {
    if (typeof reset == 'undefined') { reset = true; }
    var response = ajax.response;
    if (reset == true) {
        ajax.response = null;
    }
    return response;
};

then you upload-file.php would, instead of something like this:
PHP:
if (file_exists($file)) {
    echo "<script type="javascript"> ... </script>";
}

it would just return the javascript expression (eg. the alert).
PHP:
if (file_exists($file)) {
    echo "alert('This file already exists');";
}

the upload.php would then call the ajax like this:
HTML:
<script type="text/javascript">
    ajax.request('http://www.yourdomain.com/upload-file.php', 'POST', { var1:'yourVar' });
    eval(ajax.response());
</script>
 
Last edited:

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
The system could be even simpler where the upload-file.php script only returns 0 or 1 and the upload.php AJAX processes the response and acts accordingly.
 

kbjradmin

New Member
Messages
512
Reaction score
2
Points
0
@xav0989, that is normally how i would do it too, but the question was about "Returning javascript with php", so that's what i showed them how to do. but, yes, i would agree.
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
The difficulty with XHR is that (as far as I know) there's no cross-browser way of sending files using it. You can get XHR to send files in Firefox, but it requires changing the configuration settings, which a) makes FF less secure and b) requires too much from users, in terms of both comfort with mucking about with settings and making them have to jump through extra hoops. Forms and iframes, however, work fine.
 
Last edited:
Top