6

I have created an array in PHP. And I need to get that array into a javascript function. This is what I have tried.

$sql = "SELECT * FROM Questions WHERE Form_ID='$FormID' AND QuestionsDataHave='YES' ORDER BY Questions_ID+0, Questions_ID";
$GetTheValidationRule = mysqli_query($con, $sql);
$ValidatinArray = array();
$J = 0;
while($RowVal = mysqli_fetch_array($GetTheValidationRule)){
    $ValidatinArray[$J] = $RowVal['Validation_Type'];
    $J++;
}

And This is my javascript code.

$(document).ready(function() {
    $("form").submit(function(){
    var P= <?php echo json_encode($ValidatinArray); ?>;
        var O = P.length;
        alert(O);
        return false;
    });
}); 

But this gives me an error like this

 SyntaxError: syntax error
 var P= <br />

Isn't it possible to get the array in this way. Please someone help me.

UPDATE: This is the final out put of my error message

<script>
    $(document).ready(function() {
        $("form").submit(function(){
            alert('AAAAAAAAAAAAAAAAAAA');
            var IDsOfTheColumns = document.getElementsByName("DataColumnID[]");
            var Data = document.getElementsByName("DataInputValue[]");
            var A = IDsOfTheColumns.length;
            alert(A);
            <br />
            <b>Notice</b>:  Undefined variable: ValidatinArray in <b>C:\xampp\htdocs\PHIS\CreateTheForm.php</b> on line <b>16</b><br />
            var P = null;   return false;
        });
    });
</script>
Bhavik Hirani
  • 1,916
  • 4
  • 27
  • 41
t4thilina
  • 1,947
  • 3
  • 17
  • 18
  • Is that JavaScript code inside of a PHP file? – Justin Wood Dec 18 '13 at 01:46
  • 1
    I do not believe `echo json_encode($ValidatinArray);` results in a bare `
    `. Is there any processing later on going on there?
    – Wrikken Dec 18 '13 at 01:47
  • 1
    @Wrikken - that `
    ` appeared after @thefourtheye's edit to highlight his code, I assume it came from SO's WYSIWYG
    – scrowler Dec 18 '13 at 01:48
  • 1
    What is the raw result when you click "view html source" in browser – Rezigned Dec 18 '13 at 01:49
  • 3
    Is the location of the PHP and JavaScript on the same page, in your top example? You do know you don't have to assign `$ValidatinArray = array()`, or increment it inside a loop. You can just do `$ValidatinArray[] = $RowVal['Validation_Type'];`, forgetting `$J` and defining as `= array();`. – StackSlave Dec 18 '13 at 01:50
  • @ Justin Wood nd PHPglue: Yes it is. – t4thilina Dec 18 '13 at 01:51
  • 1
    @t4thilina can you try ``var_dump(json_encode($ValidatinArray));`` and show me the result – Rezigned Dec 18 '13 at 01:51
  • So you have php code in a file with `.js` extension? – zerkms Dec 18 '13 at 01:52
  • @scrowler: nope, it was already in the original question (see the _source_), and the OP answered to a now deleted erroneous answer about quoting (you weren't the only one) _"unterminated string literal var P= '
    Npe then the error shows me like this"_ ... which leads me think it's actually there...
    – Wrikken Dec 18 '13 at 01:55
  • @Rezigned: string(289) "["No validation","No validation","No validation","No validation","No validation","No validation","No validation","No validation","No validation","No validation","No validation","No validation","No validation","No validation","No validation","No validation","No validation","No validation"]" – t4thilina Dec 18 '13 at 01:57
  • 1
    Is it double quote issue? – Vicky Gonsalves Dec 18 '13 at 01:58
  • Have you tried: – Jonathan Dec 18 '13 at 02:03
  • What does this mean. Do I need to write it in replacing the Json? – t4thilina Dec 18 '13 at 02:06
  • Why is it ValidatinArray and not ValidationArray? Are you sure you call it the same thing in both places? – JAL Dec 18 '13 at 02:12
  • @t4thilina I can't see how the code snippets you provided fit together. Can you please confirm your code looks like [this gist](https://gist.github.com/simonrobb/8016325)? – Simon Robb Dec 18 '13 at 02:22
  • Might sound stupid but why don't you want to use json combined with ajax on the JS ? You are doing a sql query it sounds like the way to go. – zipp Dec 18 '13 at 05:29
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – miken32 Mar 23 '17 at 04:18
  • Try this : `var P = ;` – Bhavik Hirani May 28 '20 at 18:42
  • I don't understand the number of upvotes here! it's just a simple mistake for undefined variable – Ali Akbar Azizi May 29 '20 at 01:09

5 Answers5

0

The problem is, that in the variable $ValidatinArray is not available in the file, that prints the javascript code. Maybe this manual page helps you:

http://www.php.net/manual/en/language.variables.scope.php

stofl
  • 2,908
  • 5
  • 33
  • 48
0

Your tag is coming from the form that you are submitting. check what your form data is before you encode it to verify the output. you can use console.log($("form));

Also using form is not a good idea since if you have more than one form and form is a global name. For forms you should give it a unique form name like "myForm" so that you can target that specific form.

Hope this helps

0

Sorry for the late response...Try rewriting your document.ready as:

$(document).ready(function() {
    $("form").submit(function(){
    var P = JSON.parse('<?php echo json_encode($ValidatinArray); ?>');
        var O = P.length;
        alert(O);
        return false;
    });
});
0

Try this:

<?php


    echo   ' <script>
             $(document).ready(function() {
                                $("form").submit(function(){
                                var P= '. json_encode($ValidatinArray) . ';
                               var O=P.length;
                                 alert(O);
                               return false;
                          });
                   }); 
               </script>';

?>

What you do is simply echo the js using php.

The Oracle
  • 2,077
  • 3
  • 24
  • 42
-1

In php json_encode the array like this:

$inlinejs='';
$inlinejs.='var validatinArray=\''.addslashes(json_encode($ValidatinArray)).'\';'."\n";
$inlinejs.='var validatinArray=eval(\'(\' + validatinArray + \')\');'."\n";

and in javascript:

$(document).ready(function() {
    $("form").submit(function(){
    <?php echo $inlinejs; ?>
    console.log(validatinArray);
    });
}); 
joHN
  • 1,705
  • 2
  • 15
  • 30
  • `eval()` in JavaScript is not the recommended was to parse JSON data! See [How to parse JSON in JavaScript](http://stackoverflow.com/a/4935684/2594742) for the better way. – AeroX Jan 20 '14 at 12:13