0

My index.php page includes a config.php file, which returns an array that I have defined some variables in by using "define('var1' , 10)".

I am trying to validate my forms input, but I can't figure out how I can reference var1 from within the JS function. What is the easiest way to do this?

2 Answers 2

1

Just echo it to a javascript variable:

<script type="text/javascript">
    var var1JS = "<?php echo $var1; ?>";
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

Not quite sure I am full understanding without seeing the code but, you could echo the variable from the PHP array in the JS function (as above answer).

Or Echo the entire JS query:

$y = count($PHPdata_array);

echo "function exampleFunction() {";
echo "var ArrayName = [";
for ($i = 1; $i <= $y; $i+=2) {
echo "{" . $PHPdata_array[$i] . "," . $PHPdata_array[$i-1] . "},";
}
echo "];";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.