0

How can I get the following to open a window with the message variable? I'm assuming there is a way to get this to work but so far I've had no luck.

echo "<script>myWindow=window.open('','','width=200,height=100');
    myWindow.document.write("$message");</script>";

given the contents of the $message variable...

$message =  "<html><body><table>
        <tr><td><strong>
            Visitor's Name:</strong> ".$_POST['tester_name']."</td></tr>".
        "<tr><td><strong>
        Visitor's E-mail:</strong> ".$_POST['tester_email']."</td></tr>".
        "<br /><tr><td><strong>Answers:</strong></td></tr>";

        $x = 1;
        $y = 0;
        foreach($fields as $key => $field) {
            if (preg_match("/q./", $field)) {
                if ($field == "q3" or $field == "q9") {
                    $cor = (implode(" or ", $corArray[$y]));
                } else {
                    $cor = $corArray[$y];
                }

                $message = $message.
                "<tr><td><em>$x.</em> (
                <b>Response:</b>".$_POST[$field].
                ")</td><td><b>Correct Answer:</b>".$cor."
                </td></tr>";
                ++$x; ++$y;
            }       
             }              

$message = $message."<tr><td><br /><strong>
    Score:</strong> ".$numCorrect."/10 or ".$perc."
        %</td></tr></table></body></html>";
codingManiac
  • 1,654
  • 4
  • 29
  • 42
  • no luck as in the script was never attached to the page? or the echoed out put is wrong? or the script is written but the pop up never appeared? – user1600124 Sep 28 '13 at 07:04
  • The window won't display when trying to display the variable content in it, but I can display simple text such as myWindow.document.write('hello'); – codingManiac Sep 28 '13 at 07:06
  • Check the actual output the request to see if $message turned out to be empty. If so, i'd actually try putting echo's to each stage of the code to see how $message is changing throughout the code – user1600124 Sep 28 '13 at 07:14

5 Answers5

0

Try :

echo "<script>myWindow=window.open('','','width=200,height=100');myWindow.document.write(\"".$message."\");</script>";

You forgot to escape the " inside echo

trrrrrrm
  • 11,362
  • 25
  • 85
  • 130
  • Still nothing, it works if I try simple text, but when I try to reference the variable It doesn't even show. – codingManiac Sep 28 '13 at 07:05
  • you need to remove the `line-breaks` in your `$message` variable either do this manually or follow this question http://stackoverflow.com/questions/5258543/remove-all-the-line-breaks-from-the-html-source – trrrrrrm Sep 28 '13 at 07:13
  • made sure there were no line breaks and still got nothing. I can do echo $message by itself and I get the right output but it won't work in the open.window – codingManiac Sep 28 '13 at 07:22
0

As pointed by ra_htial, you need to esacpe the quotes. Also, your code won't work, if any of the posted parameters has newline character. This will end up terminating Javascript statement and it will raise error. Make sure the values are properly chopped of for newline character. Also do turn on javascript error reporting in your browser. Additionally you can also view the generated source code from your script, which will help you to find exact statement terminated by new line.

CuriousMind
  • 3,143
  • 3
  • 29
  • 54
0

You can do this work:

  1. Create a php file that contain a function to create your table.
  2. Create a page that have an standard web page width a blank body.

Later, when you open popup window set a variable to second page. Second page call function of first page with argument you receive.

I suggest you use a framework to make your app.

0

You might want to base64_encode the message and pass it via $_GET parameters. On the page receiving the message just use base64_decode to get the original.

rfoo
  • 1,160
  • 1
  • 12
  • 27
0

Here is everything you need to know to convert your code, save this as php page and open in browser reviewing the source to piece it together:

<?
$myPhpCalculations = "php within html example:" . 1234;
$message =  "<p>my string with some quotation marks escaped: \" \" \": " . $myPhpCalculations . "</p>";

echo "<script>myWindow=window.open('','','width=200,height=100');
    myWindow.document.write('" . stripslashes($message) . "');</script>";

?>

Essentially, if you are using quotation marks to hold a string in $message, then any quotations must be escaped within that string. You then append it using stripslashes() function to avoid breaking the html code that is echo'd to the page.

OBV
  • 1,169
  • 1
  • 12
  • 25