Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

print php variable with javascript

Posted on 2006-06-15
9
Medium Priority
?
456 Views
Last Modified: 2012-08-14
Hello boys. I've a question.
I have a php that sends a mail and I wish thast when I click a button, it cleans the page with javascript and prins a php variable.
This is part of my page:

$query_result = mysql_query("SELECT articulo, marca, tipusRoba, hd, referencia, tallas, color, precio, cantidad, SUM(cantidad*precio) AS subtotal FROM ".$session[taula]." group by id");

$filecontents = '<table width=800 border=0 align=center cellpadding=0 cellspacing=1 bgcolor=#990000><tr><td bgcolor=#FFFFFF>Datos del cliente:';
  $filecontents .= '</td></tr></table><table width=800 border=0 align=center cellpadding=0 cellspacing=1 bgcolor=#990000><tr><td bgcolor=#FFFFFF>';
  $filecontents .= '<br><strong>Nombre y apellidos:</strong> '.$nombre.'';
  $filecontents .= '<br><strong>dirección:</strong> '.$direccion.', '.$numero.', '.$piso.' - '.$puerta.'';
  $filecontents .= '<br><strong>Población:</strong> '.$poblacion.' - '.$cp.'';
  $filecontents .= '<br><strong>Província:</strong> '.$provincia.'';
  $filecontents .= '<br><strong>Teléfono:</strong> '.$telefono.' -<strong> E-Mail: </strong>'.$mail.'';
  $filecontents .= '<br><strong>Anotación:</strong><br>'.$nota.'<br><br>';
    $filecontents .= '</td></tr></table><table width=800 border=0 align=center cellpadding=0 cellspacing=1 bgcolor=#990000><tr><td bgcolor=#FFFFFF>PEDIDO:';
  $filecontents .= '</td></tr><table cellpadding=0 cellspacing=1 bgcolor=#990000 width=800 border=0 align=center><tr><td class=camp_edt>articulo</td><td class=camp_edt>marca</td><td class=camp_edt>tipo</td><td class=camp_edt>sexo</td><td class=camp_edt>refer.</td><td class=camp_edt>talla</td><td class=camp_edt>color</td><td class=camp_edt>precio</td><td class=camp_edt>cant.</td><td class=camp_edt>subt.</td>
  </tr><tr>';
while($row = mysql_fetch_row($query_result))
 
  $filecontents .= '<td bgcolor=#FFFFFF>'.implode('</td><td bgcolor=#FFFFFF>', $row).''.'</tr><tr>';
  $filecontents .= '</tr></table>';

 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>FINALIZAR PEDIDO</title>
<script>
function clearMe()
{
  var _s=top;
  var _d=_s.document;
var llista = <?echo $filecontents;?>
  _d.open();
  _d.write("");
  _d.close();
}
</script>

Then, I press a button and calls the function clearMe.
What can I do to print the variable $filecontents when the screen is clear?

Thanks a lot
Jordi
0
Comment
Question by:jbadia
[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
9 Comments
 
LVL 30

Assisted Solution

by:callrs
callrs earned 200 total points
ID: 16909460
<script type="text/javascript">
document.write(<?php print "'".$filecontents."'"; ?>);
</script>

And/or try
<script type="text/javascript">
window.document.body.outerHTML=(<?php print "'".$filecontents."'"; ?>);
</script>
0
 
LVL 30

Expert Comment

by:callrs
ID: 16909476
www.activewidgets.com/javascript.forum.4257.6/javascript-and-php-variables.html   "ActiveWidgets • javascript and php variables • javascript php variable"
0
 

Author Comment

by:jbadia
ID: 16912125
Code goes right, but it doesn't print my variable $filecontents, why? perhaps because is a multiline variable?

Jordi
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 30

Expert Comment

by:callrs
ID: 16916067
I don't think you need function clearMe
Try replacing it with a new function that has the code I gave you, or use the code without putting it in a function.
0
 

Author Comment

by:jbadia
ID: 16916219
yes, as i said, the your code goes right with another variable, but not with the variable I said, $filecontents.
This is how I have now:

<script>
function clearMe()
{
  document.write(<?php print "'".$nombre."'"; ?>);
}
</script>

If I put:
</script>

<script>
function clearMe()
{
  document.write(<?php print "'".$filecontents."'"; ?>);
}
</script>

It don't go. Can You explain me why? Thanks a lot.

Jordi
0
 
LVL 6

Accepted Solution

by:
nitinsawhney earned 800 total points
ID: 16919194
This is because PHP is parsed before the javascript.
If $filecontent is having any newline characters then it will break the javascript.

You should also get a javascript error. Try looking at javascript console if usint firefox else look for the small alert triangle in IE.

A possible workaround for this could be that you replace all the new lines with <br />.

<script>
function clearMe()
{
<?php
  $replace = array("\n\r","\n"); // New line for windows is \n\r and for linux is \n
  $filecontents = string_replace($replace, '<br />' , $filecontents);
?>
  document.write(<?php print "'".$filecontents."'"; ?>);
}
</script>

Hope this helps :-)
0
 
LVL 2

Expert Comment

by:Mr_Lenehan
ID: 16919903
Use the javascript urlencode to code up the $fileconents, then urldecode to print. this will sort out any newline problems.
0
 

Author Comment

by:jbadia
ID: 16921692
Thank you guys.
50 to answer in part my question and 200 to solve the problem.

jordi
0
 
LVL 30

Expert Comment

by:callrs
ID: 16921788
'<br>', not "\n" was in the code you posted here, so it escaped my attention 'til last night.

Good job nitinsawhney!
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

688 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