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
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
Share on other sites

Thanks for you quick reply. So where i have the code--> if (false == ereg( '^[A-Za-z0-9_-]+$', $comments ))

  {

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

 

Shall i do the same process and put the trim value there?

 

Link to comment
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
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
Share on other sites

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

 

As hinted in the edit, you need to change $comments to $_POST['comments'] as in the previous conditions.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.