dbawel Posted October 4, 2017 Share Posted October 4, 2017 Hello, I have a form built using PHP, and need to pass the text input value to an external .js file. Here is the PHP code: <?php $brsh_size = 1; ?> <span> <div><span><center><input class = 'sp-flat' type="color" id="picker" value="#000" act="get-color" /></center></div> <center class='font-brush'>Brush Size:</a><input type="text" size="3" maxlength="3" id="br_size" act="get-brushsize" value="<?php echo $brsh_size;?>"></div></center> </span> I have a case is an external .js file where the case is called correctly: case 'get-brushsize': //var myPhpValue = $("#brsh_size").val(); //console.log(myPhpValue); //var brush_size_i = document.getElementById("br_size").value; //console.log("brush size is " + brush_size_i); break; I left some of my trial code for reference, but please ignore as I've gotten much farther with this - but run into issues. I've tried every method on stack overflow and other sites, and am unable to '_GET' the value of the PHP variable (not my first choice) but preferably the text input from the input 'id'. Any help is appreciated. Thanks, DB Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted October 4, 2017 Share Posted October 4, 2017 just fire a function that grabs the value from where you need and passes it to what ever function you need. If you are using my "act" bindings from the system I helped you with its super easy. Ill call you tonight when I get off work. I got you fam ^_^. Quote Link to comment Share on other sites More sharing options...
Raggar Posted October 4, 2017 Share Posted October 4, 2017 I think you should seperate PHP and Javascript whenever possible. If you change your Javascript files extension to .php, the following does, however, seem to work for me (In FF at least): index.php <?php $value = "'With more PHP?'"; ?> <html> <head> <script type="text/javascript" src="js.php"></script> </head> <body> <script> console.log(jsval + " in a Javascript Variable?..." + <?php echo $value;?>); </script> </body> </html> js.php <?php $jsval = json_encode('PHP Value'); ?> var jsval = <?php echo $jsval;?>; Outputs "PHP Value in a Javascript Variable?...With more PHP?" to the console. Integers don't seem to need json encoding, but strings do. Haven't tried floats. Quote Link to comment Share on other sites More sharing options...
TomaszFurca Posted October 5, 2017 Share Posted October 5, 2017 I prefer to do that like this: <?php $jsval = json_encode('PHP Value'); ?> var jsval = '<?php echo $jsval;?>'; I think about ' ' between php code in js, because in case of null value of php variable js will terminate due to syntax error; Raggar 1 Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted October 5, 2017 Share Posted October 5, 2017 I got him on track last night, this is "solved"... Quote Link to comment Share on other sites More sharing options...
Raggar Posted October 5, 2017 Share Posted October 5, 2017 38 minutes ago, Pryme8 said: I got him on track last night, this is "solved"... Which solution did he choose? AJAX or something? Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted October 5, 2017 Share Posted October 5, 2017 Correct file structure. He is not using a standard set up, I know because I designed the system for him ^_^... but basically an event system with custom callbacks that has the value passed through the event. Pretty much could not do what he was doing because context was being pulled by AJAX post page propagation. var self = this; this.BINDINGS = { MOUSEDOWN: function(e) { ... }, MOUSEUP: function(e) { ... }, CHANGE: function(e) { var _act = (e.target).getAttribute('change-act'); if (_act) { e.preventDefault(); } console.log(_act); self.ACTS(e, _act); return } }; document.addEventListener("change", this.BINDINGS.CHANGE, false); document.addEventListener("mousedown", this.BINDINGS.MOUSEDOWN, false); document.addEventListener("mouseup", this.BINDINGS.MOUSEUP, false); this prolly does not make much sense unless you see the whole object and I excluded script hence the '...' but yeah its an interesting structure for dynamic content. Might have to write up a tutorial at some point showing this kind of structure at work because its super powerful. Id post more but out of respect for Daves projects I cant. But for anyone wondering on how to pass JS vars to PHP, yeah a xhr request with a _POST is ideal. But PHP to JS is different, and it was not really PHP to JS, it was HTML to JS in this instance. Quote Link to comment Share on other sites More sharing options...
dbawel Posted October 6, 2017 Author Share Posted October 6, 2017 Hello, @Pryme8 is correct in his last post, and I personally would be using Ajax to _Post the variable - or echo the PHP variable in javascript text which is very straight forward; and there are many examples on the Stack Overflow forum with jsfiddle code examples for this and other methods - depending on your needs. In the script example above, I was passing an event to a specific case incorrectly; whereas he added a 'CHANGE:' function which allows me to change values when passing the event to specific cases - however, it's no obvious in the example above as there's more to the code which would require far more than a single post to expand upon. It's also a very different approach architecturally than what most Developers would choose for what is generally required to simply pass variables between JS and PHP. My issue was that I didn't fully understand that I wasn't passing the correct event at the correct time within the code that @Pryme8 initially set up for me governing how variables and functions are called and passed within the app until I got too frustrated on my own and contacted him directly - which he then solved in about about 20 minutes following my wasting 2 days of my time prior. After working on several projects with him, I find he is always at least 10 steps ahead of me when it comes to solving coding issues. If anyone else is truly having issues passing variables between JS and PHP, then create a post and I can point you to a few simple examples of what is normally used as methods in passing variables between PHP and Javascript - which actually wasn't my problem - but valuable to know. Otherwise, I'm back on track and moving quickly through building the UI which is built in HTML and CSS primarily using PHP - with Javascript to handle most of the dynamic functions within the app. I would have to say that whenever I need serious help, it's been well worth hiring @Pryme8 to handle much of the "heavy lifting" when it comes to finding solutions to unique problems in games and apps - worth every penny I've ever spent bringing him into a project. I know this sounds like an ad for hiring him, but it actually is - as I'd have developed a far different and less efficient architecture on two different projects recently - as he saved my ass on both building something I could work with and easily expand, once I could mentally grasp what he'd built. Otherwise, the process in working to find solutions on my own has schooled me and made me a far better developer, so I owe him a great deal for building the elements and leaving me to finish the work - as he was busy on other projects. And now I can assist other developers in areas which I had absolutely no clue - and am thinking way out of the box to solve many problems by anticipating functionality far in advance. So I'll mark this question as solved, however, if anyone needs help with PHP and JS, I'm happy to say I can now assist in most areas. Create a new post if there is still an issue for other developers on this forum, and I'm certainly happy to help with what I've recently learned the hard way. Cheers, DB Pryme8 1 Quote Link to comment 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.
Note: Your post will require moderator approval before it will be visible.