简体   繁体   中英

strlen() doesn't work with $_POST

I am using this code in my *.php file

$check = $_POST['dati'];

 if (strlen($check) != 0) {
  // calculations
 }
 else {
   echo "contact me at <a href='mailto:myemail'></a>";
 }

The dati inside that POST is the name attribute of an input field. If the input has a length = 0, that echo must be displayed.

I have the same code in another part of my server and it works perfectly. I'm having troubles here because when the length of dati is 0, the script makes the calculations instead of showing the echo.

<input name="dati" id="dati" style="width:310px" type="text">

This is the code of the input. Any ideas?

You code works well as you can see here: http://codepad.org/fCvlokOJ and here http://codepad.org/t2TvFozt

<?php
$_POST['dati']= "text";

if (strlen($_POST['dati']) != 0) {
   echo" calculations";
 }
 else {
   echo "contact me at <a href='mailto:myemail'></a>";
 }

In the following example, if field is left blank, echo'ed message is "sorry".

If text entered, then Email link appears. TESTED

<?php

if(isset($_POST['submit']))

$check = $_POST['dati'];

 if (strlen($check) == 0) {

echo "Sorry";

 }
 else {
   echo "Contact me at <a href='mailto:myemail'>LINK</a>";
 }

?>

FORM

<form method="post" action="your_handler.php">

<input name="dati" id="dati" style="width:310px" type="text">

<input type="submit" name="submit" value="Submit">

</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM