PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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.
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.
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.
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
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:
<?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;?>
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: