解析错误:语法错误,意外''(T_ENCAPSED_AND_WHITESPACE),期待标识符(T_STRING)或变量

时间:2013-04-10 03:46:04

标签: php javascript

执行此操作时,我一直收到此错误:

<?php
$info = $_POST['mname'];
$info = ucwords($info);

// Make a MySQL Connection
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("javadatest") or die(mysql_error());  

// Get a specific result from the "example" table
$result = mysql_query("SELECT * FROM movieList
WHERE name = '$info'") or die(mysql_error());  

// get the first (and hopefully only) entry from the result
$row = mysql_fetch_array( $result );
// Print out the contents of each row into a table 
//echo $row['name']." ".$row['year']." ".$row['genre'];
echo "
  <script language=javascript>
  var jsvar;
  jsvar = <?php echo $row['name'], $row['year'], $row['genre'];?>
  function buy() {
    window.location = \"https://www.paypal.com\";   
    alert(\"Thanks for shopping at Movie Store\");     
  }
  var myWindow = window.open('', '', 'width = 300, height = 300);
  myWindow.document.write(jsvar);
  myWindow.document.write('<body>'); 

  myWindow.document.write('<input type="button" value="Buy" onclick=/"buy()/">');

  myWindow.document.write('</body>');

 //myWindow.buy = buy;
 </script>
";
?>

我正在尝试通过将我的javascript代码放在我的echo语句中来在php文件中使用javascript。我无法弄清楚我做错了什么。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

您不需要写这一行

jsvar = <?php echo $row['name'], $row['year'], $row['genre'];?>

只需使用

jsvar = {$row['name']} {$row['year']} {$row['genre']};

因为你已经在PHP中了。

另外

<强>替换

myWindow.document.write('<input type="button" value="Buy" onclick=/"buy()/">');

<强>与

myWindow.document.write('<input type=\"button\" value=\"Buy\" onclick=\"buy()\">');

答案 1 :(得分:0)

myWindow.document.write('<input type="button" value="Buy" onclick=/"buy()/">');

应该是

myWindow.document.write(\"<input type='button' value='Buy' onclick='buy()'>\");

答案 2 :(得分:0)

    $row = mysql_fetch_array( $result );
    // Print out the contents of each row into a table 
    //echo $row['name']." ".$row['year']." ".$row['genre'];
    ?>
      <script language=javascript>
      var jsvar;
      jsvar = <?php echo json_encode($row['name']), json_encode($row['year']), json_encode($row['genre']);?>
      function buy() {
        window.location = \"https://www.paypal.com\";   
        alert(\"Thanks for shopping at Movie Store\");     
      }
...
</script>