2

我正在使用 PHP 和 JavaScript,在处理confirm()JavaScript 中的函数时遇到了问题。

假设我有一个页面 add.php,首先我接收到从另一个页面传递的一些参数,然后检查它们是否有效。如果是,我只是将数据插入db并返回另一个页面,如果它们无效,则会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&amp;name=$name&amp;area\"
            + \"=$area&amp;description=$description&amp;\"
            + \"capacity=$capacity&amp;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&amp;name"
                     . "=$name&amp;area=$area&amp;description"
                     . "=$description&amp;capacity=$capacity&amp;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";
        }
    }
4

2 回答 2

1

我已经解决了这个问题,只需使用“&”而不是“&” 在 url 链接中...现在可以正常工作了...

于 2013-02-26T01:53:37.050 回答
0

你试试 location.reload() javascript 调用?

于 2013-02-25T23:25:23.160 回答