Page 1 of 1
please How i Make Required fields in php form
Posted: Mon Dec 27, 2010 6:14 am
by Mr.only
please ..
i have this php form
Code: Select all
<FORM action="add.php" method="post">
<TABLE width="100%" border=0>
<TBODY>
<TR>
<TD align=center><FONT color=#ff0000>* </FONT>title:<BR><INPUT size=40
name=title><BR></TD>
</TR>
</TBODY>
<TABLE width="100%" border=0>
<TBODY>
<TR>
<TD align=center><FONT color=#ff0000>* </FONT>Url<BR><INPUT size=40
name=url value=http://><BR><TD></TR></TBODY></TABLE>
</TD>
</TR>
<TABLE width="100%" border=0>
<TBODY>
<TR>
<TD align=center>description<BR>
<textarea cols="50" rows="4" name="description"></textarea><BR><TD></TR></TBODY></TABLE>
</TD>
</TBODY></TABLE>
<P style="TEXT-ALIGN: center" align=center><INPUT class=submit type=submit value="add" name=send></TD>
</FORM>
and i want make the first 2 fields required ( title and url )
but i want the user don't leave the form if this 2 fields empty ..
i need the user don't go the add.php if this fields empty
or get Pop-up message .. like :: please fill the blank fields
so can anyone help me ..
Re: please How i Make Required fields in php form
Posted: Mon Dec 27, 2010 6:50 am
by Darhazer
Actually this is an HTML form, not a PHP one.
If you want to perform validation on the client side (before form is submitted to add.php), you need to do this with JavaScript.
But you MUST perform the same validation in PHP, since the client can disable javascript validation and submit the form.
Re: please How i Make Required fields in php form
Posted: Mon Dec 27, 2010 7:37 am
by Mr.only
ok .. how can i do that ? .. please
Re: please How i Make Required fields in php form
Posted: Mon Dec 27, 2010 7:51 am
by Darhazer
Try this one:
Code: Select all
http://docs.jquery.com/Plugins/Validation/Methods/minlength
You have to include the jQuery JS file in the document. jQuery uses CSS selectors, so if you know CSS, you'll easy get to work with jQuery and it's plugins.
Re: please How i Make Required fields in php form
Posted: Mon Dec 27, 2010 8:09 am
by Mr.only
yes .. that's exactly what i want.. but
where i pot it ..
Code: Select all
$("#myform").validate({
rules: {
field: {
required: true,
minlength: 3
}
}
});
Re: please How i Make Required fields in php form
Posted: Mon Dec 27, 2010 8:34 am
by phazorRise
PHP's giving you lot of built-in functions.
Form validation using Javascript is not a good way to go to. User might have disabled Javascripts. Or he might be amature to know about it.
So with help of some built-in function you can acieve validation.
Re: please How i Make Required fields in php form
Posted: Mon Dec 27, 2010 9:02 am
by Mr.only
thanx bro .. so can you help me .. to do this with built-in function
Re: please How i Make Required fields in php form
Posted: Tue Dec 28, 2010 8:22 am
by phazorRise
[text]Okay. You can use following function to validate entered string.[/text]
Code: Select all
function fnValidateStringr($str)
{
#letters and space only
return preg_match('/^[A-Za-z\s ]+$/', $str);
}
[text] It checks string against regular expression. This RE represents only spaces and characters.
Function will return 0 or 1. 0 for string doesn't contain chars or spaces. 1 if string contain chars. [/text]
OR
You can simply count no of chars user entered. using
function.
Re: please How i Make Required fields in php form
Posted: Tue Dec 28, 2010 1:00 pm
by amirbwb
hello, so i just discovered how 2 months ago

so this is so easy to do it you just need PHP ... you can also use javascript but i think the php is more dynamic and it work 100%
1-create the from with the two fields and add at the end of the form (still inside the form) a hidden field <input type="hidden" name="insert" value="insert"/>...
2-go to the top of the page and write this:
Code: Select all
<?php
if(isset($_POST['insert']) && $_POST['insert'] == "insert"){
if(empty($_POST['url']) || $_POST['url'] == ''){
$msg1='Please enter a URL';
}
if(empty($_POST['description']) || $_POST['description'] == ''){
$msg2='Please enter the description';
}
if(!isset($msg1) || !isset($msg2)){
//insert the info in database
INSERT INTO ....
}
}
?>
when finish don't forget to put this next to the field URL: <?php echo $msg1;?> and DESCRIPTION:<?php echo $msg2;?>
GOOD LUCK
Re: please How i Make Required fields in php form
Posted: Wed Dec 29, 2010 1:52 pm
by Mr.only
thanxx amirbwb
that's what i taking about .. but i still have simple problem ..
i do everything . but the page appear blank ..
i think.. the problem in this part ..
Code: Select all
if(!isset($msg1) || !isset($msg2)){
//insert the info in database
INSERT INTO ....
}
because the form go to the add.php . after user fill the fields . and inside add.php the php who send to db.
and i wonder why i must add hidden field ..
can you help me again please ..
Re: please How i Make Required fields in php form
Posted: Thu Dec 30, 2010 3:40 pm
by amirbwb
I didn't understand what is the problem ...
maybe you are asking for
and i added the hidden field to make the condition more dynamic and easier to understand ...
you can remove it but if you remove it you should change the condition to:
good luck