How can I check the input field on the form when I submit it to the server?

How can I verify that the input field exists on the form when I submit it to the server?

For example, I want to check whether or not a flag named mem_follow exists or not.

Or do I need to use javascript (jquery)?

+6
source share
10 answers

I assume that you need to check the back end after submitting the form. If this is the case, you can check it like this ...

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

Hope this helps!

+2
source

It must be Javascript. PHP cannot contact the server in the gut of the browser and check you. He can only check if the field name is present in the presented data.

In jquery, this is trivial:

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

Of course, this begs the question ... why do you need to know if a field exists or not? Presumably, it was you who created the form. You need to know if this field exists or not.

+1
source

In the following script, the form will not be submitted if the checkbox is unchecked. And if you try, a hidden div will display with an error message so that 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

+1
source

You can do this in two ways:

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 } ?> 

On the 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; } } }); 
+1
source

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 does not matter if it is GET or POST)

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

I suggest you 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))))

0
source

You can try this code in the form of submit event handler

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

The other answers here are correct: you have to do this on the client side. In the case of the check box, if the check box is not selected, there is no guarantee that the browser will include the POST parameter for this field.

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

 <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 sent.

So no, you cannot guarantee that this flag exists in the form on the server side.

0
source

Good for all elements try this

 number = document.form_name.elements.length; 

else for a specific input name use this

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

It's him

to use

0
source

You can use javascript (or jQuery) for this:

 <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 placing all the conditions in an if statement .

0
source

Source: https://habr.com/ru/post/895778/


All Articles