JAVASCRIPT:OK按钮转到另一个.php表单


JAVASCRIPT: OK button proceed to another .php form

我有一个用于保存条目的基本javascript代码。

场景:

我有两个表单,第一个表单[view_netbldg.php]用于我的数据输入,然后当用户单击"ADD"按钮时,它将进入下一个表单[save_netbldg.php]。一切都很好,但我担心的是,当代码检测到两个变量中的任何一个为空时,我正在使用的下面的代码将被执行。

这是我的代码:

if($bldgName == "" || $netName == "")
{
echo "<script type='text/javascript'>'n";
echo "alert('Please complete the fields for Building Name or Network Name');'n";
echo "</script>";
header("location:view_netbdlg.php");
}

*程序的结果是,它不显示警报,而是跳到标题

正确的方法是,如果$bldgName="||$netName="=true**将显示警报,当用户单击确定按钮时,它将进入下一个表单,即"view_netbdlg.php"。我不需要在这里确认消息,因为这只是一条类似的错误消息。

提前谢谢。

您混淆了php和javascript,您可以通过解决此问题

if($bldgName == "" || $netName == "")
{
 echo "<script type='text/javascript'>'n";
 echo "alert('Please complete the fields for Building Name or Network Name');'n";
 echo "window.location='view_netbdlg.php';'n";
 echo "</script>";
}

之所以会发生这种情况,是因为当加载文档时,它会在找到脚本标记时停止。

在您的情况下,当文档的未确定部分已经加载时,您正试图回显该部分中的数据。

这是我的解决方案:

PHP文件(test.PHP):

<?php
    if(!isset($_POST['bldgName']) || !isset($_POST['netName']) {
        die("false"); //returns false if the values are NOT set
    } else {
        die("true"); //returns true if the values are set
    }
?>

在您的文件(file.html)中:

<form method="post" action="test.php" id="formtest">
    <input type="text" name="bldgName" />
    <input type="text" name="netName" />
</form>
<script type="text/javascript">
if(getAjaxResponse(document.getElementById("formtest") == "true") {
    alert("Please complete the fields for Building Name or Network Name");
    window.navigate("view_netbdlg.php");
}
function getAjaxResponse(frm){
var elem = frm.elements;
var params = "";
url = form.action;
for(var i = 0; i < elem.length; i++){
    if (elem[i].tagName == "SELECT"){
        params += elem[i].name + "=" + encodeURIComponent(elem[i].options[elem[i].selectedIndex].value) + "&";
    } else {
        params += elem[i].name + "=" + encodeURIComponent(elem[i].value) + "&";
    }
} 
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST",url,false);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(params);
return xmlhttp.responseText;    
}
</script>

您已经在脚本下编写了代码,但从未调用过该脚本为此,您必须使用函数并手动调用函数。。。

有关函数调用的更多信息,请参阅

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/Call

您可以尝试:

if($bldgName == "" || $netName == ""){
    header('Refresh: 10; URL=your url');#10 second delay
    echo "Please complete the fields for Building Name or Network Name";
    exit;#make sure rest of page doesn't render!
}