OnClick help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jay87
Forum Commoner
Posts: 61
Joined: Thu Jan 07, 2010 5:22 am

OnClick help

Post by Jay87 »

I want to add an onclick command to the following code:

Code: Select all

<td class=smooth" . $stripe .">" . substr($item['description'],0,65) . "</td><td class=smooth" . $stripe .">" . $item['logged_fmt'] . "</td><td class=smooth" . $stripe .">" . $days . "d&nbsp;" . $hours . "h&nbsp;" . $mins . "m&nbsp;</td>
everytime i copy and paste:

Code: Select all

onclick=\"document.location='viewcall.php?id=
into the <td but it just breaks the page....

Any ideas?
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: OnClick help

Post by cpetercarter »

Surely your code for 'onclick' is not complete. It should be something like:

Code: Select all

onclick=\"document.location='viewcall.php?id=[some-id]'\"
.

But why are you using Javascript here? Cant you simply place a normal link

Code: Select all

<a href="viewcall.php?id=[some-id]">Your item description</a>
between <td> and </td>?
Jay87
Forum Commoner
Posts: 61
Joined: Thu Jan 07, 2010 5:22 am

Re: OnClick help

Post by Jay87 »

cpetercarter wrote:Surely your code for 'onclick' is not complete. It should be something like:

Code: Select all

onclick=\"document.location='viewcall.php?id=[some-id]'\"
.

Your right it's: onclick=\"document.location='viewcall.php?id=" . $item['id'] . "&pfx=" . $item['id_prefix'];

But why are you using Javascript here? Cant you simply place a normal link

Code: Select all

<a href="viewcall.php?id=[some-id]">Your item description</a>
between <td> and </td>?
Because each row has a unique description i.e. call 0001 description could be: 'not receiving emails' and then call 0002 description could be 'can we set up a new user'
I tried:

Code: Select all

<td class=smooth onclick=\"document.location='viewcall.php?id=" . $item['id'] . "&pfx=" . $item['id_prefix']; . $stripe .">" . substr($item['description'],0,75) . "</td>
but this doesn't work.
Jay87
Forum Commoner
Posts: 61
Joined: Thu Jan 07, 2010 5:22 am

Re: OnClick help

Post by Jay87 »

Code: Select all

<td class=smooth onclick=\"document.location='viewcall.php?id=" . $item['id'] . "&pfx=" . $item['id_prefix']; $stripe .">" . substr($item['description'],0,75) . "</td>
the onclick works but has totally screwed up the formatting of the table, syntax error?
Jay87
Forum Commoner
Posts: 61
Joined: Thu Jan 07, 2010 5:22 am

Re: OnClick help

Post by Jay87 »

This applies the onclick to the whole row:

Code: Select all

<tr  style=\"color:green;\" onclick=\"document.location='viewcall.php?id=" . $item['id'] . "&pfx=" . $item['id_prefix']; if($HTTP_SERVER_VARS['QUERY_STRING']){ echo "&";} echo $HTTP_SERVER_VARS['QUERY_STRING'] . "';\">
I want it just to apply on the:

Code: Select all

<td class=smooth" . $stripe .">" . substr($item['description'],0,75) . "</td>
but can't work out how to add the onclick bit to the above code!

:banghead: :banghead: :banghead:
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: OnClick help

Post by cpetercarter »

You need to sit down and carefully work through the correct syntax for the 'onclick' bit. The 'onclick' code needs to be surrounded by (escaped) double quotation marks, and the bit following 'window.location=' needs to be in single quotation marks. So the correct code should I think be:

Code: Select all

echo "<td onclick=\"document.location='viewcall.php?id=" . $item['id'] . "&pfx=" . $item['id_prefix'] . "'\">" . substr($item['description'],0,75) . "</td>";
If you are still stuck, have a look at the source code for your web page in your browser. This shows the html that your code is outputting. It is often obvious from that where you have gone wrong, where you have omitted a set of quotation marks etc.

I still do not understand your problem with using a normal html link. Why will this not do what you want?

Code: Select all

echo "<td><a href=\"viewcall.php?id=" . $item['id'] . "&pfx=" . $item['id_prefix'] . "\">" . substr($item['description'],0,75) . "</a></td>";
Post Reply