Hi Guys i am having a very peculiar problem i have written a small code so that it is possible to sign up to the website i am creating with a few restrictions to each field however from what i see the code seems ok but each time i try to use the php code with html code the browser is always printing part of the code like this:
"11){ echo 'Username needs at least 6 and maximum 11 characters'; } else if (strpos($Username, '#') != 0) { echo ' Username cant be your email'; } ?>"
<?php
$Username = 'aasd';
if (strlen($Username) < 6 || strlen($Username) > 11){
echo 'Username needs at least 6 and maximum 11 characters';
}
else if (strpos($Username, '#') != 0) {
echo ' Username cant be your email';
}
?>
From what i can see the code is correct but i can't seem to find the reason as to why this is happening am i missing something in the PHP code? do i have some condition or operator that is not set in the right way?
Thank you first hand to all those who reply
Related
I need a simple example of php filter array. This function is new for me so please I can't understand complex program of it.
I want to store filtered data to the database from a simple form where these conditions can exist.
i.e:
NAME in capital words.
proper email/Password syntax.
proper address and mobile number (1234-1234567)
registration no: 2012-2015-AUP-1234
if any one mistakes an alert or message is displayed.
array_filter :
The official documentation for PHP is full of very understandable examples for each function. These examples are given by the documentation or the community, and follows each function description.
PHP Documentation
Form validation :
A little code example for the script called by your POST form. Of course a lot of changes to this can be made to display the errors the way you like, and perhaps tune more precise checks on each data.
<?php
if (isset($_POST['name']) && !ctype_upper($_POST['name'])) {
$errors[] = 'name should be uppercase';
}
if (isset($_POST['email']) && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = 'email is invalid';
}
if (isset($_POST['password']) && strlen($_POST['password']) >= 6 && strlen($_POST['password']) <= 16)) {
$errors[] = 'password should be between 6 and 16 characters length';
}
if (isset($errors)) {
// do not validate form
echo '<ul><li>' . join('</li><li>', $errors) . '</li></ul>';
// ... include the html code of your form here
}
else {
// ... call things that must work on validated forms only here
}
When people search for a real estate agent by zip code they will see a message on the site I'm working on that reads: There are x number of our Agents in your neighborhood.
x is the number determined by this php code:
<?php echo isset($total_record) ? $total_record : "";?>
if the number is Zero, the message sounds dumb (There are 0 number of...)
How do I change the message just for those cases with 0 as a search result? so that a different message appears? Something like - Sorry, we don't have any Agent in your immediate area.
Any help, much much appreciated.
Use a simple if statement:
if (isset($total_record) && $total_record > 0){
echo $total_record." number of our Agents in your neighborhood";
} else {
echo "Sorry, we don't have any Agent in your immediate area.";
}
if (isset($total_record))
{
if ($total_record > 0)
{
echo "There are {$total_record} of our Agents in your neighborhood.";
}
else
{
echo "There are no Agents in your neighborhood.";
}
}
Use the empty function rather than isset. The empty function checks if a variable exists and has a value. 0, false, and a few other values are also considered empty, check the manual for a full listing.
echo !empty($total_record) ? 'There are ' . $total_record . ' number of our Agents in your neighborhood.' : 'Sorry, we don\'t have any Agent in your immediate area.';
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. ....
Have a bit of a problem with some current code we're using Android to POST to PHP where using the following code
$fbid = $_POST['fbid'];
if($fbid == "no"){
echo 'no fbid';
}else{
echo 'fbid look for user';
}
We used the following strlen($fbid) which returns 2
All the posted strings don't seem to match and when used strlen() on all equal the correct amount - any ideas? could this be encoding problem?
Just as a trivial suggestion:
if($fbid == "no"){
echo 'no fbid';
}else{
echo 'fbid look for user';
}
I'm coding out a simple contact form, and the following code is giving me trouble:
if(!strlen($_POST['lastname']) > 0){
echo '<p class="message error">Please enter the parent\'s last name.</p>';
}
if(!strlen($_POST['comments']) > 5){
echo '<p class="message error">Please tell us a little more in the comments field.</p>';
}
Relevant form element:
<textarea name="comments" cols="60" rows="5"><?=(isset($_POST['comments']) ? $_POST['comments'] : '')?></textarea>
When I leave both fields blank, only the first error message (along with the others not shown) displays, whereas the one for the comments field does not.
The error checking even returns with an error if I submit the comments fields with less than 5 characters, as it should, but the error message does not print. In addition, I even echoed the strlen() of the comments field when I submitted with it blank and it prints out 0.
Can anyone see what the problem here is?
if (!strlen > 0) first evaluates strlen, which gives, say, 10. This is then negated by ! to false. This is then compared to > 0, which is false, since false will be cast to 0 and 0 > 0 is false. The other way around, if the string is actually empty, the condition will be true.
You either want if (!(strlen > 0)) or if (strlen <= 0).
Missing parenthesis?
if(!(strlen($_POST['lastname']) > 0)) {
echo '<p class="message error">Please enter the parent\'s last name.</p>';
}
if(!(strlen($_POST['comments']) > 5)) {
echo '<p class="message error">Please tell us a little more in the comments field.</p>';
}
If you want to validate the data whether it is entered you can use following options too
if(empty($_POST['name'])){
echo 'Enter Name';
}
or
if(trim($_POST['name'])==''){
echo 'Enter Name';
}
Just to be clear, the problem here is that the ! applies to the return value from strlen only and not the entire comparison expression. The above if statements will always return false.
It could be rewritten like this to work:
if (!(strlen($_POST['lastname']) > 0)){ /* display error */ }
This is because the ! negates the result of the expression nested in parentheses and not the number returned by strlen.
Also, I would recommend not returning the POSTed value unaltered in the HTML source, that's just asking for trouble...