How to validate php if form is filled - php

//So i have a php validation external and a html file. I'm trying to validate if //the input boxes are filled out correctly... so far i have one but I can't get it //to run and i tried testing it out doesn't work... do i need to download //something or is my code completely wrong. I just trying to check if its empty and if it has at least 3 characters
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cal5t</title>
</head>
<body>
<?php
$title= $_REQUEST["title"];
if ($title == "" or >3 ) {
echo "<p>5please!</p>";
?>
</body>
</html>

You are probably looking for something like:
if (($title == "") or (strlen($title) < 5))
{
echo "<p>5please!</p>";
}

if(!empty($title= $_POST['title']) && strlen($title) > 5){
echo "valid: $title";
} else {
echo "incorrect title: '$title'";
}
Also, its beter to use $_POST or $_GET over $_REQUEST.

I think you're looking for:
if(strlen($title) < 5){
echo '<p>The title you entered is not long enough.</p>";
}
You can make sure there is a value with the isset() function.

requird validation
$title = trim($_POST['title']); // it will remove stat and end spaces
if(strlen($title) <= 0)
{
echo "Title Field is required";
}

Related

how to add a parameter to the url in php?

I'm trying to add the parameter cs with a value of 1 to the url using a form and hidden type input. When I execute this, I have a simple if statement that checks to see if $_GET['cs'] is = to 1, but I get a Notice: Undefined index. I'm a php beginner, any help, explanations, or alternatives is very much appreciated.
echo '<form>';
echo '<input type="hidden" name="cs" value="1">';
echo '</form>';
if($_GET['cs'] == 1) {
echo'working';
}
else {
echo 'not working';
}
Only after form is submitted you will see 'cs' in $_GET array.
The $_GET array contains all parameters on the URL line.
For example if a URL is: https://example.com?param1=1&param2=2
You will get a $_GET array with two entries ('param1' and 'param2') with values 1 and 2.
You should add a check using the isset function (https://www.php.net/manual/en/function.isset.php). Otherwise it will try to access the parameter if it isn't sent. In your case the parameter was probably not sent.
Url in html:
test the get method
The url test
<?php
if ($_GET['varaible'] == true) {
$varaible = 'Its work';
$value = false;
}else{
$varaible = 'Oooppss';
$value = true;
}
echo $varaible . '<br>';
?>
Full code:
<?php
if ($_GET['varaible'] == true) {
$varaible = 'Its work';
$value = false;
}else{
$varaible = 'Oooppss';
$value = true;
}
echo $varaible . '<br>';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>get varaible</title>
</head>
<body>
test the get method
</body>
</html>

Why using operator in front of function?

I started learning PHP 1 week ago, i'm making some form tests to get more apprehension on the language, i get stuck in problem small problem for you guys....
I used in_array function from the PHP manuale to check an array off names at login
The function did not work to check the array tell i add !in_array !!!
Normal in_array did not work to check for names ...
So I used blaindly the operator "!" in front of the function, so why did it work with that operator and not without it ?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Forum</title>
<meta name="Nic" content="" />
<link rel="stylesheet" type="text/css" href="style.css"> </head>
<body>
<?php
if(isset($_POST['submit'])){
$names = array("Nicolaus", "Solo", "Yiu", "Yang", "Darius");
$minimum = 5;
$maximum = 10;
$username = $_POST['username'];
echo '<br>';
$password = $_POST['passwoard'];
if(strlen($username) < $minimum){
echo 'Username needs to be longer then 5 carcaters !';
}
if(strlen($username) > $maximum){
echo "Username can't be longer then 10 carcaters";
}
if(!in_array($username, $names)){
echo "You are not alowed , not in database !";
} else {
echo 'Welcome';
}
echo '<br>';
echo 'Your Username is : ' . "<b>" . $username . '</b>';
echo ' Your Password is : ' . "<b>" . $password . '</b>';
}
?>
<form action="my_from.php" method="post">
<input type="text" name="username" placeholder="username">
<input type="password" name="passwoard" placeholder="password">
<input type="submit" name="submit"> </form>
</body>
Ty
! means NOT, its an operator to make comparisons when you want to check if something is false.
$raining = false;
if (!$raining){
echo "its not raining";
}
Now that you posted your code:
if(!in_array($username, $names)){
echo "You are not allowed , not in database !";
} else {
echo 'Welcome';
}
in_array is a function that checks if some value is inside an array. You are testing if $username is inside $names array.
Your conditional checks if $username is NOT in array, if so, it means that $username is not allowed because it doesn't make part of the permitted $names.
Your ELSE clause identify that it is in the array, and then the user is able to login.
you can invert your logic:
if(in_array($username, $names)){
echo 'Welcome';
} else {
echo "You are not allowed , not in database !";
}
And it will work as well.
! symbol is used for negating the logic of the function.
What you actually want to do is, you want to reject an user with $username, if his/her name is NOT in the $names array. You have got a function to check whether an element is present in an array. So adding ! operator to it negates the logic.
if (!in_array($username, $names))
That means if the $username is NOT present in array $names.
Using ! is similar to checking with condition == FALSE. But we can't directly use that logic on that function so we opted for ! operator.
Another alternative for your example without ! is,
if (in_array($username, $names)) {
echo "Welcome";
} else {
echo "You are not alowed , not in database !";
}
Hope this helps! :)

PHP If / Else statement going directly to the Else without waiting for form input

Ok so I have a form with 1 input and a submit button. Now I am using an if/else statement to make three acceptable answers for that input. Yes, No, or anything else. This if/else is working the thing is the code is kicking out the else function as soon as the page is loaded. I would like there to be nothing there until the user inputs then it would show one of three answers.
Welcome to your Adventure! You awake to the sound of rats scurrying around your dank, dark cell. It takes a minute for your eyes to adjust to your surroundings. In the corner of the room you see what looks like a rusty key.
<br/>
Do you want to pick up the key?<br/>
<?php
//These are the project's variables.
$text2 = 'You take the key and the crumby loaf of bread.<br/>';
$text3 = 'You decide to waste away in misery!<br/>';
$text4 = 'I didnt understand your answer. Please try again.<br/>';
$a = 'yes';
$b = 'no';
// If / Else operators.
if(isset($_POST['senddata'])) {
$usertypes = $_POST['name'];
}
if ($usertypes == $a){
echo ($text2);
}
elseif ($usertypes == $b){
echo ($text3);
}
else {
echo ($text4);
}
?>
<form action="phpgametest.php" method="post">
<input type="text" name="name" /><br>
<input type="submit" name="senddata" /><br>
</form>
You just need to call the code only when the POST value is set. This way it will only execute the code when the form was submitted (aka $_POST['senddata'] is set):
if(isset($_POST['senddata'])) {
$usertypes = $_POST['name'];
if ($usertypes == $a){
echo ($text2);
}
elseif ($usertypes == $b){
echo ($text3);
}
else {
echo ($text4);
}
}
Just put the validation in the first if statement like this:
if(isset($_POST['senddata'])) {
$usertypes = $_POST['name'];
if ($usertypes == $a) {
echo ($text2);
} elseif ($usertypes == $b) {
echo ($text3);
} else {
echo ($text4);
}
}
When you load your page the browser is making a GET request, when you submit your form the browser is making a POST request. You can check what request is made using:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Your form was submitted
}
Put this around your form processing code in order to keep it from being executed on GET request.

