Jump to content

Encode php for html? (Whitespace breaks code)


Chotor

Recommended Posts

Hi.

 

This is probably an easy request, but I never received my "PHP and MySQL programming" book from the bookstore, so I'm doing it by trial and error. :P

 

This is the code I'm trying to send back to the html page.

return " <a href=javascript:myfunction('".$k."'); style=\"font-size: ".$s."%; color:#99cfec; cursor:pointer \">".$k."</a> ";

However, if I have a $k with spaces, the value will break off at the first whitespace.

For instance, having $k="John Wayne" will output

javascript:myfunction('John

(The strange thing is that font-size, color, cursor, and the second $k is output correctly)

I'm guessing it has something to do with htmlentities and/or encoding text for html, but I'm not sure.

I don't know if this helps, but here is a bit of code that I wrote for ya that can show one way of passing vars into javascript, there are other ways though, you can pass them through the function itself...

 

<script language="javascript" type="text/javascript">
<!--
function testFunction(form) {
var $testVariable = form.testVariable.value;
alert($testVariable);
return false; 
}
// -->
</script>

<?
$k="John Wayne"
?>
<form name="test_form" action="" method="POST" onSubmit="return testFunction(this)">
<input type="text" name="testVariable" value="John Wayne">
<input type="submit" name="submit_button" value="Test Me">
</form>

First of all, I'd use printf() or sprintf() in this situation because it's neater. 

 

But other than that...it just cuts off the rest of the code, or just the JS function input?

It cuts off the JS function.

The statusbar in FireFox (showing the current target) shows only that.

I think the function and html is properly sent from php and recieved to html. It's just the browser's rendering that breaks things..

 

I don't know if this helps, but here is a bit of code that I wrote for ya that can show one way of passing vars into javascript, there are other ways though, you can pass them through the function itself...

Thanks. I'll look at that tomorrow.

Here is a part of the response sent by PHP and recieved by the browser:

<a href=javascript:MYFUNCTION('Ron Perlman'); style="font-size: 100%; color:#99cfec; cursor:pointer ">Ron Perlman</a>  <a href=javascript:MYFUNCTION('Rosemary Harris'); style="font-size: 100%; color:#99cfec; cursor:pointer ">Rosemary Harris</a>  <a href=javascript:MYFUNCTION('Rupert Evans'); style="font-size: 100%; color:#99cfec; cursor:pointer ">Rupert Evans</a>  

Note that the html is intact.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.