0

I created JavaScript validation into which PHP variables are passed.

onblur="<?= "proveriPolje(this, {$validate['txtUlica']['options']['regexp']}, '{$greske['txtUlica']}', '{$def['txtUlica']}')"; ?>"

But when trying to verify the PHP variables are set before passing them to the JavaScript with this:

  onblur="<?= "proveriPolje(this, isset({$validate['txtUlica']['options']['regexp']}, '{$greske['txtUlica']})', 'isset({$def['txtUlica']}'))"; ?>"

The isset() function is parsed as string, could someone help me with this complex situation?

2
  • isset return true/false, so it's used for if normally, what you want to do? Commented Dec 4, 2014 at 13:01
  • AIUI you're trying to ensure that the PHP vars are set before passing them to JS? Commented Dec 4, 2014 at 13:22

3 Answers 3

1

Looking at it as a raw PHP string your problem is here:

"proveriPolje(this, isset({$validate['txtUlica']['options']['regexp']},

Using { to break out of the string is in the wrong place and it won't work for functions anyway essentially you're just writing "isset" as part of the output string - try something like this (which should pass an empty string to the JS if the PHP isn't set):

onblur="<?= "proveriPolje(this, " 
    . "'" . ( isset($validate['txtUlica']['options']['regexp']) ? $validate['txtUlica']['options']['regexp'] : "" ) . "'"
    . ", '{$greske['txtUlica']}', "
    . "'" . ( isset($def['txtUlica']) ? $def['txtUlica'] : "" ) . "'"
    . ")"; ?>"

Ugly as sin but it should work.

You should end up with the JavaScript function call looking something like:

proveriPolje(this, 'DATA FROM PHP VAR', 'DATA FROM PHP VAR', 'DATA FROM PHP VAR')
Sign up to request clarification or add additional context in comments.

1 Comment

Updated - I'd lost track of the brackets ... I've also added ' marks around the first parameter
0

You have to put your php tags around the variables. php can not know what this javascript function should do.

onblur="proveriPolje(this, {<?= $validate['txtUlica']['options']['regexp'] ?>}, '{<?= $greske['txtUlica'] ?>}', '{<?= $def['txtUlica'] ?>}')"; ?>"

2 Comments

problem is proveriPolje() is javascript function this whole code is in javascript, in your way function is in php
No it is not, The <?= and ?> make sure that only the variables are seen as php code. The function shouldn't be between php tags otherwise the function is seen as php code.
0

eval()? Or simple echo? Since you dont print it this is just a string Btw is this a file, processed by php or a single html? I know its a dumb question but I have just saw a qiestion where the code was ran from html and that was the origin of the problem.

4 Comments

<?= ?> echoes the variable between the tags. :)
And is this short tag enabled on the server?
this is set by default on the server for the new PHP version... you don't need to specify this
Quite strange style... Anyway didnt you missed the concatenation dot? Or this style does not need it?

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.