How do you use Javascript within echoed PHP code?

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

User avatar
turbolemon
Forum Commoner
Posts: 70
Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK

Re: How do you use Javascript within echoed PHP code?

Post by turbolemon »

Meta refresh in the BODY element? Thats not a good idea, if it even works. Use php: header('Location: http://mysite.com/place/to/go/');. That issues a HTTP redirect header to the client, as long as you haven't sent any input to the browser already!

Are you trying to limit the input to a TinyMCE textarea? That could be your problem, I don't know whether that will actually work..
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How do you use Javascript within echoed PHP code?

Post by Benjamin »

turbolemon wrote:Are you trying to limit the input to a TinyMCE textarea? That could be your problem
Right, the example I tested was not using an js editor.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you use Javascript within echoed PHP code?

Post by simonmlewis »

Doesn't work without it either:

Code: Select all

<textarea name='description' rows='6' cols='40'
             onKeyDown=\"limitText('textentry',80);\"
onKeyUp=\"limitText('textentry',80);\"></textarea>
And yes, am using a meta refresh, which works on many pages here. That isn't causing this problem though. Something bizarre is.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How do you use Javascript within echoed PHP code?

Post by Benjamin »

simonmlewis wrote:That isn't causing this problem though. Something bizarre is.
Internet Explorer?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you use Javascript within echoed PHP code?

Post by simonmlewis »

Haha... wouldn't put it past it - but no. Firefox is the same.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

RESOLVED Re: How do you use Javascript within echoed PHP cod

Post by simonmlewis »

Hi all

It finally worked.

With a combination of the \" and the correct javascript which someone here provided, it does work. I even managed to get a secondary text box to do the same think, and countdown from that one countdown field!

Cheers.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you use Javascript within echoed PHP code?

Post by simonmlewis »

Code: Select all

$result = mysql_query ("SELECT COUNT(id) AS numrows FROM dxadverts 
WHERE userid = '$cookieid' GROUP BY userid") or die (mysql_error());
$numadverts = $row['numrows'];
Hi all

This'll be a silly problem, but I am getting a brick wall in my brain.

I am trying to see how many entries in a table for a particular ID number. And then display it.

This doesn't produce anything!

HELP!!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: How do you use Javascript within echoed PHP code?

Post by jackpf »

Where are you defining $row?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you use Javascript within echoed PHP code?

Post by simonmlewis »

It's defined in "AS numrows".... ie. it should use numrows for $row['numrows'];.

It now has the fetch:

Code: Select all

$result = mysql_query ("SELECT userid, COUNT(*) AS numrows FROM dxadverts 
WHERE userid = '$cookieid' GROUP BY userid") or die (mysql_error());
while ($row = mysql_fetch_object($result)){
$numadverts = $row['numrows'];
if ($numadverts == '3') { echo "You have reached your limit of adverts.  Either edit one of your existing adverts, or delete one and create a new one.";}
[can't believe I forgot that]

Still doesn't show anything, and errors on

Code: Select all

$numadverts = $row['numrows'];
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: How do you use Javascript within echoed PHP code?

Post by jackpf »

If you're using mysql_fetch_object(), shouldn't it be $row->numrows?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Count NumRows, how do you echo the answer?

Post by simonmlewis »

Yes it should..... so it's now saying this:

Code: Select all

$result = mysql_query ("SELECT userid, COUNT(*) AS numrows FROM dxadverts 
WHERE userid = '$cookieid' GROUP BY userid") or die (mysql_error());
while ($row = mysql_fetch_object($result))
  {
  $numadverts = $row->numrows;
  if ($numadverts == '3') { echo "You have reached your limit of adverts at $numadverts adverts.  Either edit one of your existing adverts, or delete one and create a new one.";}
elseif ($numadverts <= '2') 
  {
  echo "$numadverts
  <form method='post' name='myform' action='index.php?page=vehicleadded&menu=a_products' enctype='multipart/form-data'>...........
And the error I get is:
You have reached your limit of adverts at 3 adverts. Either edit one of your existing adverts, or delete one and create a new one.
Warning: mysql_fetch_object(): 9 is not a valid MySQL result resource in /usr/local/psa/home/vhosts/domain.co.uk/httpdocs/includes/advertadd.inc on line 20
Line 20 is "while ($row....".
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: How do you use Javascript within echoed PHP code?

Post by jackpf »

What does

Code: Select all

var_dump($result);
output?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you use Javascript within echoed PHP code?

Post by simonmlewis »

Well, when I remove the free_result code, there is no error, so I don't think I will bother changing anything else.

I know it's "bad code", but it works and there is no error.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply