Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-11-2010, 05:18 PM   PM User | #1
zodehala
Regular Coder

 
Join Date: Dec 2007
Posts: 269
Thanks: 28
Thanked 0 Times in 0 Posts
zodehala can only hope to improve
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;
zodehala is offline   Reply With Quote
Old 03-11-2010, 05:31 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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>
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-12-2010, 09:18 AM   PM User | #3
zodehala
Regular Coder

 
Join Date: Dec 2007
Posts: 269
Thanks: 28
Thanked 0 Times in 0 Posts
zodehala can only hope to improve
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
File Type: zip sc.zip (1.6 KB, 22 views)
zodehala is offline   Reply With Quote
Old 03-12-2010, 09:43 AM   PM User | #4
Goldfish
New Coder

 
Join Date: Mar 2010
Posts: 26
Thanks: 3
Thanked 1 Time in 1 Post
Goldfish is an unknown quantity at this point
$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.
Goldfish is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:40 AM.


Advertisement
Log in to turn off these ads.