开发者

How can I check a input field exists in the form when I submit it to the server?

How can I check a input field exists in the fo开发者_JAVA百科rm when I submit it to the server?

For instance, I want to check whether a check box named 'mem_follow' exists or not in the form.

Or do I have to use javascript (jquery)?


I'm guessing you need to check on the server side after the form is submitted. If that's the case, you can check like so...

<?php
if (isset($_POST['mem_follow']))
{
    // Work your server side magic here
} else {
    // The field was not present, so react accordingly
}
?>

Hope this helps!


It'd HAVE to be Javascript. PHP can't reach out from the server into the browser's guts and check for you. It could only check if the fieldname is present in the submitted data.

In jquery it's trivial:

if ($('input[name="nameoffield"]')) { ... field exists ... }

Of course, this raises the question... why do you need to know if a field exists or not? Presumably you're the one who's built the form. You should know already if the field exists or not.


In the following script the form will not submit unless the checkbox is ticked. And If you do try, the hidden div with an error message is shown, to let you know why.

<form action="test.php" method="post" id="myform">
    <input type="checkbox" name="checkbox" id="checkbox" value="YES" />
    <input type="submit" name="submit" value="submit" />
</form>
<div id="error_message_div" style="display:none">You must tick the checkbox</div>
<script>
    $('#myform').submit(function() {
        if($('#checkbox').attr('checked')) {
            return true;
        } else {
            $("#error_message_div").show();
            return false;
        }
    });
</script>

Cheers Matt


you can do this in two way:

By Server Side: In php

<?php
if (isset($_POST['mem_follow'])
{
    // Work your server side magic here
} else {
    // The field was not present, so react accordingly
}
?>

By Client side: In Jquery

$('#submit_button').click(function() {
  if ($('input[name="box_name"]')) {
    if($('input[name="box_name"]').attr('checked')) {
        return true;
    } else {
        return false;
    }
  }
});


You can use jQuery and do something like this:

$('#myForm').submit(function (e) {
    if($(this).children(':checkbox[name=mem_follow]').length > 0)
        //exists
    else
        //doesn't exist
});

Or use php and check if there is any variable named 'mem_follow': (this is for POST, although it doesn't matter if it's GET or POST)

if(isset($_POST['mem_follow']))
    //exists
else
    //doesn't exist


You can try this code in the form submit event handler

if($("input[name=mem_follow]").length > 0){
    //It exists
}


The other answers here are correct: you'd have to do that on the client side. In the case of a checkbox input field, if the box is not checked there is no guarantee the browser will include a POST parameter for that field.

For example, if you submit this form without checking the checkbox:

<form action="test.php" method="post">
    <input type="checkbox" name="checkbox" value="YES" />
    <input type="submit" name="submit" value="submit" />
</form>

only the submit=submit parameter will be submitted.

So, no, you cannot guarantee that a given checkbox exists in a form on the server side.


well for all elements try this

number = document.form_name.elements.length;

else for a specific name of input use this

number = document.form_name.getElementsBYName('Name of Input').length;

Thats it

enjoy


I suggest you to use Jquery

$.fn.Exists = function() {
return $(this).length>0;
}

if ($("here are your selector to check").Exists()) {
    ...
}

Simple and useful! And you can use this Exists method everywhere))))


You can use javascript (or jQuery) to do that :

<script>
    $('#myform').submit(function() {
        if($('#yourFieldId').val()=='') {
            alert('the field ' + $('#yourformField').name() + ' is empty !');
            return false;
        } else {
            return true;
        }
    });
</script>

you can do this for all your fields one by one, or by putting all conditions in the if statement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