I got a problem with my code.
I want to figure out if there is a specific number in the text box and if it's empty it should say something like "There's nothing inside".
I did so but got a problem if the text box is empty.
If it's empty it skips the code for checking if it's empty, proceeds with the function after it.
That's what I got so far.
<?php
if(isset($_POST["submit"])){
$name = $_POST['winner'];
if(strpos($name,'123456789') !== false){
echo "<br><br>".$name." was the correct answer! Congratulations!";
}elseif($name !== ""){
echo "<br><br>You haven't typed in a number.";
}else{
echo "<br><br>".$name." wasn't correct. Better luck next time.";
}
}
?>
Anyone know what the error is?
This statement:
}elseif($name !== ""){
Should be:
}elseif ($name === ""){
Or:
}elseif (!strlen($name)){
I think you meant the opposite.
Should be elseif($name === "") not elseif($name !== "")
or also elseif(empty($name))
you want that if user leave text field empty it again ask for that ...
give me your email I'll mail you an example. ....
Related
is it possible to do this , im trying to validate a form then, it will redirect using header() if TRUE.. but it seems not to be working? or my method is completely wrong ?
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = $_POST["clientEmail"];
if ($email != $sentEmailClients) {
echo 'Please enter a valid email';
} else {
$newURL = "http://www.myurl.com";
header('Location: ' . $newURL);
}
}
Give us more details about what actually happens when you run your code. You're likely facing one of the following problems:
You're using header() after you've already sent output to the browser. Headers must be sent before any other output. Check out the docs. If you change that line with die('redirecting') and that text shows up, then this is your problem.
Request method is not POST. Add die($_SERVER['REQUEST_METHOD']). If something other than POST is printed, then this is your problem.
$_POST['clientEmail'] is not set, or is not equal to $email
$email is not what you expect (where does it come from?)
$sentEmailClients is not what you expect (where does it come from?)
Basically, "why doesn't it work?" is not a good question because it doesn't give us much info with which to help you. Be more specific about what is happening.
Show enough of your code that we understand the origin of the variables you use.
Hi It seems that Your outermost if condition is not working thats why your header function is not working i just tried this and it woks fine. That means either your first if condition is false or either second if condition becomes true every time just try to echo your values before checking them.
<?php
if (1) {
$email = $_POST["clientEmail"];
if (0) {
echo 'Please enter a valid email';
} else {
$newURL = "http://www.google.com";
header('Location: ' . $newURL);
}
}
check this print_r($_SERVER["REQUEST_METHOD"]); is POST or not and
$email != $sentEmailClients true or false
I've created a form which I'd like to upon the submit button being clicked, display an error if the text box is empty. That much I've managed to get working. My issue starts when I do fill out the text box and click submit. Basically, nothing is returned.
I appreciate that the code below is probably atrocious but I'm still learning and haven't yet got around to formatting of code.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["node"])) {
$nodeErr = "Hostname is required";
} else {
echo $node;
}
}
My understanding is (albeit limited at this point) echo $node should return a value if captured with ($_POST["node"]) i.e. if the text field is complete but I think I'm missing a trick here.
The value is right in the place you just checked.
if (empty($_POST["node"])) {
$nodeErr = "Hostname is required";
} else {
echo $_POST["node"];
}
Pretty sure this is a quick and easy question but I have a form that on action POST goes to a confirmation page. I need a message to display on the confirmation page if the user selects county1 but if user selects county2, county3, or county4. However, when I setup the statement it's not working. Probably a syntax error or two on my part. Any help would be greatly appreciated.
A messy idea of what I think should work:
<?php $county=$_POST['County'];
if ($county="Polk") {
echo "Important message about your county"; }
else {
echo " "; // Or nothing at all
}
?>
But
<?php echo $_POST['County'] ?>
displays the name of the county so I know the submission is carrying through. Thoughts on why my above code wouldn't be working? If you could flag syntax errors or code placement that'd be much appreciated! Thank you!
Inside the if condition you should use two equal operators instead of one . try this code
<?php
$county = isset($_POST['County'])?$_POST['County']:"";
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
?>
Use double equal in your if statement for comparison
See the answer and read the comment to understand why you have to change it
if ($county="Polk") {
Single equal is assignment operator, it assign value
Change this line to this
if ($county=="Polk") {
So your whole code should look like this
$county=$_POST['County'];
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
Use double equal sign, double equal is comparison operator,
Here you are checking if the $county is equal to Polk or not,
Means you are comparing value
I have a textarea field and want to add a text or php code when the field is empty and if the field is not empty to add another php code. For example:
if the field is empty>do something.....else>do something...
I'm not good at php, so I'll be glad for any help.
Thanks.
p.s.Sorry for my english.
Assuming you're posting this from a form then you can do the following.
I'll write this out the long way so it's easier to understand:
if (!empty($_POST['TEXTAREA_NAME']){
// If the textarea is set then do something here
} else {
// If it is NOT set it then it will do the code here
}
<?php echo isset($_POST['texarea_name']) && !empty($_POST['texarea_name']) ? 'textarea not empty' : 'texarea is empty'; ?>
To test if outside variable is empty or not, you can just compare it with empty string
if ( $_POST['text'] === '') {
echo 'there was something';
} else {
echo 'nothing to say';
}
I have script when I will input data from a textbox.
For example: In the textbox, we may input anything string excepting(admin, Admin, ADMIN, AdmiN). I am using this script, but i think my script is so long
Can you show me other ways, regex possibly, to do this?
<?php
$nama=$_POST['name'];
$pesan=$_POST['pesan'];
if(isset($_POST['submit'])){
if($nama== ("admin") || $nama==("Admin") || $nama==("AdmiN") || $nama=("ADMIN"))
{
echo "Masukkan nama yang lain";
}else{
$sql="insert into table1(nama,pesan)values('$nama','$pesan')";
$result_sql=mysql_query($sql);
header('Location:index.php');
}
}
?>
Convert the string to lower case:
if (strtolower($nama) === 'admin')
Try something like:
//make an array of your restricted words, all in lowercase
$restrictedWordsArr = array("admin"); //and more you want
//then check
if(in_array(strtolower($yourWordToCheck), $restrictedWordsArr)) {
//restricted word
}
else {
//valid
}
No need for regex, simply: strtolower($nama) == "admin"