在提交之前过滤表单参数

时间:2013-12-05 03:31:17

标签: javascript php html url

在提交之前,有没有办法检查参数是否为空?

<form method="GET">
    <input name="param1" value="test"/>
    <input name="param2" value=""/>
    <input type="submit" name="" id="search-submit" class="button" value="Submit">
</form>

如果我有这样的表格。 param2是空的,所以当我按提交时,它看起来像这样

link.com?param1=test&param2=

因此,当param为空时,有可能达到这样的目的。它不应该包括在内。 像

link.com?param1=test

因为param2是空的

5 个答案:

答案 0 :(得分:2)

纯粹的javascript方法

form.onsubmit=function(){
  var arr=this.elements,i=0,l=arr.length;
  for(;i<l;i++){
    if(arr[i].value===''){
      arr[i].disabled=true;
    }
  }
}

回答OP的进一步问题

function checkForm(){
  var arr=this.elements,i=0,l=arr.length;
  for(;i<l;i++){
    if(arr[i].value===''){
      arr[i].disabled=true;
    }
  }
}
var arr=document.getElementsByTagName('form'),i=0,l=arr.length;
for(;i<l;i++){
  arr[i].onsubmit=checkForm;
}

答案 1 :(得分:1)

总是更好地验证服务器端的数据,用户可以禁用javascript并仍然提交表单。看看这段代码并尝试一下:

//完整代码

// myFile.php

<?php
if(isset($_GET['get'])){        
        //validating:
        if($_POST['param1']==""||$_POST['param2']==""){
            echo 'come on boy, insert a text';
        }else{
            echo 'ok i got it <hr />'
            .$_POST['param1'].'<br />'
            .$_POST['param2'].'<br />';
        }
}else{

?>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script> 
<script type="text/javascript">
$(document).ready(function(){
     $('#idForm').ajaxForm({
        beforeSend: function() { 
           $('#response').html('Processing...').show();
        },
        error: function(xhr, ajaxOptions, thrownError) {
            $('#response').html('Error:<br />'+thrownError);
        }, 
        success: function(response) { 
            $('#response').html(response);
        }
    });
});

</script>
<form method="POST" id="idForm" action="myFile.php?get=0">
    <input name="param1" id="idParam1" value="test"/><br />
    <input name="param2" id="idParam2" value=""/><br />
    <input type="submit" name="" id="search-submit" class="button" value="Submit"><br /><br />
</form>

<div id="response"></div>

<?php   
}   
?>

答案 2 :(得分:1)

使用纯javascript,它会像 - (未经测试)

var form = document.forms[0];
form.onsubmit=function(){
    var inputs, index;
    inputs = document.getElementsByTagName('input');
    for (index = 0; index < inputs.length; ++index) {
         if(inputs[index].value == ''){
            form.removeChild(inputs[index]);
         }
    }
}

使用jQuery,它会像 - (未经测试)

$('form').submit(function() {
     $('input').each(function(){
        if($(this).val() == ''){
           $(this).remove();
        }
     });
});

答案 3 :(得分:0)

你必须检查param是否在 php 中使用isset()函数设置 所以:

if ( isset($_GET['param2']) ) { //do some thing }...

主要是页面会给你一个错误,因为第一个视图没有设置$ _GET;

所以将代码更改为::

if ( isset( $_GET['SUBMIT BUTTON'] ) ) {
    if ( isset ( $_GET['param2']) ) {
    //do some thing
  }
}

答案 4 :(得分:0)

提交前过滤:

<form name="FOOO">
    <input type="text" name="firsttt"   value="blabla">
    <tEXtarea> aaa</tEXtarea>
    <input type="text" name="Secondssssss"   value="blabla2">
    <input type="submit" onclick="return MyValidator('FOOO');">
</form>

<script type="text/javascript">
function MyValidator(FORMNAMEE){
    var inputs = document.forms[FORMNAMEE].getElementsByTagName("input");
    for(var i = 0; i < inputs.length; i++)    {
        alert(inputs[i].value);
    }
    var textareas = document.forms[FORMNAMEE].getElementsByTagName("textarea");
    for(var i = 0; i < textareas.length; i++)    {
        alert(textareas[i].value);
    }
}
</script>
相关问题