如果条件,在PHP中使用js?

时间:2014-10-26 16:56:40

标签: javascript php libphonenumber

使用libphonenumber js和php验证文件中的数字列表:     

include 'SimpleXLSX.class.php';
$lines = array();

if(isset($_FILES['file']))
{
    $filename = $_FILES['file']['name'];
    $filetmpname = $_FILES['file']['tmp_name'];
    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    print_r($ext);

    if($ext == 'csv')
    {   
        $fr = fopen($_FILES['file']['tmp_name'], 'r+');
        while( ($row = fgetcsv($fr, 8192)) !== FALSE ) 
        {   
            $lines[] = $row;
        }
    }           
    else if($ext == 'xlsx')
    {
            $xlsx = new SimpleXLSX($filetmpname);
            $rows = $xlsx->rows();

            foreach ($rows as $row)
            {
                $lines[] = $row;    
            }                                               
    }
}
$objects = (object)$lines;


$number='';

foreach($objects as $object)
{   
    $number = $object[7];   


    if(<script> </script>');


    var_dump($object[7]);
}










?>

<html>  
    <h2>libphonenumber Tool</h2>


<form action="" method="post" accept-charset="utf8" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="btn_submit" value="Upload File"  />


<script src="goog/base.js"></script>
<script>
  goog.require('goog.proto2.Message');
</script>
<script src="phonemetadata.pb.js"></script>
<script src="phonenumber.pb.js"></script>
<script src="metadata.js"></script>
<script src="phonenumberutil.js"></script>
<script src="asyoutypeformatter.js"></script>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

<script>






var number = <?php print_r(json_encode($number));?>;
var regionCode = null;
var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
/* Grab the parser. */
//var isNumberValid = phoneUtil.isValidNumber(myStr);
/* Parse the phone number. */
var proto = null;
try {
    proto = phoneUtil.parse(number, "US");
  //  if(myStr = isNumberValid){console.log('succsess');}
} catch (error) {
    proto = error.toString();
}

var isNumberValid = phoneUtil.isValidNumber(proto);
if(isNumberValid)
{
    alert('Valid');
}
else{alert('not valid')};enter code here
</script>

我希望php中的每个$ number都传递给js中的var number,并返回true或false给php if语句。 抱歉英文不好

1 个答案:

答案 0 :(得分:0)

PHP在服务器端执行,JavaScript在客户端执行(在浏览器中)。您只能将PHP变量传递给JS脚本,如下所示:

<?php
    $x1 = 'foo';
    echo ('<script type="text/javascript">var x1 = "' + $x1 + '";</script>');
?>

但是在PHP中生成的脚本将在客户端浏览器上执行,到那时PHP将停止工作。所以你不能(直接)将你的JS变量传递给PHP脚本。你可以使用AJAX做到这一点,但我不认为这就是你的意思。