SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Enthusiast cfpmedia.com's Avatar
    Join Date
    Nov 2010
    Location
    California, USA
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP and JavaScript Together?

    Hi there,

    I would like to know how I could implement JavaScript into a PHP file.

    For example, I have a web page that informs the browser to select the javascript.php to be the JavaScript file.
    From there, the code of that file is
    PHP Code:
    <?php

    echo "function validate_form() {
    <!--

    var username = document.getElementById(\"username\");
    var password = document.getElementById(\"password\");
    var status = document.getElementById(\"status\");

    if (username.value.length <= 3) {

    status.innerHTML = \"Username is too short!\";

    username.focus();

    return false;

    }
    else if (password.value.length <= 3) {

    status.innerHTML = \"Password is too short!\";

    password.focus();

    return false;

    }
    else {

    return true;

    }

    //-->
    }"
    ;

    ?>
    How can I use things like str_replace("", "", variable); and whatnot in JavaScript?
    Because, I tried using it with a JS variable, but it didn't work... Any ideas?

  2. #2
    SitePoint Addict MrBaseball34's Avatar
    Join Date
    Sep 2002
    Location
    Round Rock, Texas
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Give this a shot:
    Code:
    <?php
    echo "  document.write('function validate_form() {');\n";
    echo "  document.write('  <!--');\n";
    echo "  document.write('  var username = document.getElementById(\"username\");');\n";
    echo "  document.write('  var password = document.getElementById(\"password\");');\n";
    echo "  document.write('  var status = document.getElementById(\"status\");');\n";
    echo "  document.write('  if (username.value.length <= 3) {');\n";
    echo "  document.write('    status.innerHTML = \"Username is too short!\";');\n";
    echo "  document.write('    username.focus();');\n";
    echo "  document.write('    return false;');\n";
    echo "  document.write('  } else if (password.value.length <= 3) {');\n";
    echo "  document.write('    status.innerHTML = \"Password is too short!\";');\n";
    echo "  document.write('    password.focus();');\n";
    echo "  document.write('    return false;');\n";
    echo "  document.write('  } else {');\n";
    echo "  document.write('    return true;');\n";
    echo "  document.write('  }');\n";
    echo "  document.write('  //-->');\n";
    echo "  document.write('}');\n";
    ?>
    Remember, you must do this in your HTML:
    Code:
    <script type="text/javascript" src="phpscript.php"></script>
    MrBaseball34
    Hook'Em Horns!

  3. #3
    SitePoint Addict MrBaseball34's Avatar
    Join Date
    Sep 2002
    Location
    Round Rock, Texas
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BTW, Here's a javascript equivalent for str_replace:
    JavaScript str_replace - php.js

    put that in yor functions js file and you'll be able to call it just like in php.
    MrBaseball34
    Hook'Em Horns!

  4. #4
    SitePoint Enthusiast cfpmedia.com's Avatar
    Join Date
    Nov 2010
    Location
    California, USA
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, thanks.

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2010
    Location
    Brighton, UK
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not wishing to pour cold water, I would think carefully about dynamically generating JavaScript. Remember, JavaScript is Cached. It there any particular reason this file needs to be in a PHP file, and not just in a plain old javascript file?

  6. #6
    SitePoint Enthusiast cfpmedia.com's Avatar
    Join Date
    Nov 2010
    Location
    California, USA
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no reason, really. I am new to JavaScript, and I was just curious if it were possible to a large extent. But I realize the answer now. So I require no more assistance, thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •