473,699 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I convert old onclick() code to DOM if a PHP variable is involved?

Hi,

I've created a table where the header columns link to an AJAX function
which calls a PHP file and returns content - the purpose is to sort
the table on the heading.

The code snippet is:
<th scope="col"><a href="javascrip t:;" onclick="loadBo okingContent('< ?
php echo $SERVERPAGE_URL . SORT_BY_BOOKING ; ?>','innerhbl') ">Booking</
a></th>

<th scope="col"><a href="javascrip t:;" onclick="loadBo okingContent('< ?
php echo $SERVERPAGE_URL . SORT_BY_VESSEL; ?>','innerhbl') ">Vessel</
a></th>

<th scope="col"><a href="javascrip t:;" onclick="loadBo okingContent('< ?
php echo $SERVERPAGE_URL . SORT_BY_CUTOFF; ?>','innerhbl') ">Cutoff</
a></th>
The $SERVERPAGE_URL variable also contains one parameter, a session ID

I would like to convert this to DOM and store the code in an external
JS file thereby separating the JavaScript code from the HTML.
However, I need to pass the first content of the loadBookingCont ent()
function but my PHP code doesn't work in an external JS file.

I'm relatively new at DOM but there must be a way.

Thanks,
Don

Oct 28 '07 #1
4 3672
On Oct 28, 4:06 am, donpro <donpro-2...@rogers.com wrote:
Hi,

I've created a table where the header columns link to an AJAX function
which calls a PHP file and returns content - the purpose is to sort
the table on the heading.

The code snippet is:
<th scope="col"><a href="javascrip t:;" onclick="loadBo okingContent('< ?
php echo $SERVERPAGE_URL . SORT_BY_BOOKING ; ?>','innerhbl') ">Booking</
a></th>

<th scope="col"><a href="javascrip t:;" onclick="loadBo okingContent('< ?
php echo $SERVERPAGE_URL . SORT_BY_VESSEL; ?>','innerhbl') ">Vessel</
a></th>

<th scope="col"><a href="javascrip t:;" onclick="loadBo okingContent('< ?
php echo $SERVERPAGE_URL . SORT_BY_CUTOFF; ?>','innerhbl') ">Cutoff</
a></th>

The $SERVERPAGE_URL variable also contains one parameter, a session ID

I would like to convert this to DOM and store the code in an external
JS file thereby separating the JavaScript code from the HTML.
However, I need to pass the first content of the loadBookingCont ent()
function but my PHP code doesn't work in an external JS file.

I'm relatively new at DOM but there must be a way.

Thanks,
Don
if your table is one of many in the page then give the table an id
your external js should just spot this id
then cycle through the three column headers (the links)
and for each attach an onclick to the link sending the innerHTML value
of that link as the sort by paramter
you shouldnt need a session id in the url, this suggests an old style
php installation or programming style. also innerhbl is being repeated
so I question whether you need to pass that as a paramter as well.
thus all you do is code for a "toogle" method sending which you want
to toggle, the php script returns html of the new table (including the
headings though it doesnt not need to) and away you go.
thus you dont actually need php in your external js file at all

Oct 28 '07 #2
On Oct 28, 12:06 am, donpro <donpro-2...@rogers.com wrote:
Hi,

I've created a table where the header columns link to an AJAX function
which calls a PHP file and returns content - the purpose is to sort
the table on the heading.

The code snippet is:
<th scope="col"><a href="javascrip t:;" onclick="loadBo okingContent('< ?
php echo $SERVERPAGE_URL . SORT_BY_BOOKING ; ?>','innerhbl') ">Booking</
a></th>
First off, you need to give those links proper href's. Pass the sort
field to the PHP script that generates the page. That script needs to
look for that parameter and sort accordingly. Then users without
script can sort your tables. Even if you don't care about
accessibility, this lays the perfect foundation for your AJAX
enhancement.
<th scope="col"><a href="javascrip t:;" onclick="loadBo okingContent('< ?
php echo $SERVERPAGE_URL . SORT_BY_VESSEL; ?>','innerhbl') ">Vessel</
a></th>

<th scope="col"><a href="javascrip t:;" onclick="loadBo okingContent('< ?
php echo $SERVERPAGE_URL . SORT_BY_CUTOFF; ?>','innerhbl') ">Cutoff</
a></th>

The $SERVERPAGE_URL variable also contains one parameter, a session ID

I would like to convert this to DOM and store the code in an external
JS file thereby separating the JavaScript code from the HTML.
Attach an onclick hander to the body and watch for anchor tags with
href's that contain the sort field parameter. Pass the href to the
loadBookingCont ent function. So that it downloads just the sorted
table (not the entire page), use a header to tell the PHP script that
it is an AJAX call.

Oct 28 '07 #3
donpro wrote:
I've created a table where the header columns link to an AJAX function
which calls a PHP file and returns content - the purpose is to sort
the table on the heading.

The code snippet is:
<th scope="col"><a href="javascrip t:;" onclick="loadBo okingContent('< ?
Are you using XHTML and are you declaring the `scope' attribute in your
internal subset? If not, the `th' element has no `scope' attribute in
Valid HTML. http://validator.w3.org/

The `javascript:;' attribute value is nonsense. You should cancel the
`click' event instead. And it would be a good idea to remove the dependency
on client-side scripting -- you do want people without script support to
book at your store, don't you?
php echo $SERVERPAGE_URL . SORT_BY_BOOKING ; ?>','innerhbl') ">Booking</
a></th>

<th scope="col"><a href="javascrip t:;" onclick="loadBo okingContent('< ?
php echo $SERVERPAGE_URL . SORT_BY_VESSEL; ?>','innerhbl') ">Vessel</
a></th>

<th scope="col"><a href="javascrip t:;" onclick="loadBo okingContent('< ?
php echo $SERVERPAGE_URL . SORT_BY_CUTOFF; ?>','innerhbl') ">Cutoff</
a></th>
The $SERVERPAGE_URL variable also contains one parameter, a session ID

I would like to convert this to DOM and store the code in an external
JS file thereby separating the JavaScript code from the HTML.
However, I need to pass the first content of the loadBookingCont ent()
function but my PHP code doesn't work in an external JS file.

I'm relatively new at DOM [...]
It shows. Your notion of "converting to DOM" is based on the misconception
that you are currently not using the DOM, which in fact you do by employing
intrinsic event handler attributes.

Therefore, my first attempt of refactoring your code would be HTML refactoring:

<th class="col"><a href="<?php
echo preg_replace('/?.*/', '', $_SERVER['PHP_SELF']);
?>?sort=booking " onclick="loadBo okingContent('< ?php
echo $SERVERPAGE_URL . SORT_BY_BOOKING ;
?>', 'innerhbl'); return false">Booking</a></th>

<th class="col"><a href="<?php
echo preg_replace('/?.*/', '', $_SERVER['PHP_SELF']);
?>?sort=vessel " onclick="loadBo okingContent('< ?php
echo $SERVERPAGE_URL . SORT_BY_VESSEL;
?>', 'innerhbl'); return false">Vessel</a></th>

<th class="col"><a <a href="<?php
echo preg_replace('/?.*/', '', $_SERVER['PHP_SELF']);
?>?sort=cutoff " onclick="loadBo okingContent('< ?php
echo $SERVERPAGE_URL . SORT_BY_CUTOFF;
?>', 'innerhbl'); return false">Cutoff</a></th>

Then I would refactor the PHP code:

<?php
$sort_cols = array(
'Booking' =SORT_BY_BOOKIN G,
'Vessel' =SORT_BY_VESSEL ,
'Cutoff' =SORT_BY_CUTOFF );

$me = preg_replace('/?.*/', '', $_SERVER['PHP_SELF']);

foreach ($sort_cols as $key =$val)
{
?>
<th class="col"><a href="<?php
echo "${me}?sort =" . strtolower($key );
?>" onclick="loadBo okingContent('< ?php
echo $SERVERPAGE_URL . $val;
?>', 'innerhbl'); return false"><?php
echo $key;
?></a></th>
<?php
}
?>

And only *then* I would ask myself whether or not it was necessary to
further "separate the JavaScript code from the HTML" and would probably
answer "no". However, if I were to answer "yes", then I would use event
bubbling. And if I would not knew what that was, I would ask Google.
HTH

PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Oct 28 '07 #4
Thomas 'PointedEars' Lahn wrote:
$me = preg_replace('/?.*/', '', $_SERVER['PHP_SELF']);
$me = preg_replace('/\?.*/', '', $_SERVER['PHP_SELF']);

PHP's PCRE-based engine is not that forgiving.
PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Oct 28 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5382
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and calculate days, months, and years. This is not for a college course. It's for my own personal genealogy website. I'm stumped about the code. I'm working on it but not making much progress. Is there any free code available anywhere? I know it...
2
9164
by: Kevin Lyons | last post by:
Hello, Can anyone assist me with what I am trying to do with the following code (six different scenarios to try to make the functionality work correctly)? I want to always (and ONLY) display in the status bar 'Symantec Corporation' whenever anyone mouses over (onMouseOver) my image or link OR when one clicks while holding the left mouse down (onClick) on the same image or link. Upon releasing the mouse (onMouseOut), the
3
5881
by: Byron | last post by:
Hi, Javascript confuses me, so I usually limit myself to Dreamweaver's built-in scripts for stuff like imageswaps. But this time I'm trying to write something very simple myself. I do most of my stuff in ASP and PHP so I'm familiar with server-side programming; for some reason JavaScript syntax trips me up. I want to assign a value to a variable according to an onclick event, and then run an if...then on the variable, and according to...
3
4350
by: geotso | last post by:
Hi I use the following script in order to show/hide a section, and at the same time to change a companion .gif with another: function doExpand(paraNum,arrowNum){ if (paraNum.style.display=="none"){paraNum.style.display="";arrowNum.src="../../images/arrOn.gif"} else {paraNum.style.display="none";arrowNum.src="../../images/arrOff.gif"} }
5
2580
by: moondaddy | last post by:
I have a <a> element in a datagrid which wraps some asp.net labels. this element also has an onclick event which does not fire in netscape 6 (and perhaps other browsers for all I know...). Below is the code for this. the onclick event calls a javascript function which I put an alert in the firt line to tell me if its working. It does work in IE. Any ideas on how to get netcrap... oops, I'm sorry, netscape to fire the onclick event? ...
9
10520
by: Andy Sutorius | last post by:
Hi, I am receiving the error when compiling the project, "cannot implicitly convert type object to string". The error points to this line of code and underlines the dtrRecipient: objMailMessage.To = dtrRecipient; I'm not sure why I am getting this error. Below is the complete code I am working with.
6
7170
by: Nx | last post by:
i've got it all working nicely in firefox, but whenever i test it in IE none of the onclick events are triggered. i'm using an xsl to transform an rss feed into a photogallery. when i try to use setAttribute FF and safari work, but IE stops working when i used addEventListener and attachEvent safari stops working and when i tried .onClick,none of them worked being fairly inexperienced (but learning fast), i figure i'm doing it
7
7478
by: abs | last post by:
Hi everyone. Please, check my test code here: http://skocz.pl/jstest . The trouble is that no matter which span element I click, it alerts '3' and I'm wondering why not '1' for the first span, '2' for second and '3' for third ? Anybody has any idea ? Best regards, ABS
2
2823
by: =?Utf-8?B?Uml0YUc=?= | last post by:
I posted this question in the C# discussion group but am posting it here also since ASP is involved. I'm new to C# and need some help regarding an onClick event not firing. I have a data grid that I add a cell to programatically. The cell is a hyperlink and the onClick is set to _doPostBack. In my code I handle the event but when I click on that added hyperlink nothing happens. Here's my code (I'll just show the relevant stuff).
0
8620
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8920
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8887
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7755
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6536
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2351
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.