Jump to content

HELPP!!!


chris_161

Recommended Posts

Hi there. Im quite new to php but slowly getting there lol. Below is a bit of code ive written, but for some reason the invalid characters bit does not work properly and i cannot see what is wrong. Any help would be much appricated. Thanks in advance.

 

 

 

CODE***

 

<title>PHP</title><center>

<?php

  if (empty($_POST['username']))

  {

    $errors[] = 'Please enter a Username';

  }

  if (empty($_POST['SID']))

  {

    $errors[] = 'Please enter a SID';

  }

  else if (!is_numeric($_POST['SID']))

  {

$errors[] = 'Please enter a valid SID with numeric values';

  }

  if (empty($_POST['comments']))

  {

    $errors[] = 'Please enter some comments';

  }

  else if (strlen( $_POST['comments'] )> 50)

  {

      $errors[] = 'comments is too long';

  }

  else if (false == ereg( '^[A-Za-z0-9_-]+$', $comments ))

  {

      $errors[] = 'comments contains invalid characters';

  }

  else

$mail= $_POST["mail"];

 

if(!filter_has_var(INPUT_POST,"mail"))

 

{

 

$errors[] =  "Input type does not exists";

 

}

 

else

 

{

 

if(!filter_input(INPUT_POST,"mail",FILTER_VALIDATE_EMAIL ))

 

{

 

$errors[] =  "Email address is not valid";

 

}

 

else

 

{

 

echo "Form successully submitted";

 

}

 

}

if (count($errors) == 0)

  {

    // Process form

  }

  else

  {

    echo $errors[0];

  }

?>

<form method="post">

  <table>

    <tr>

      <td>Username:</td>

      <td><input type="text" name="username"></td>

    </tr>

    <tr>

      <td>SID:</td>

      <td><input type="text" name="SID"></td>

    </tr>

    <tr>

      <td>Comments:</td>

      <td colspan="2"><textarea name="comments"></textarea></td>

    </tr>

    <tr>

    <td>Enter an Email address :</td>

    <td><input type="text" name="mail"/></td>

    </tr>

    <tr>

      <td colspan="2"><hr><center><input type="submit" name="submit"></center></td>

    </tr>

  </table>

</form>

</center>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/206982-helpp/
Share on other sites

Hello,

 

Try replacing the way you are checking the contents of the field as follows:

 

Current

empty($_POST['username'])

 

Replace with

trim($_POST['username']  . "") != ""

 

The  . "" is a bit redundant, but I have had some cases where I needed to use that to ensure no errors.  It's really out of laziness ^.^

trim Will remove any of the trailing spaces, so the string will be truncated.

!= ""  means that the value is not

 

Short and sweet, should fix your problem.

 

EDIT: In the future, you might want to put a subject which is more descriptive of your problem than "HELP", or such.  In this case, a good subject would be something like "IF statements not processing"

Link to comment
https://forums.phpfreaks.com/topic/206982-helpp/#findComment-1082327
Share on other sites

@myrddinwylt

 

Why is that method of evaluating if the string is empty, better than the function actually designed for it? Plus if you look at the regexp he's already performing a check to make sure there's no spaces, so the trim() call's redundant.

 

@chris_161

 

Use empty() as you were.. ereg however is now deprecated, you should be using preg_match:

 

  else if (!preg_match('/^[A-Za-z0-9_-]+$/', $comments))
  {
      $errors[] = 'comments contains invalid characters';
  }

 

Edit: where are you defining $comments? On the previous line you're using $_POST['comments'] ..

Link to comment
https://forums.phpfreaks.com/topic/206982-helpp/#findComment-1082333
Share on other sites

<title>PHP</title><center>

<?php

  if (empty($_POST['username']))

  {

    $errors[] = 'Please enter a Username';

  }

  if (empty($_POST['SID']))

  {

    $errors[] = 'Please enter a SID';

  }

  else if (!is_numeric($_POST['SID']))

  {

$errors[] = 'Please enter a valid SID with numeric values';

  }

  if (empty($_POST['comments']))

  {

    $errors[] = 'Please enter some comments';

  }

  else if (strlen( $_POST['comments'] )> 50)

  {

      $errors[] = 'comments is too long';

  }

  if (!preg_match('/^[A-Za-z0-9_-]+$/', $comments)) 

  {     

      $errors[] = 'comments contains invalid characters'; 

  }

  else

$mail= $_POST["mail"];

 

if(!filter_has_var(INPUT_POST,"mail"))

 

{

 

$errors[] =  "Input type does not exists";

 

}

 

else

 

{

 

if(!filter_input(INPUT_POST,"mail",FILTER_VALIDATE_EMAIL ))

 

{

 

$errors[] =  "Email address is not valid";

 

}

 

else

 

{

 

echo "Form successully submitted";

 

}

 

}

if (count($errors) == 0)

  {

    // Process form

  }

  else

  {

    echo $errors[0];

  }

?>

<form method="post">

  <table>

    <tr>

      <td>Username:</td>

      <td><input type="text" name="username"></td>

    </tr>

    <tr>

      <td>SID:</td>

      <td><input type="text" name="SID"></td>

    </tr>

    <tr>

      <td>Comments:</td>

      <td colspan="2"><textarea name="comments"></textarea></td>

    </tr>

    <tr>

    <td>Enter an Email address :</td>

    <td><input type="text" name="mail"/></td>

    </tr>

    <tr>

      <td colspan="2"><hr><center><input type="submit" name="submit"></center></td>

    </tr>

  </table>

</form>

</center>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/206982-helpp/#findComment-1082344
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.