Jump to content
Larry Ullman's Book Forums

How Do Things Initialize?


Recommended Posts

I guess I don't understand the relationship between the php and the javascript. This has plagued my mind how this works.

 

 

In the very first example, the form uses this line:

<form action="login.php" method="post" id="loginForm">

 

 

But, below the form is the line:

 

<script src="js/login.js"></script>

 

 

 

I understand that because the script is at the bottom of the html page, it is automatically loaded and the init() function is called. What is confusing is, if the form action="login.php", what action actually calls the validateForm() in the javascript?

 

 

Please help me get beyond this simple but confusing (to me) flow.

Link to comment
Share on other sites

Actually. I found my answer. I had read Chapter 2, but it didn't stick. I carefully read again and now it more clear. Just for reference for anyone else just beginning, and to lock it in my own brain, here are the simplified steps, based on the html and script in CH2.

 

HTML page. At bottom

<script src="js/login.js"></script>. This executes the login.js script.

 

 

 

At bottom of login.js is --> window.onload = init; This tells us which function to execute when it loads the script.

in function init()--> We create a listener var loginForm = document.getElementById('loginForm');

loginForm.onsubmit = validateForm;

 

The validateForm() function does the validate form which returns true if everything is good, if not, it returns false.

 

If the script returns true, then

<form action="login.php" method="post" id="loginForm"> actually executes.

 

 

A key piece of information that I picked up from page 45 is this

To start, note that the only thing different about this form and one that wouldn't be tied to JavaScript is that each element has both a name attribute and an id attribute. The name value will be used when the form data is submitted to the server-side PHP script. The id value will be used by the JavaScript.

 

I also learned an understood that JavaScript is an event language. So an event listener will happen before the call to action="somescript.php".

  • Upvote 1
Link to comment
Share on other sites

The event part is very important. As in every language that has them, events will fire at particular times. You create a listener to catch them. It's the $_POST array in PHP, actionEvents in Java and something other in Javascript. Those details are very important to pick up, because having a solid understatement of things like that is what makes you a good programmer in the end. The devil lies in the details, isn't that the english expression? :)

 

I'm only at about page 45 myself, so I'm a real newbie at JS. Hope I can pick up most of this language in a couple of weeks.

  • Upvote 1
Link to comment
Share on other sites

A minor point, but I wouldn't say that the $_POST array is an event listener in PHP. PHP doesn't have event listeners in the sense of a function that gets called when something happens. This is because PHP isn't a running application that watches for events. PHP is just a script that gets executed when a page is requested, including a form's submission. It's true that PHP responds to that event, but there's no true event listening. (And, in any case, it's definitely not in $_POST, because $_POST is theoretically no different than $_GET, $_COOKIE, or $_SESSION.)

Link to comment
Share on other sites

I know that. The point was finding something comparable in PHP. After a form submission, the super global post array is pretty similar to when an event fires in other languages. When I learned about events and listeners, I'd just wished someone gave me such examples. They are often easier to understand then technical explanations, exactly what makes you my favorite writer for this stuff. ;)

 

You actually often do what I did her in your books, though with explanations. Something along:

 

Hint: The post array are not really an event, they just work similar. If you are interested in the small details, search the web.

 

That being said... Might be a stupid example, but I try. haha.

Link to comment
Share on other sites

My thanks to you for helping and for the nice words on what I do. I definitely try not to over fixate on incidental details in my writing, to be sure. My point in trying to clarify here is that the OP is explicitly confused by the differences between PHP and JavaScript, so I worry that suggesting both languages have a similar event model, when they don't, would only further that confusion.

Link to comment
Share on other sites

 Share

×
×
  • Create New...