+ Reply to Thread
Results 1 to 6 of 6

Thread: Returning javascript with php

  1. #1
    thenewprogrammer is offline x10Hosting Member thenewprogrammer is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    45

    Returning javascript with php

    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>";
    
     }

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Returning javascript with php

    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.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    kbjradmin's Avatar
    kbjradmin is offline x10 Elder kbjradmin is an unknown quantity at this point
    Join Date
    Feb 2008
    Location
    Washington State, USA
    Posts
    512

    Re: Returning javascript with php

    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 Code:
    if (file_exists($file)) {
        echo 
    "<script type="javascript"> ... </script>";

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

    the upload.php would then call the ajax like this:
    HTML Code:
    <script type="text/javascript">
        ajax.request('http://www.yourdomain.com/upload-file.php', 'POST', { var1:'yourVar' });
        eval(ajax.response());
    </script>
    Last edited by kbjradmin; 12-06-2009 at 02:31 PM. Reason: fixed a typo :P

  4. #4
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Returning javascript with php

    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.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  5. #5
    kbjradmin's Avatar
    kbjradmin is offline x10 Elder kbjradmin is an unknown quantity at this point
    Join Date
    Feb 2008
    Location
    Washington State, USA
    Posts
    512

    Re: Returning javascript with php

    @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 by kbjradmin; 12-06-2009 at 03:06 PM.

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Returning javascript with php

    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 by misson; 12-06-2009 at 05:37 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. Replies: 0
    Last Post: 12-24-2008, 03:59 PM
  4. Replies: 3
    Last Post: 03-10-2008, 12:22 PM
  5. Using JavaScript ads in my PHP site
    By fattony in forum Scripts & 3rd Party Apps
    Replies: 4
    Last Post: 11-07-2007, 12:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers