(PHP&JavaScript)a href添加按钮赢得'我不适用于Mozilla,但适用于IE和谷歌

(PHP&JavaScript) a href add button won't work on Mozilla but works on IE and Google

本文关键字:适用于 Mozilla 不适用 IE 谷歌 JavaScript amp PHP href 按钮 添加      更新时间:2023-09-30

我真的不确定添加按钮是否是问题的原因,因为它在其他浏览器上运行得很好。我希望你能在某些方面帮助我。非常感谢。顺便说一句,这里有完整的.php代码。

<html>
<head>
<LINK REL=StyleSheet HREF="addOfficerStyle.css" TYPE="text/css" MEDIA=screen>
<script type="text/javascript">
    function validateForm(action)
        {
            var lname=document.forms["validation"] ["lname"].value;
            var fname=document.forms["validation"] ["fname"].value;
            var mname=document.forms["validation"] ["mname"].value;
            var address=document.forms["validation"] ["address"].value;
            var contact=document.forms["validation"] ["contact"].value;
                if (lname==null || lname=="" || fname==null || fname=="" || mname==null || mname=="" || address==null || address=="" || contact==null || contact=="")
                    {
                    alert("Fill all required fields");
                    return false;
                    }
                else{
                    form = document.getElementById('userLocation');
                    form.action = action;
                    form.submit();
                    }
        }

    function numeric(e)
        {
            var unicode=e.charCode ? e.charCode : e.keyCode;
                if (unicode==8 || unicode==9 || (unicode >=48 && unicode <=57))
                    {
                    return true;
                    }
                else
                    {
                    return false;
                    }
        }
    function inputLimiter(e,allow)
        {
            var AllowableCharacters = '';
                if (allow == 'Letters'){AllowableCharacters=' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';}
            var k = document.all?parseInt(e.keyCode): parseInt(e.which);
                if (k!=13 && k!=8 && k!=0)
                    {
                        if ((e.ctrlKey==false) && (e.altKey==false))
                            {
                                return (AllowableCharacters.indexOf(String.fromCharCode(k))!=-1);
                            }
                        else
                            {
                                return true;
                            }
                    } 
                else
                    {
                        return true;
                    }
        } 
</script>
</head>
<body onunload="opener.location=('PromoOfficer.php')">
<br><br>
<form  id="userLocation" name="validation" method="post" onsubmit="return validateForm()">
    <div id="tableAlign">
     <table>
        <tr>
            <td><b>ADD: Promo Officer</td></b>
        </tr>
        <tr> 
              <td>Last name:</td>    <td><input type="text" name="lname" maxlength="20" onkeypress="return inputLimiter(event,'Letters')" /></td>
        </tr>                
        <tr>
              <td>First name:</td>   <td><input type="text" name="fname" maxlength="20" onkeypress="return inputLimiter(event,'Letters')" /></td>
        </tr>     
        <tr>    
              <td>Middle name:</td>  <td><input type="text" name="mname" maxlength="20" onkeypress="return inputLimiter(event,'Letters')" /></td>
        </tr>
        <tr>
              <td>Address:</td>     <td><input type="text" name="address" maxlength="50" /></td>
        </tr>
        <tr>
              <td>Contact:</td>      <td><input type="text" name="contact" maxlength="11" onkeypress="return numeric(event);" /></td>
        </tr>
        <tr>
              <td>User Type:</td>    <td><input type name = "usertype" readonly = "true" value = "Promo Officer"></td>
        </tr>
    </table>
    </div>
<div id="addBtn">   
        <a href="#" onclick="javascript: validateForm(action);return false;"><img src="images/add.png" height="27" width="60"></a>
</div>      
<!-- PHP -->
<?
    include('global.php');

    if(isset($_REQUEST['salesID']))
        $pID = $_REQUEST['salesID'];
    else
        $pID = "";
    if(isset($_REQUEST['lname']))
        $lname = $_REQUEST['lname'];
    else
        $lname = "";
    if(isset($_REQUEST['fname']))
        $fname = $_REQUEST['fname'];
    if(isset($_REQUEST['mname']))
        $mname = $_REQUEST['mname'];
    if(isset($_REQUEST['address']))
        $address = $_REQUEST['address'];
    if(isset($_REQUEST['contact']))
        $contact = $_REQUEST['contact'];
    if(isset($_REQUEST['usertype']))
        $usertype = $_REQUEST['usertype'];
            if($_POST)
                {
                    $query = "INSERT INTO promoofficerform SET ";
                    $query = $query."LastName='".$lname."', ";
                    $query = $query."FirstName='".$fname."', ";
                    $query = $query."MiddleName='".$mname."', ";
                    $query = $query."Address='".$address."', ";
                    $query = $query."Contact='".$contact."', ";
                    $query = $query."UserType='".$usertype."' ";
                    //$query = $query."WHERE PromoNameID='".$pID."'";   
                    //echo $query;
                    ExecuteQuery($query);
                    echo "<script type='"text/javascript'">
                        <!--
                        window.close();
                        //-->
                        </script>";
                }
?>
<!-- EndOfPHP -->
</form>
</body>
</html>

此代码有问题:

<a href="#" onclick="javascript: validateForm(action);return false;">

应写成:

<a href="#" onclick="validateForm(document.forms['userLocation'].action);return false;">

将验证器指向表单的操作。

您不能将javascript:放入onclick属性中。前缀为on的属性是回调(有效的JS函数)。javascript:类似于协议规范。因此,只能在href属性中使用javascript:。类似于:

<a href="javascript: alert(123)">test</a>

要解决您的问题,只需从onlick中删除javascript:

<div id="addBtn">   
        <a href="#" onclick="validateForm(action);return false;"><img src="images/add.png" height="27" width="60"></a>
</div>