Solved

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

Posted on 2016-08-19
4
63 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 53

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 110

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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