2

I'm working on a guestbook for my website. What I have right now is a basic HTML form:

<form action="submit.php" method="post">
  Name: <input type="text" value="Julian Davis" name="name"readonly><br>
  E-mail (Not published) <input type="text" name="email"><br>
  Message: <input name="message" rows="5" cols="40"><br>
  <input type="submit">
</form>

(the name field being read-only is part of the gag). It uses HTTP POST to send the results to submit.php, which looks like this:

<?php

if strlen($_POST["message"]) = 0 {
  echo "You didn\'t type a message. Don't you want people to hear 
  what you have to say?";
}

else if strlen($_POST["message"]) > 499 {
  echo "Messages are limited to 500 characters. Try again.";
}

else{
  echo "Your response has been received. Cheers!";
}
?>

I'm planning to add some SQL queries to aggregate the responses, but first I want this script to not return a blank page.

I haven't written any forms or used POST before (plus, I'm a little shaky on my elseifs), so it's probably some silly syntax thing, but I've looked the script up and down, and I can't see anything wrong with it.

(Also, if anyone has any tips for how to make this form secure, perhaps a CAPTCHA, please let me know!)

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
J. Davis
  • 31
  • 1

1 Answers1

8

Many issues:

  1. All your if statements should have ( ... ) in them, the whole syntax is wrong! Learn PHP well.

    if (expr)
      statement
    
  2. And the first one should have == instead of =.

    There are lot of questions that answer this. See The 3 different equals.

    • The = is assignment operator. This means, the left side variable gets assigned a new value.
    • The == is what you need, which is a comparison operator. Checks whether left side and right side are equal.
    • The === is strict comparison operator, which checks both the value and type of the data.
  3. Also better to use elseif instead of else if.

    As said in the comments, Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error."

Corrected Code:

<?php
    if (strlen($_POST["message"]) == 0) {
        echo "You didn\'t type a message. Don't you want people to hear what you have to say?";
    }

    elseif (strlen($_POST["message"]) > 499) {
        echo "Messages are limited to 500 characters. Try again.";
    }

    else {
        echo "Your response has been received. Cheers!";
    }
?>

Plus, your input syntax is invalid

<input name="message" rows="5" cols="40">

This type of syntax is for <textarea>. i.e.:

<textarea rows="4" cols="50">

The syntax is:

<input type="text" name="input_name" maxlength="4" size="4">

If you absolutely want to have that <input /> larger than the others, you will need to resort to using CSS.

Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • 1
    *"Also better to use elseif instead of else if"* - http://www.php.net/manual/en/control-structures.elseif.php *"Note: Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error."* – Funk Forty Niner Sep 16 '15 at 21:25
  • @Fred-ii- Well, that's fine. We **are** using curly braces here, right? And moreover, I am asking the OP to use `elseif` only. – Praveen Kumar Purushothaman Sep 16 '15 at 21:26
  • They sure are. that was just a sidenote for future visitors. However, *"And the first one should have `==` instead of `=`"* adding the fact that they are assigning rather than comparing, would improve the answer, should my comment happen to disappear about that ;-) – Funk Forty Niner Sep 16 '15 at 21:28
  • @Fred-ii- See my updated previous comment and answer. – Praveen Kumar Purushothaman Sep 16 '15 at 21:29
  • @Fred-ii- Updated buddy. – Praveen Kumar Purushothaman Sep 16 '15 at 21:31
  • 1
    Very nicely done, *cheers* – Funk Forty Niner Sep 16 '15 at 21:31
  • Thought I'd put in my own "2 cents", after studying the OP's code a bit more closely. Something else looked out of whack. However, it's hard to say if OP wanted a bigger input than the others or not, which is why I added a note about using CSS and you removed. – Funk Forty Niner Sep 16 '15 at 21:42
  • @Fred-ii- I edited it, but accidentally removed it. – Praveen Kumar Purushothaman Sep 16 '15 at 22:12
  • 1
    It's ok. I probably stood at being wrong. I myself sometimes do like to use larger buttons for certain inputs. But in this case, it's pretty clear that they should/want to use a ` – Funk Forty Niner Sep 16 '15 at 22:15