XRS Posted August 2, 2013 Share Posted August 2, 2013 Hello, I'm here with a question that maybe silly for most of you but I just can't handle it. As I know, there's no way in built in PHP inside JS because diferent kind of languages ( Server Side and Client Side ). But I have a problem that I need help to handle. I have a multilanguage system in my site ( PHP ) and form validations ( JS ). I have the error messages showed by JS instantly, but I can only print text, not a PHP variable info. For example: $(function() { // validate signup form on keyup and submit $("#loginform").validate({ rules: { user: "required", password: "required", }, messages: { user: "Please enter your username or email.", password: "Please enter your password", } }); }); And my multilanguage system uses the following system: $lang['login_error'] = 'Incorrect details provided.'; There is any way to make something like this: $(function() { // validate signup form on keyup and submit $("#loginform").validate({ rules: { user: "required", password: "required", }, messages: { user: <?php echo $lang[login_error']; ?>, password: "Please enter your password", } }); }); I know this doesn't work, but it's to make an easy example in what I need, because depending the language the user as choosen, the error should be there in that language, not only in one language. Could you please help me to handle this ? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/280760-php-code-inside-javascript-how-to-handle-it/ Share on other sites More sharing options...
Muddy_Funster Posted August 2, 2013 Share Posted August 2, 2013 You would need to use AJAX to run the PHP on the server and pull back the response into the current page Quote Link to comment https://forums.phpfreaks.com/topic/280760-php-code-inside-javascript-how-to-handle-it/#findComment-1443261 Share on other sites More sharing options...
kicken Posted August 2, 2013 Share Posted August 2, 2013 (edited) There is any way to make something like this: Use json_encode when outputting variables: $(function() { // validate signup form on keyup and submit $("#loginform").validate({ rules: { user: "required", password: "required", }, messages: { user: <?php echo json_encode($lang['login_error']); ?>, password: "Please enter your password", } }); }); This is assuming your JS is within the PHP page. If it's in a separate .js file you'll need to either make the JS file parsed by PHP or possibly load the messages w/ ajax or similar. Edited August 2, 2013 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/280760-php-code-inside-javascript-how-to-handle-it/#findComment-1443270 Share on other sites More sharing options...
XRS Posted August 4, 2013 Author Share Posted August 4, 2013 Hello, I have that validation in a different file. There's anyway to show me an example how could be parsed? My knowledge about JS/Ajax is not great and I'd like to have an example to try it out. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/280760-php-code-inside-javascript-how-to-handle-it/#findComment-1443437 Share on other sites More sharing options...
nogray Posted August 5, 2013 Share Posted August 5, 2013 (edited) You can define your language variables as a global javascript variable in your page and use that variable in your validation function for example, in your php page add the following (You might want to only print out the error messages used by your javascript instead of the entire language array). <script type="text/javascript"> var $lang = <?PHP echo json_encode($lang); ?>; <script> and in your javascript validation file, you can use the language variable as user: $lang['login_error'], Edited August 5, 2013 by nogray Quote Link to comment https://forums.phpfreaks.com/topic/280760-php-code-inside-javascript-how-to-handle-it/#findComment-1443578 Share on other sites More sharing options...
XRS Posted August 6, 2013 Author Share Posted August 6, 2013 Hello, Thanks. That worked very well. It's now solved and working like a charm =D Quote Link to comment https://forums.phpfreaks.com/topic/280760-php-code-inside-javascript-how-to-handle-it/#findComment-1443692 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.