Solved

How to write a line break in an alert box that's being "echoed" in PHP?

Posted on 2016-08-19
4
41 Views
Last Modified: 2016-08-20
Here's what I've got:

	if($_SESSION['guarantor_session']['PaymentWithin30Days']==0)
			{
				$javascript_message="<script language=\"JavaScript\">
			
				alert(\"Hello<br>How are you?\");
				</script>";
				echo $javascript_message;
			}			

Open in new window


It works exactly the way that I want it to, with the exception that I get this:

alert box
I want to get a legitimate line break and even after trying "\n," I still don't get a break.

Any suggestions?
0
Comment
Question by:brucegust
4 Comments
 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 125 total points
Comment Utility
Your rendered javascript should look like

alert("Hello\nHow are you?");


I think it is cleaner to separate your php and html
	if($_SESSION['guarantor_session']['PaymentWithin30Days']==0)
			{
				echo echo '<div class="test" data-pay="1"></div>'
			}	

Open in new window


And that will render
<div class="test" data-pay="1"></div>

Open in new window

You can then use data-pay=1 or 0 to throw your alert

$(function(){
  var pay = $('div.test).attr('data-pay');
 if (pay  ==1){
     alert("Hello\nHow are you?");
}
});

Open in new window

0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 250 total points
Comment Utility
Use this.  Note the '\\' to escape the '\' for Javascript.
	if($_SESSION['guarantor_session']['PaymentWithin30Days']==0)
			{
				$javascript_message="<script type='text/javascript'>alert('Hello\\\nHow are you?');</script>";
echo $javascript_message;
				echo $javascript_message;
			}			

Open in new window

1
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
Comment Utility
Please see: https://iconoun.com/demo/temp_brucegust.php?q=bruce
Note line 12.
<?php // demo/temp_brucegust.php
/**
 * https://www.experts-exchange.com/questions/28964280/How-can-I-incorporate-a-PHP-SELECT-statement-at-this-point-in-Javascript.html
 * https://www.experts-exchange.com/questions/28964436/How-to-write-a-line-break-in-an-alert-box-that's-being-echoed-in-PHP.html
 */
error_reporting(E_ALL);


// USE PHP TO CREATE VARIABLES FOR OUR HTML
$out = NULL;
$get = !empty($_GET['q']) ? $_GET['q'] : NULL;
if (strtolower($get) == strtolower('Bruce')) $out = '<script>alert("Hello \\nBruce!");</script>';


// CREATE OUR WEB PAGE IN HTML5 FORMAT
$htm = <<<HTML5
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<style type="text/css">
/* STYLE SHEET HERE */
</style>

<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
    /* JQUERY CODE HERE */
});
</script>

<title>HTML5 Page With jQuery in UTF-8 Encoding</title>
</head>
<body>

<noscript>Your browsing experience will be much better with JavaScript enabled!</noscript>

$out

<p>Hello World</p>

</body>
</html>
HTML5;


// RENDER THE WEB PAGE
echo $htm;

Open in new window

Screen Capture
0
 

Author Comment

by:brucegust
Comment Utility
Excellent, guys!

Thanks!

I've got it working, now I'm trying to make it look prettier. I've got that at https://www.experts-exchange.com/questions/28964572/Is-there-a-way-I-can-make-this-alert-a-little-more-pretty.html. Feel free to weigh in.

Rock on!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now