2013-04-10 166 views
0

當我做這個,我不斷收到此錯誤:解析錯誤:語法錯誤,意外'(T_ENCAPSED_AND_WHITESPACE),預計標識符(T_STRING)或可變

<?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> 
"; 
?> 

我想在PHP中使用JavaScript通過將我的JavaScript代碼放入我的echo語句中。我無法弄清楚我做錯了什麼。任何幫助,將不勝感激。

+1

'$ _POST ['mname'] =「或1 = 1」'。繁榮! – elclanrs 2013-04-10 03:47:50

+1

我喜歡類似問題的代表點 – zerkms 2013-04-10 03:49:14

+0

@elclanrs現在他們可以看到所有的電影xD – Musa 2013-04-10 03:54:04

回答

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()\">'); 
+0

我很確定無論'jsvar = {$ row ['name']} {$ row ['year']} {$ row ['genre']};'會導致一個js錯誤。 – Musa 2013-04-10 03:53:15

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

應該

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

並參閱下面的@Dipesh Parmar的答案 – 2013-04-10 03:49:51

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> 
相關問題