Copy link to clipboard
Copied
Hello good people.
I have a very basic question to ask. ![]()
Here is my index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PHP Test</title>
<style>
body {
margin:50px;background-color:#232323; font-family:sans-serif; color:#B7B7B7; font-size:13px;
}
input, textarea {
border: #464646 solid 1px; background-color:#1C1C1C; font-family:sans-serif; color:#CECECE; font-size:10px; margin-top:15px; margin-bottom:15px;
}
#submit {
font-size:13px;
}
</style>
</head>
<body>
<form action="index.php" method="post">
What is your name?<br />
<input type="text" name="name" /><br />
What is your age?<br />
<input type="text" name="age" /><br />
<textarea type="text" name="tekstas" rows="5" cols="40">Your message.</textarea><br /><br />
<input type="submit" value="Go!" id="submit"/>
</form>
</body>
</html>
And index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
body {
margin:50px;background-color:#232323; font-family:sans-serif; color:#B7B7B7; font-size:13px;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PHP Result</title>
</head>
<body>
<?php
settype($_POST["age"], "integer");
echo "Your name is ".$_POST["name"]."<br />";
if ($_POST["age"]!=0) echo "Your age is ".$_POST["age"]."<br /><br />";
if ($_POST["age"]=0 ) echo "Please specify your age."."<br /><br />";
echo "<b>Message:</b><br />".$_POST["tekstas"];
?>
</body>
</html>
The question is:
Why the Age part of php is not working properly? If I leave it black or type zero in the Age <input>, PHP just ignores the "Please specify your age" part of code.
You're relatively new to this forum. Welcome.
Please take a moment to read How to get help quickly. Using a subject line like "Newbie problem" is meaningless, and is likely to get ignored at busy times. Always try to use a descriptive subject line.
The answer to your problem is very simple. It's a common beginner's mistake that even catches out more experienced people from time to time. It's in this line:
if ($_POST["age"]=0 ) echo "Please specify your age."."<br /><br />";
You have used only one eq
...Copy link to clipboard
Copied
You're relatively new to this forum. Welcome.
Please take a moment to read How to get help quickly. Using a subject line like "Newbie problem" is meaningless, and is likely to get ignored at busy times. Always try to use a descriptive subject line.
The answer to your problem is very simple. It's a common beginner's mistake that even catches out more experienced people from time to time. It's in this line:
if ($_POST["age"]=0 ) echo "Please specify your age."."<br /><br />";
You have used only one equals sign, which assigns the value to a variable, so it always equates to TRUE.
To compare two values, you must use two equals signs like this:
if ($_POST["age"] == 0 ) echo "Please specify your age."."<br /><br />";
There are other things that could be done to improve your code, but that solves the basic problem.
Copy link to clipboard
Copied
Thank you. I'm just starting PHP, so my code is not very sophisticated, I know that I could have used "else", but I haven't got that far. Hopefully, I'll get there soon.
Thanks again mate!
Copy link to clipboard
Copied
One more question. What if I wanted to prevent from blank "Name" <input>?
if ($_POST["name"]="") echo "Please specify your name.<br />";
Does not seem to work.
Copy link to clipboard
Copied
Oh, right...
silly me.
if ($_POST["name"]=="") echo "Please specify your name.";
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more