Jump to content

Using php to insert a line break inside javascript


jeff5656

Recommended Posts

Inside a <script type="text/javascript"> I have a function. Insuide that function I am echoing out variables using php code.

 

while ($reqarray=mysql_fetch_assoc($rrr)){

?>errors += checkText(formname, '<?php echo $reqarray['var_name'];?>', '<?php echo $progarray[$progname];?>');<?php


}

 

however, the script does not work because there is no line break after each "errors += checkText..." line. Apparently the script won't work unless everything is on it's own separate line (even though it ends with a semi-colon;).

 

How do I echo out a line break inside the while loop to put each one on a separate line? If I echo a <br/> it doesn't work(it just puts the text <br/> there.

Just put that JS code on it's own line.  Anything outside of PHP tags is used as it is pretty much:

while ($reqarray=mysql_fetch_assoc($rrr)){
?>
errors += checkText(formname, '<?php echo $reqarray['var_name'];?>', '<?php echo $progarray[$progname];?>');
<?php
}

 

You should also use json_encode() for echoing out your PHP variables, that why the are properly escaped for JS output:

while ($reqarray=mysql_fetch_assoc($rrr)){
?>
errors += checkText(formname, <?php echo json_encode($reqarray['var_name']); ?>, <?php echo json_encode($progarray[$progname]);?> );
<?php
}

 

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.