NotionCommotion
Members-
Posts
2,446 -
Joined
-
Last visited
-
Days Won
10
Everything posted by NotionCommotion
-
How best to send PHP data to a JavaScript client
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Yes, I know the nightmare of dynamically generated JavaScript. I remember doing it and thinking I was so smart. I soon learned the error of my ways. The limited dynamically generated JavaScript used to populate a variable has much less of the troubleshooting issues, however, I expected it wasn't best practice thus asked the question. Just curious, on this limited use of just setting a variable, what are the security vulnerabilities? Will Ajax's asynchronous behavior cause problems? I might have script in other files that needs access to the object, thus putting it in a callback will not be so simple. -
How best to send PHP data to a JavaScript client
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
The client later uses the data to perform various given tasks. For instance, maybe the myObj consists of ten arrays. User clicks the "three" button, and the data in array 3 is displayed. So, my JavaScript needs to be able to access myObj.array3. My thought of doing it this way instead of individual Ajax calls upon each click is that they will be clicked more than once on a given page load, and I want to reduce the calls to the server. header("Content-type: text/javascript"); $myObj=array( 'array1'=>array(1,2,54,213,54), 'array2'=>array(8,4,213,54), 'array3'=>array(4,2,54,213,54), ... 'array10'=>array(77,2,54,23,54) ); exit('var myObj='.json_encode($myObj)); -
I am successfully doing this, however, I believe I am doing it wrong. I am currently doing the following. How should I be doing this? My thought is getObject.php should return JSON, not JavaScript, but I don't know how to assign the received JSON to the myObj variable. Thanks PS. If there is a special way to do so when using Twigs, please advise. <script type='text/javascript' src='getObject.php?id=123'></script> <script type='text/javascript'> console.log(myObj); </script> getObject.php header("Content-type: text/javascript"); $myObj=array('foo'=>'bar'); //Actually, queries DB using $_GET['id'], and gets a bunch of data exit('var myObj='.json_encode($myObj));
-
For me, the best teacher is an example. Try out the following script. Submit the form and click the links. See what happens when you follow the name with square brackets. You are correct in your use of GET. Typically, you want to use GET when you are getting something from the server, and POST when you are changing something on the server. <?php $url=htmlspecialchars($_SERVER['PHP_SELF']); echo("GET:<pre>".print_r($_GET,1)."</pre> POST:<pre>".print_r($_POST,1)."</pre> <hr> <form action='{$url}' method='post'> <input name='p1' type='text' value='value for p1' /> <input name='p2' type='text' value='value for p2' /> <input name='p3' type='text' value='value for p3' /> <input name='p_array[]' type='text' value='value for p1_array' /> <input name='p_array[]' type='text' value='value for p2_array' /> <input name='p_array[]' type='text' value='value for p3_array' /> <input name='submit' type='submit' value='SUBMIT' /> </form> <ul> <li><a href='{$url}?g1=123&g2=321'>link 1</li> <li><a href='{$url}?g_array[]=123&g_array[]=321'>link 1</li> </ul>"); ?>
-
Should I use a template engine?
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
No, I don't insist. Just adapting to a new way of looking at things. In reflect, I agree they shouldn't come from the controller. Again, I am looking at things differently, and it takes a bit of time it to sink in. I am stronly leaning towards templates being a good thing unless you have a site with one million hits per hour in which you have no problems. I posted a related post at http://forums.phpfreaks.com/topic/291199-recommended-application-structure/, and would appreciate any comments. Thanks -
I typically structure my applications something like the following. I lump general functionality into a given component. For instance, I might have com_users component which deals with user administration, and implements the ability to view a list of users, view a given user, delete a user, edit a user, etc. This structure was borrowed long ago from my learning PHP Joomla days which I no longer use. index.php figures out which component is being accessed, evokes the controller which gets data from the model and sends the data to the view. The view defined in each component only deals with the central content, and mainTemplate.php deals with the peripheral. First question. Is there anything inherently wrong with this approach? Second question. I am looking to start using Twig. Would this same structure work? I would replace /var/www/html/lib/templates/mainTemplate.php with a Twig file, and replace var/www/components/component1/views/view-1-1.php, etc with Twig files which extends the main Twig template. /var/www/components/component1/controllers/controller-1-1.php /var/www/components/component1/controllers/controller-1-2.php /var/www/components/component1/models/model-1-1.php /var/www/components/component1/views/view-1-1.php /var/www/components/component2/etc/etc /var/www/html/lib/templates/mainTemplate.php /var/www/html/lib/templates/mainCSS.css /var/www/html/lib/js/someJSforAllComponents.js /var/www/html/lib/components/component1/js/someJSForComponent1.js /var/www/html/index.php
-
Hi Guy, You should learn MySQL. It doesn't take much time to learn the basics. For a totally new subject, I like the In Easy Steps books. They have one for MySQL and PHP, but I don't know if they are out of date. Regardless, you get throught them in a couple of hours, and won't know everything, but will know the basics. If you want to get more into writing queries, the Simple SQL book is good. If you still want to wait, you could always create an array or object which mimics the output of MySQL. For insteand, create the following array. Then use it to practice your loops, etc. $fakeDBoutput=array( array(3,"hello"), array(5,"hello"), array(7,"hello"), array(8,"hello"), );
-
Should I use a template engine?
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Thanks again Jacques1, I see your point regarding HTML in the PHP script for a select menu, etc. How do you handle this? Previously, I would pass an array of options along with the selected value, and use a function to create a select menu. Could I and should I do something similar with Twig? Also, what about passing an array of JS/CSS/etc filenames from the controller to the Twig view? While I don't see a way around it, it goes against keeping the controller totally isolated from presentation content. -
Should I use a template engine?
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Thanks Jacques1, Yes, I knew you used Twig, but didn't know whether you just did the backend portion, and had someone else do the frontend template work. Just curious, but do you use Syphony2 as well? I've obviously looked at the Twig documentation, but I haven't found any tutorial describing typical application-wide design patterns. My current thoughts (which might be wrong) are that I should make one general page template which includes the HTML, HEAD, BODY tags and other general content. I would then extend this template to be more specific. I will pass an array listing external JS/CSS/etc resources to the template which would in turn insert them in the HEAD. I am uncertain whether I will use PHP directly to create navigation menu and select option HTML and pass these to the template, or have the template create them from an array, or maybe there is some other approach altogether (thoughts?). I could also use a second general template to render large HTML blocks requested from Ajax on the rare occasions that I don't want to just send back JSON. I would appreciate any comments. Also, if you know of a good article/book/blog/etc, please let me know. Thanks -
Should I use a template engine?
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Quick question for you Jacques1. Do you actually ever use twigs, or do you just work on the backend side? -
Should I use a template engine?
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
I've messed around with twig a bit. Haven't made up my mind yet, however, I really do like the inheritance feature. -
Giving my PHP loops unique pages with links
NotionCommotion replied to Nickmadd's topic in PHP Coding Help
I'm a little confused. I don't see any PDO, and your script seems to be unrelated to your questions. Normally, you would create a list with a bunch of links. Each link would be something like <a href="index.php?task=getItem&id=123 />Item 123</a>. Your server would then implement the getItem task and use the ID to create the page. -
Thats all your stuck on? Start with a calender. Keep it client side with JS (jQueryUI has one). Now you have two dates in your form. One option is to submit your form using Ajax. Server then gets the two dates, validates them, and performs a query between those two dates. The server then creates the zip file, and stores it in a tmp directory with a random name, and returns the name of the file to the client. The client then redirects to that file. Consider a cron job to keep your temp folder from filling up. Alternately, you could traditionally submit your form, and then stream back the file, but it will take more resources. I am sure there are other ways to skin this cat.
-
Should I use a template engine?
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Thank you Jacques1 and sKunKbad, You both bring up excellent, yet conflicting, points. I guess I will need to ponder this for a bit. I understand your reasoning behind staying away from magic numbers and using constants instead. Please elaberate on what you mean regarding not "Hard-coding the character encoding". -
Hello All! Below is a simplified version how I render about every page. Should I start using a dedicated template engine? If so, which one and why? Or, should I stick with my existing system? If so, should I make any changes to it? Thank you <?php class myModel { public function getData() //This would really be a query to a DB { $dataFromMyDB=new stdClass(); $dataFromMyDB->firstname='John'; $dataFromMyDB->lastname='Doe'; $dataFromMyDB->age=111; return $dataFromMyDB; } } /** * Typically used to htmlspecialchars variable. * $nbsp=0: return htmlspecialchars input * $nbsp=1: return htmlspecialchars input or   if nothing * $nbsp=2: return input or   if nothing * @param $html * @param boolean $nbsp */ function h($html,$nbsp=0) { $html=trim($html); switch($nbsp) { //Consider changing ENT_QUOTES to ENT_QUOTES|ENT_XHTML or ENT_QUOTES|ENT_HTML401 case 0:default:$return=htmlspecialchars($html, ENT_QUOTES, 'UTF-8');break; case 1:$return=($html)?htmlspecialchars($html, ENT_QUOTES, 'UTF-8'):' ';break; case 2:$return=($html)?$html:' ';break; } return $return; } function render($viewFile,$model) { ob_start(); require $viewFile; $html_string=ob_get_contents(); ob_end_clean(); return $html_string; } $model=new myModel(); echo(render('mvc_view.php',$model->getData())); ?> mvc_view.php <p>Hello! This is my View</p> <?php echo(h($model->firstname).' '.h($model->lastname).' is '.h($model->age).' years old.'); ?>
-
Thanks! Can mod_proxy only be added via conf file and not htaccess? Also, will it only effect websites and not email? In regards to bandwidth, it is a very low traffic site. Thanks again!
-
I tried both ways but neither worked. Adding the flag [P] resulting in site not being found. Adding the ProxyRequests code to my HTACCESS file resulted in a 500 server error. Any suggestions? Thank you
-
The owner of the site has his own email server located in his office. To get his emails going to him, I needed to set up the MX records to point to his email server, and the ISP I normally use wasn't able to provide this. For his site, I switched to another ISP and got the email working, but now am having problems with the Joomla site (they acknowledge they have a bug related to Plesk and Joomla but don't know when it will be fixed). I don't want to take a chance of messing up his email, and wanted a temporary fix until I better understand the situation. Using Proxy should only effect the site and not email, right? Thanks for the help!
-
Thanks Corbin, Buy using mod_proxy, do you mean using the Proxy flag? RewriteEngine on RewriteRule ^(.*)$ http://www.site2.com/somedirectory/$1 [P NC,L] Or doing something like the following? ProxyRequests Off <Proxy *> Order deny,allow Allow from all </Proxy> ProxyPass / http://www.site2.com/somedirectory ProxyPassReverse / http://www.site2.com/somedirectory As you can probably guess, I have know idea how to use proxy, and would appreciate any guidance. Thanks, Michael
-
Hi, I am having problems with the server hosting my site, and wish to temporarily use another server until the problem has been resolved. I don't want the user to know that they have been redirected, but think they are at the same URL. Also, I can't simply point the domain name to the new server because I must preserve the email going to the original server. For instance, the URL www.site1.com/bla/bla would redirect to www.site2.com/somedirectory/bla/bla, but still display www.site1.com/bla/bla in the users browser. Would using htaccess work? I have tried using the following three .htaccess approaches, but still no success. A request to site1 redirects me to site2 using all three .htaccess approaches, however, the URL in the browser shows site2 and not site1. When I do something similar but make the redirect internal (i.e. don't include the http://), the URL in the browser keeps the original URL. Is it possible to use .htaccess to redirect to another external URL, but keep the original URL in the browsers URL box? If not, is their a non-htaccess way of doing this? THANKS!!! Redirect 302 / http://www.site2.com/somedirectory/ RewriteEngine on RewriteRule ^(.*)$ http://www.site2.com/somedirectory/$1 [NC,L] RewriteEngine on RewriteCond %{HTTP_HOST} ^www\.site1\.com$ [NC] RewriteRule ^(.*)$ http://www.site2.com/somedirectory/$1 [NC,L]