我正在使用PHP和JavaScript,並且在處理JavaScript中的confirm()
函數時遇到了問題。Javascript確認()函數無法正確傳遞參數
說我有一個頁面add.php,首先我收到一些從另一個頁面傳遞的參數,我檢查它們是否有效。如果是的話,我只是將數據插入到數據庫並返回到另一個頁面,如果它們無效,彈出一個confirm()
窗口,讓用戶選擇是否繼續。如果用戶仍選擇繼續,我希望頁面重新加載所有參數再次發送。但問題是,我第二次加載add.php時無法獲取參數。
此前我沒有使用window.onload
函數和confirm()
彈出,但一個< a href>
鏈接,而不是一切工作正常(請參閱附加的代碼在最後)。但是,當我嘗試使用下面的代碼,相同的URL停止工作
echo "<script type=\"text/javascript\">";
echo "window.onload = function() {
var v = confirm(\"$name is not alive, do you want to add it into system?\");
if (v) {
window.location.href= \"add.php?type=room&name=$name&area\"
+ \"=$area&description=$description&\"
+ \"capacity=$capacity&confirm=Y\";
} else {
window.location.href= \"admin.php?area=$area\";
}
}";
echo "</script>";
以下是以前的版本,而不是使用window.onload()
,我用< a href="..." />
鏈接,一切都在那個時候工作得很好。 get_form_var是functions.inc中的一個函數,它使用$_GET
數組獲取參數。
<?php
require_once "functions.inc";
// Get non-standard form variables
$name = get_form_var('name', 'string');
$description = get_form_var('description', 'string');
$capacity = get_form_var('capacity', 'string');
$type = get_form_var('type', 'string');
$confirm = get_form_var('confirm','string');
$error = '';
// First of all check that we've got an area or room name
if (!isset($name) || ($name === ''))
{
$error = "empty_name";
$returl = "admin.php?area=$area"
. (!empty($error) ? "&error=$error" : "");
header("Location: $returl");
}
// we need to do different things depending on if its a room
// or an area
elseif ($type == "area")
{
$area = mrbsAddArea($name, $error);
$returl = "admin.php?area=$area"
. (!empty($error) ? "&error=$error" : "");
header("Location: $returl");
}
elseif ($type == "room")
{
if (isset($confirm)){
$dca_osi = getOsiVersion($name);
$room = mrbsAddRoom(
$name,
$area,
$error,
$description,
$capacity,
$dca_osi,
1
);
$returl = "admin.php?area=$area"
. (!empty($error) ? "&error=$error" : "");
header("Location:$returl");
}
else {
$dca_status= pingAddress($name);
$dca_osi = getOsiVersion($name);
if($dca_status == 0){
$room = mrbsAddRoom(
$name,
$area,
$error,
$description,
$capacity,
$dca_osi,
0
);
$returl = "admin.php?area=$area"
. (!empty($error) ? "&error=$error" : "");
header("Location:$returl");
}
else {
print_header(
$day,
$month,
$year,
$area,
isset($room) ? $room : ""
);
echo "<div id=\"del_room_confirm\">\n";
echo "<p>\n";
echo "$name is not alive, are you sure to add it into system?";
echo "\n</p>\n";
echo "<div id=\"del_room_confirm_links\">\n";
echo "<a href=\"add.php?type=room&name"
. "=$name&area=$area&description"
. "=$description&capacity=$capacity&confirm"
. "=Y\"><span id=\"del_yes\">"
. get_vocab("YES") . "!</span></a>\n";
echo "<a href=\"admin.php?area=$area\"><span id=\"del_no\">"
. get_vocab("NO") . "!</span></a>\n";
echo "</div>\n";
echo "</div>\n";
}
}
}
function pingAddress($host)
{
$pingresult = exec("/bin/ping -c 1 $host", $outcome, $status);
if ($status==0) {
return $status;
}
else {
return 1;
}
}
function getOsiVersion($host)
{
$community = 'public';
$oid = '.1.3.6.1.4.1.1139.23.1.1.2.4';
$sysdesc = exec("snmpwalk -v 2c -c $community $host $oid");
$start = strpos($sysdesc, '"');
if ($start!==false) {
$sysdesc = substr($sysdesc, $start+1,$sysdesc.length-1);
return $sysdesc;
}
else {
return "not available";
}
}
該Javascript在哪些文件? add.php? – 2013-02-25 23:33:12
我注意到原始版本和JS版本之間的一個區別是,在JS版本中,您沒有傳遞confirm = Y作爲URL的一部分。我不確定這是否是問題,雖然... – 2013-02-26 00:26:27
雖然這可能是一個錯誤,但無論如何都需要糾正,否則它會顯示您的頁面會一直重複使用確認對話框重新加載。 – 2013-02-26 00:28:17