user3105372
user3105372

Reputation: 105

Html with javascript in php seems to be conflicting

The code i want to get into a php statement is

<a href="javascript:void();"
   onclick="document.loginfrm.user.value="username";
   document.loginfrm.pass.value="password";
   document.loginfrm.submit();">login
</a>

So what i would normally do is just surround it with an echo and quotation marks: an then replace any quotation marks in the statement with these --> ('), so that's what i did... and for some reason it seems to misinterpret the sentence severely. Here is the code i enter in php.

   echo "<a href='javascript:void();'
   onclick='document.loginfrm.user.value='username';
   document.loginfrm.pass.value='password';
   document.loginfrm.submit();'>". login ."</a>";

And this is how the browser interprets it:

    <a href="javascript:void();
" onclick="document.loginfrm.user.value=" username';="" 
document.loginfrm.pass.value="password" ;="" document.loginfrm.submit();'="">
login</a>

So yes is there any way around displaying html within php that could get around this problem

Upvotes: 1

Views: 89

Answers (4)

Alconja
Alconja

Reputation: 14883

You're not escaping your quotes. Try:

echo "<a href=\"javascript:void();\"
onclick=\"document.loginfrm.user.value='username';
document.loginfrm.pass.value='password';
document.loginfrm.submit();\">". login ."</a>";

which should produce:

<a href="javascript:void();"
   onclick="document.loginfrm.user.value='username';
   document.loginfrm.pass.value='password';
   document.loginfrm.submit();">login
</a>

As you have it now, you're closing the onclick attribute when you hit the quote at the start of the "username" value, which means the browser is interpreting username as another attribute and it just gets more confused from there...

Edit: sorry, fixed the html, rather than the php code...

Upvotes: 1

zzlalani
zzlalani

Reputation: 24384

You should do something like this..

<a href="javascript:void();"
   onclick="document.loginfrm.user.value='username';
   document.loginfrm.pass.value='password';
   document.loginfrm.submit();">login
</a>

and with php it should be

echo '<a href="javascript:void();"
          onclick="document.loginfrm.user.value=\"username\";
          document.loginfrm.pass.value=\"password\";
          document.loginfrm.submit();">
          login
      </a>';

Upvotes: 0

Krish R
Krish R

Reputation: 22741

Can you try this, added \

    echo "<a href=\"javascript:void();\"
       onclick=\"document.loginfrm.user.value='username';
       document.loginfrm.pass.value=password';
       document.loginfrm.submit();\">login </a>";

Upvotes: 1

Konsole
Konsole

Reputation: 3475

You need to escape it properly. Try

echo "<a href=\"javascript:void();\"
   onclick=\"document.loginfrm.user.value='username';
   document.loginfrm.pass.value='password';
   document.loginfrm.submit();\">". $login ."</a>";

Upvotes: 1

Related Questions