Forum Moderators: open

Message Too Old, No Replies

What to build with js

content

         

Orangutang

1:36 pm on Aug 28, 2010 (gmt 0)

10+ Year Member



Hi,

I'll try to explain as best as possible and hopefully it will make sense.

I've been looking into ways to build the content part of my site. The site uses html, css an php and uses absolute divs for header, footer and left nav pane, content scrolling in center.

The content part of the site needs to display text, forms, tables, images, feeds, php, mysql and is ok as is.

I'm looking at improving the performance by implementing js.
I understand I can use js for form validation, pop ups, alert windows, displaying dates, redirects, and calling different style sheets for different devices.

Question 1:
If I use js for form validation do I still need to validate it with php as well ?

Question 2:
Can js display all the content I need as outlined above in bold?

Question 3:
Can js replace the following bold php. Can it use the $result variable from the sql query and manipluate it the same as php does but with a js while statement and document.write instead of echo ?

if (! isset($_SESSION['user']) or (isset($_SESSION['user']) and ! ($_SESSION['user'] > 0)))
{
$query = "SELECT * FROM `quotes` WHERE `username` = '".$_SESSION['user']."' ";
$result = mysql_query ($query)or die (mysql_error());
while ($row = mysql_fetch_array($result)) // Can javascript do this ?
{?>
<table width='' border='0' bgcolor="#F3D673">
<tr>
<td align='left' valign='top' width='140'><strong>Quote ID:</strong></td>
<td align='left' valign='top' width='350'><b><?php echo $row['quoteid'];?></b></td></tr>

Question 4:
If I can is there any point, I use sessions and php needs to refresh the page anyway and the sql statement is the time consumer anyway!

Summary:

I use this js to output a single line change but cannot get it to work with forms or tables or returned table data after php manupulation ?

<div id="DivExample">
<a href="#self" onclick="document.getElementById('DivExample').innerHTML = 'This is the output of the javascript'; ">
This is an example of javascript</a>
</div>

I've tried lightboxes but again have problems with the light box not scrolling and I'm not sure if using a lightbox is correct anyway. I've also looked into ifames but don't know if thats correct.

Conclusion

I suppose I'm trying to define what language is appropriate for what functionality ? I'd like to use js as much as possible because client side is quicker but I'm strugglng to find where it is appropriate and where it isn't.

Any help would be much appreciated, I've already looked into loads of methods but still don't have a definitive answer.

bhukkel

1:46 pm on Aug 28, 2010 (gmt 0)

10+ Year Member



The answer for question 1: yes you must also validate with php. Because a hacker can post directly to your website and bypass the javascript validation.

For the other questions i think you must see the difference between php (server side) and javacript (client side). There are things like executing sql queries that cannot be done directly with javascript.

Orangutang

2:08 pm on Aug 28, 2010 (gmt 0)

10+ Year Member



Thanks bhukkel,

Most of my pages use sql queries so that sorts that. :-)

Also why would anyone ever write form validation in js? Why do both, why not just php? Is there another method of protection from hackers if you just use js?

bhukkel

3:35 pm on Aug 28, 2010 (gmt 0)

10+ Year Member



Validation with JS can be user friendly because the page isnt sent to the website before there is valid input.

You can use PHP caching like APC to speed up queries.

rocknbil

6:08 pm on Aug 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also why would anyone ever write form validation in js?


Right, it is to enhance the user experience, as all Javascript should be. Build the app, then enhance the user experience with Javascript. Too many apps are entirely Javascript dependent. In a way, this kind of defines the answer to your question. Don't remove solid back end functions and move them to Javascript "just because you can." Apply your Javascript in such a way that it "takes over" if present, and if not, it still all works.