2014-10-30 120 views
-2

我做了一個網頁,從一個字符串中獲取他的數據從一個可視化的基本項目,並將其發送到MySQL。現在它已經運行他的PHP後關閉瀏覽器(Internet Explorer)。現在我找到了一種方法來關閉它,但沒有「腳本試圖關閉瀏覽器」Promt,但它太快並且數據永遠不會到達數據庫。我希望它等待(最好是直到PHP腳本完成,但如果不可能,那麼幾秒鐘)使javascript等待php

這裏是我的PHP:提前

<!DOCTYPE html> 
<html> 
<body> 

<?php 

//variabelen terughalen 
$user = $_GET['userw']; 
$ip = $_GET['ipw']; 
$pc = $_GET['laptopw']; 
$keuze = $_GET['keuzew']; 

echo "$user <br> $ip <br> $pc <br> $keuze <br>"; 

//Serverside info 
$servername = "172.17.80.153"; 
$username = "connect"; 
$password = "123456"; 
$dbname = "vbtest"; 

// Connectie maken 
$conn = new mysqli($servername, $username, $password, $dbname); 

// connectie testen 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 
echo "Connected successfully <br>"; 

//regels voor de lengte van variabelen 
if (strlen($user) <> 4) { echo "geen veranderingen toegestaan"; 
die ;} 
    if (strlen($ip) > 14) { echo "geen veranderingen toegestaan"; 
die ;} 
    if (strlen($keuze) > 3) { echo "geen veranderingen toegestaan"; 
die ;} 
    if (strlen($keuze) < 2) { echo "geen veranderingen toegestaan"; 
die ;} 

//Entry toevoegen aan de MySQL 
if ($keuze == 'ja') { 
$sql = ("INSERT INTO testtbl(Gebruikersnaam, Computernaam, Ipaddress, Keuze) VALUES('".$user."', '".$pc."', '".$ip."', '".$keuze."')"); 
} 
else 
{ 
$sql = ("INSERT INTO testntbl(Gebruikersnaam, Computernaam, Ipaddress, Keuze) VALUES('".$user."', '".$pc."', '".$ip."', '".$keuze."')"); 
$sql2 = ("INSERT INTO testtbl(Gebruikersnaam, Computernaam, Ipaddress, Keuze) VALUES('".$user."', '".$pc."', '".$ip."', '".$keuze."')"); 

if (mysqli_query($conn, $sql2)) { 
    echo "<br>New second record created successfully"; 
} else { 
    echo "Error: " . $sql2 . "<br>" . mysqli_error($conn); 
} 
} 

if (mysqli_query($conn, $sql)) { 
    echo "New record created successfully"; 
} else { 
    echo "Error: " . $sql . "<br>" . mysqli_error($conn); 
} 

?> 


<script type="text/javascript"> 
    window.open('javascript:window.open("", "_self", "");window.close();', '_self'); 
</script> 

</body> 
</html> 

感謝

+1

你在你的代碼都SQL注入和XSS漏洞。 – Boann 2014-10-30 14:27:03

+0

一旦PHP完成,您將不得不返回客戶端。 – 2014-10-30 14:27:05

+0

JavaScript已經「等待」了PHP,因爲在上面的代碼完成之前,PHP將不會回顯JavaScript,因此不能成爲數據庫插入不起作用的原因。 – Boann 2014-10-30 14:30:38

回答

-1

PHP運行就像我們讀文本文檔一樣,從左到右,從上到下。這意味着您的PHP腳本會在JavaScript重定向之前執行。您的PHP代碼中可能存在錯誤,並且由於JavaScript重定向而無法看到它。把die();在php結束標記之前,修復php代碼並移除die();

例子:

//this is a piece of your code 
if (mysqli_query($conn, $sql)) { 
    echo "New record created successfully"; 
} else { 
    echo "Error: " . $sql . "<br>" . mysqli_error($conn); 
} 
die(); //This is added to your code 
?> 


<script type="text/javascript"> 
    window.open('javascript:window.open("", "_self", "");window.close();', '_self'); 
</script> 

</body> 
</html> 
+0

,這與舊的情況下的相同字符串的數據在URL中填寫變量? – 2014-10-30 14:52:51

+0

沒有提到獲取,更改ajax請求的帖子以獲取並放入請求 – 2014-10-30 14:55:47

+0

的數據字符串,它爲我做了,感謝您的幫助 – 2014-10-30 15:00:29