iEntry 10th Anniversary Forum Rules Search
WebProWorld
Register FAQ Calendar Mark Forums Read
Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Share Thread: & Tags

Share Thread:

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-09-2006, 10:07 AM
lgoss's Avatar
WebProWorld New Member
 
Join Date: Aug 2006
Location: Lexington, KY
Posts: 21
lgoss RepRank 0
Default Tip: How do I insert javascript in php code?

To embed client side code like HTML or JavaScript into your PHP code, you simply need to use echo/print like below.
<?
echo "<script language=\"JavaScript\">\n";
echo "alert("javascript from php");\n";
echo "</script>";
?>
This is especially helpful for form validation with JavaScript.
Reply With Quote
  #2 (permalink)  
Old 10-09-2006, 04:18 PM
kgun's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: May 2005
Location: Norway
Posts: 5,723
kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10
Default

Advice:

Always start php code with: <?php and not <?

Great example:

The PHP Anthology: Object Oriented PHP Solutions

<?php
class Message {
var $message;
function Message() {}
function setMessage($message)
{
$this->message = $message;
}
function getMessage()
{
return $this->message;
}
}
class PoliteMessage extends Message {
function PoliteMessage()
{
$this->setMessage('How are you today?');
}
}

class TerseMessage extends Message {
function TerseMessage()
{
$this->setMessage('Howzit?');
}
}

class RudeMessage extends Message {
function RudeMessage()
{
$this->setMessage('You look like *%&* today!');
}
}

class MessageReader {
var $messages;
function MessageReader(& $messages)
{
$this->messages = & $messages;
$this->readMessages();
}
function readMessages()
{
foreach ( $this->messages as $message ) {
echo ( $message->getMessage().'
' );
}
}
}

$classNames = array ('PoliteMessage','TerseMessage','RudeMessage');
srand ((float)microtime()*1000000);
shuffle ($classNames);
$messages = array();
for ( $i = 0; $i < 10; $i++ ) {
shuffle($classNames);
$messages[] = new $classNames[0]();
}
$messageReader = new MessageReader($messages);
?>

Note the following:

Blue code:
$classNames = Array of message objects that are children of (extends) the parent class Message.

Red code:
The MerssageReader class takes an array of message objects in its constructor.

This is effective code. May be the same technique can be used on an email parser.
Reply With Quote
  #3 (permalink)  
Old 10-09-2006, 04:25 PM
lgoss's Avatar
WebProWorld New Member
 
Join Date: Aug 2006
Location: Lexington, KY
Posts: 21
lgoss RepRank 0
Default Actually...

On servers with shorthand support enabled you can start a scripting block with <? and end with ?>. Thanks for the advice though....
Reply With Quote
  #4 (permalink)  
Old 10-09-2006, 04:27 PM
kgun's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: May 2005
Location: Norway
Posts: 5,723
kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10
Default

But does it not make the code less portable?
Reply With Quote
  #5 (permalink)  
Old 10-09-2006, 04:40 PM
lgoss's Avatar
WebProWorld New Member
 
Join Date: Aug 2006
Location: Lexington, KY
Posts: 21
lgoss RepRank 0
Default Not necessarily

This option is turned on by default in php.ini via short_open_tag = 1 so unless someone has manually changed it in thier ini file it presents no problem as this is the case back to PHP 3.0. Again, thanks for the advice though....
Reply With Quote
  #6 (permalink)  
Old 10-09-2006, 04:45 PM
kgun's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: May 2005
Location: Norway
Posts: 5,723
kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10
Default

I know.

Problem:

The short tag style causes problem in a document where PHP is mixed with XML.

Example:

<?xml

will mistake an XML instruction with a PHP opening tag.
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 05:04 AM.



Search Engine Optimization by vBSEO 3.3.0