Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-08-19
4
Medium Priority
?
67 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 54

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 500 total points
ID: 41763053
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 84

Accepted Solution

by:
Dave Baldwin earned 1000 total points
ID: 41763056
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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 41763075
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
ID: 41764024
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

916 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