0

im using libphonenumber js with php to validate a list of numbers from a file:

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>

i want each $number in the php to be passed to the var number in js, and return true or false to the php if statement. sorry for bad english

devilbone
  • 17
  • 6

1 Answers1

0

PHP is executed server-side and JavaScript is executed client-side (in the browser). You can only pass PHP variables to the JS script, like this:

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

but the script generated in the PHP is going to be executed on the clients browser, and by that time PHP will stop working. So you can't (directly) pass your JS variables to the PHP script. You could do that using AJAX, but I don't think this is what you mean.

Michal Leszczyk
  • 1,849
  • 15
  • 19