Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts

    to read variable from php for JS

    this is my x.js file

    Code:
    function validatePhone(fld) {
        var error = "";
        var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    
    
       if (fld.value == "") {
            error = "You didn't enter a phone number.\n";
            fld.style.background = 'Yellow';
        } else if (isNaN(parseInt(stripped))) {
            error = "The phone number contains illegal characters.\n";
            fld.style.background = 'Yellow';
        } else if (!(stripped.length == 10)) {
            error = "i wanna read a variable from php )e.g : $a .\n";
            fld.style.background = 'Yellow';
        }
        return error;
    }
    x.php is my php page like following

    PHP Code:
    <?php
    $a 
    aaaaaaaaaaaaa;
    $b bbbbbbbbbbbbbbbb;

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Nope, PHP and Javascript are not compatable together; one is a server side language while the other is client side. Using Javascript you can create an AJAX request to a PHP page for its new variable information.

    Aside from that, if the data is not dynamic you can generate the javascript itself using php and include it in as if it were javascript:
    CustomJS.php
    PHP Code:
    <?php 

    header
    ("Content-type: text/javascript");

    $a aaaaaaaaaaaaa
    $b bbbbbbbbbbbbbbbb
    ?>

    function validatePhone(fld) {
        var error = "";
        var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

       if (fld.value == "") {
            error = "You didn't enter a phone number.\n";
            fld.style.background = 'Yellow';
        } else if (isNaN(parseInt(stripped))) {
            error = "The phone number contains illegal characters.\n";
            fld.style.background = 'Yellow';
        } else if (!(stripped.length == 10)) {
            error = <?php echo $a PHP_EOL;?>
            fld.style.background = 'Yellow';
        }
        return error;
    }
    Code:
    <head>
      <script type="text/javascript" src="path/to/CustomJS.php"></script>
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts
    i tried to do it like you said but it did not run correctly

    could you do it please instead of me . (it is in attachment)
    Attached Files Attached Files

  • #4
    New Coder
    Join Date
    Mar 2010
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post
    $a = aaaaa; cannot work, has to be either

    $a = 12345;
    or
    $a = "aaaaa";

    I wouldn´t echo '' the entire page, better use print <<<END

    Code:
    <?php
    $a = 12345;
    header("Content-type: text/javascript");
    
    print <<<END
    function validatePhone(fld) {
        var error = "";
        var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    
    
       if (fld.value == "") {
            error = "You didn't enter a phone number.\n";
            fld.style.background = 'Yellow';
        } else if (isNaN(parseInt(stripped))) {
            error = "The phone number contains illegal characters.\n";
            fld.style.background = 'Yellow';
        } else if (!(stripped.length == 10)) {
            error = "$a .\n";
            fld.style.background = 'Yellow';
        }
        return error;
    }
    END;
    ?>
    Make sure the line containing END; has no spaces or anything else in that line.


  •  

    Posting Permissions

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