Solved

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

Posted on 2016-08-19
4
56 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
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 83

Accepted Solution

by:
Dave Baldwin earned 250 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 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

860 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