-1

I would like to pass a PHP array from JavaScript.

PHP Data Table

---------------------------
| adminID |  adminEmail   |
===========================
|    1    | abc@gmail.com |
|    2    | xyz@ymail.com |
===========================

Javascript

<script>
    function checkuser_callback_function($el, value, callback) {
    var $array = new Array("xyz@gmail.com","abc@ymail.com");
    var valid = false;
    if($array.indexOf(value) == -1){
        valid = true;
    }
    callback({
        value: value,
        valid: valid,
        message: "User present."
    });
}
</script>

I want to pass that adminEmail here var $array = new Array("..","..");

I have tried alot in php but I didn't get any result.

<?php
    include 'config.php';
    $sql ="SELECT adminEmail FROM BEadmin";
    $result = mysqli_query($con,$sql);

    while($row = mysqli_fetch_assoc($result)) {
        $array = $row;
        $str = "'" . implode ( "', '" ,$array ) . "'";
        $parts = split("'", $str);
        print_r($str);
    }
?>
Ben Löffel
  • 931
  • 4
  • 12
  • 29
GermanCoder
  • 73
  • 2
  • 12
  • 2
    use JSON to pass between javascript and PHP – Farkie Nov 22 '17 at 08:39
  • I am not familiar with Javascript or ajex. I am new in javascript. – GermanCoder Nov 22 '17 at 08:41
  • in PHP `json_encode($variable)` on your result and return it. Then in Javascript `JSON.parse(result)` to receive the object returned. – Classified Nov 22 '17 at 08:51
  • @Classified can you create that script in answer? – GermanCoder Nov 22 '17 at 08:53
  • Do you already have setup an AJAX call (if so what variable is the return)? – Classified Nov 22 '17 at 08:57
  • basically I am using [jqboostrapValidation](https://reactiveraven.github.io/jqBootstrapValidation/) in which I am using `data-validation-callback-callback` function. I am using that script to validation that the given email is exist in DB or not. – GermanCoder Nov 22 '17 at 09:00
  • You need to setup some AJAX to actually retrieve this data from the server. A regular callback with no AJAX wont do. Go check their AJAX section it basicly describes the entire server to client transaction. – Classified Nov 22 '17 at 09:02
  • With PHP, you can do something like `echo '
    ';`. Then retrieve it in Js with `JSON.parse(document.querySelector('div[data-array]').getAttribute('data-array'));`. But it's just a trick. Better use AJAX, and provide your data trough an API.
    – Alcalyn Nov 22 '17 at 10:52

2 Answers2

-2

You can use php to create javascript array. Then use the same in the script later. Please note that , just to differentiate, I have called javascript array jArray instead of $array. Rest is explained along in the script only. Hope this helps..

        <?php
        include 'config.php';
        $sql ="SELECT adminEmail FROM BEadmin";
        $result = mysqli_query($con,$sql);

        //start script tag
        echo "<script>\n";
        // javascript array decalaration beginning
        echo "var jArray = new Array(";

        $arrStr = "";
        while($row = mysqli_fetch_assoc($result)) {
        //                $array = $row;
        //                $str = "'" . implode ( "', '" ,$array ) . "'";
        //                $parts = split("'", $str);
        //                print_r($str);
            $arrStr .= '"'. $row["adminEmail"] . '",';
        }

        // drop the last , from the string
        $arrStr = substr($arrStr,0,-1);
        // now populate javascript array with this string
        echo $arrStr;

        // end javascript array
        echo ");\n";
        echo "</script>\n";
    ?>  
T.Shah
  • 2,768
  • 1
  • 14
  • 13
-2
<?php

    include 'ePHP/config.php';
    $sql ="SELECT adminEmail FROM BEadmin";
    $result = mysqli_query($con,$sql);



    $arrStr = "";
    while($row = mysqli_fetch_assoc($result)) {
    //                $array = $row;
    //                $str = "'" . implode ( "', '" ,$array ) . "'";
    //                $parts = split("'", $str);
    //                print_r($str);
        $arrStr .= '"'. $row["adminEmail"] . '",';
    }

    // drop the last , from the string
    $arrStr = substr($arrStr,0,-1);
    // now populate javascript array with this string


?>
<script>
    function checkuser_callback_function($el, value, callback) {
    var $array = Array(<?php echo $arrStr; ?>);
    var valid = false;
    if($array.indexOf(value) == -1){
        valid = true;
    }
    callback({
        value: value,
        valid: valid,
        message: "User present."
    });
}
</script>
GermanCoder
  • 73
  • 2
  • 12