php - variable from a form - lack of special characters

I'm aware there are many similar questions on this forum, but it's the 2nd day that I'm going through the answers and nothing seems to work.
It's my first week of PHP learning, so please try to answer in a simple way :) So:
I'm creating a conjugator and so far it's going well, but only if I don't use special characters. As Polish verbs are all about special characters, I'm stuck.
This code works (the conjugation is visible on the screen after pressing "submit"):
Page 1:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Conjugator</title>
</head>
<body>
<form action="new.php" accept-charset="UTF-8" method="post" >
Conjugate: <input type="text" name="verb"><br>
<input type="submit">
</form>
</body>
</html>
Page 2:
<?php
header('Content-Type: text/html; charset="UTF-8"');
$verb = $_POST['verb'];
$last2 = substr ($verb, -2);
$last3 = substr($verb, -3);
$last4 = substr($verb, -4);
$root2 = str_replace($last2, "", $verb);
$root3 = str_replace($last3, "", $verb);
$root4 = str_replace($last4, "", $verb);
$nic = array("nie", "nisz", "ni", "nimy", "nicie", "nia" );
$gnic = array("gnije", "gnijesz", "gnije", "gnijemy", "gnijecie", "gnija");
$ac = array("am", "asz", "a", "amy", "acie", "aja");
if ($last3 == "nic" && $last4 != "gnic") {
foreach ($nic as $one) {
echo "<li>$root3$one</li>";
}
}
elseif ($last4 == "gnic") {
foreach ($gnic as $one) {
echo "<li>$root4$one</li>";
}
}
elseif ($last2 == "ac") {
foreach ($ac as $one) {
echo "<li>$root2$one</li>";
}
}
?>
But if I write for example:
$nic = array("nię", "nisz", "ni", "nimy", "nicie", "nią" );
and then:
if ($last3 == "nić" && $last4 != "gnic")
no result shows up.
For checking, try with verbs like "pienić" or "lśnić" (they don't work) or write them without the special characters ("pienic", "lsnic") - the first code will work.
Help will be deeply appreciated!!
You probably can't use substr() with UTF-8. It thinks in terms of bytes, not characters. Take a look at mb_substr().

using javascript to disable link in php script

Hello I am trying to disable a link in my PHP script using a JS function. I am not sure why is its not working. Any input appreciated..
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function disable()
{ document.getElementById("perfumes_web_link").innerHTML='<http://www.google.com" rel="external"">Perfumes';
}
function enable()
{ document.getElementById("perfumes_web_link").innerHTML='<a href="http://www.google.com" rel="external"">Perfumes<\/a>';
}
</script>
</head>
<?php
$disable_link = 0 ;
echo '<h1>JavaScript to disable link if set to zero</h1>';
echo '<li><span id="perfumes_web_link"></span><br>'."\n";
echo '<span class="perfumes_browser"><small>(Internet Explorer v6 or higher req.) < /small></span></li>';
if ($disable_link = 1)
{echo '<script type="text/javascript">disable();</script>';
}
elseif ($disable_link = 0)
{
echo '<script type="text/javascript">enable();</script>';
}
?>
</html>
So what am i doing wrong? Should i use if/else statement in the JS function itself or should i use true/false instead of 0 and 1.
Should i use something else like jQuery or Ajax..
Thanks
since you are comparing, use == instead of =,
like, change
if ($disable_link = 1)
to
if ($disable_link == 1)
OR you can do:
if ($disable_link == 1) {
echo '<script type="text/javascript">disable();</script>';
}
else {
echo '<script type="text/javascript">enable();</script>';
}
If you mean to open the link in new window, use target="_blank" instead of rel="external", like:
change
<a href="http://www.google.com" rel="external"">Perfumes<\/a>
to
Perfumes

Categories