Page 1 of 1
Script structure
Posted: Thu Dec 10, 2009 10:06 pm
by washingtonmike
If I have an object, say a text area, and want to write an event handler routine in PHP does that block of php code belong inside the form, outside the form, can it reside at the top of the page coding (prior to the header, etc.)?
I'm a little confused how php code is handled/processed with the html, how handler code like this one is processed, etc.. Thanks, Michael
Re: Script structure
Posted: Thu Dec 10, 2009 10:16 pm
by JAB Creations
You can directly run PHP within HTML...
Code: Select all
<form><fieldset> <textarea><?php echo 'Hi mom!';?></textarea> </fieldset></form>
You could also include a functions file (that is nothing more then functions) at any point and call each function to construct various parts of the page (headers, content, etc) and then simply call a function from within the textarea.
Event handler + PHP...what? Events happen in JavaScript. The textarea is called an
element, it's not an object. Using correct terminology is
very important when trying to ask a question.
When you ask a question I highly recommend giving people an example of what the final output would be
like or how it
behaves (subjective to your goal) to minimize confusion.
Re: Script structure
Posted: Fri Dec 11, 2009 8:05 am
by washingtonmike
Thank you and DO please correct my syntax if I'm referring to something incorrectly, i.e. "element", for example. I have several questions as a newbie.
A text area is properly referred to as a "form element", correct?
When I click on a "submit" button (another form element), for instance, does that action then create an "event"?
I will write php code to examine the contents input into the text area and write to a file. This can only be done after the submit button has been clicked so is it proper to refer to that block of php code as an "event handler"?
I will write a php function to do the file processing and call it in the form area ("block"?). Can I call this function using the "onclick="?
Thanks again for your patience. Michael
Re: Script structure
Posted: Fri Dec 11, 2009 12:58 pm
by rhecker
"Object" has a special meaning in some computer languages, including in PHP. It's more accurate to say: "I have this textarea thingy..."
You can put the processing PHP code anywhere on the code page, but often there are reasons why you need to put it in a particular place in the code sequence, for example because of the sequence in which variable thingies are acted upon. You don't need to concern yourself with the "onclick" even in PHP. Below is the most fundamental of forms with PHP processing. The PHP code could just as well have been put at the bottom.
Code: Select all
<?php
if ($_POST[submit]){
$the_name=$_POST[the_name];
echo "You entered $the_name";
}
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST"/>
Name: <input name="the_name" type="text" />
<input name="submit" type="submit" value="PROCESS THINGY" />
</form>
Re: Script structure
Posted: Fri Dec 11, 2009 5:55 pm
by JAB Creations
A couple things...
First try to use single quotes instead of double quotes in PHP as it's a small bit of server load you'll save, the main exception being MySQL queries as it will interpret $variables literally as the strings they contain then as strings themselves.
Also don't use $_SERVER['PHP_SELF'] on forms as they can conflict with form elements and the base element.
rhecker posted some good basic good to work with. I'll try to cover the language a bit more.
Elements have parent/child relationships in what is called the DOM.
I highly recommend learning a database language such as MySQL though unfortunately there is a political concern at the moment with Oracle attempting to consume Sun (Sun develops MySQL and Oracle is an evil monopolistic corporation like Microsoft so whole barrel of barbed monkeys there). Still Learning MySQL would get you on a good path to understanding SQL in general (relational MySQL using JOIN's are paramount to effective programming). I'm not sure why you want to write to a file, a guestbook in example?
JavaScript has events, when user does X execute function y. PHP executes according to what you program where as JavaScript can execute according to you or the user.
PHP has classes (so does JavaScript though they are much less important) which you can figure out what something is and continually refer to the same object versus using procedural code to determine the exact same thing over and over; using a class in PHP is called object oriented programming.