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.

Edited by jeff5656
Link to comment
Share on other sites

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
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.