-1

I have problem about Notice: Undefined index: comType on line 4.if ($_POST['comType'] == "parseComment") part.I don't know php very well.If you can help it would be very helpul.Thx.

mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db("yorum") or die (mysql_error());

if ($_POST['comType'] == "parseComment") {

    $name = $_POST['userName'];
    $location = $_POST['userLocation'];
    $comment = $_POST['userMsg'];

    $sql = mysql_query("INSERT INTO guestbook (name, post_date, comment, location) 
        VALUES('$name', now(),'$comment','$location')")  
        or die (mysql_error());
  • 1
    `comType` does not exist in the `$_POST` array. Most likely meaning that your form does not have an input with that name. This is also happening because you probably haven't submitted your form yet. – SamV Feb 20 '14 at 20:21
  • Check out http://ro1.php.net/isset – Sergiu Paraschiv Feb 20 '14 at 20:21
  • This question comes up a lot. Check this post for info: https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index?rq=1 – Mr. Llama Feb 20 '14 at 20:22
  • `if ($_POST['comType'] == "parseComment")` is really not enough to go on. We need to see your form, and how it's being used. The way you have it now, would only work if `parseComment` was the entered text for the input element, or from a checkbox, radio button, or select. You need to elaborate on your question. – Funk Forty Niner Feb 20 '14 at 20:27

3 Answers3

0

Try this, use isset:

if(isset($_POST['comType']))
if ($_POST['comType'] == "parseComment")
//rest of the code
Hackerman
  • 12,139
  • 2
  • 34
  • 45
0

The comType variable hasn’t been passed as a POST argument. To account for this case, you need to modify your fourth line to use isset():

 if (isset($_POST['comType']) && $_POST['comType'] == "parseComment") {
0

The way you have it now, would only work if parseComment was the entered text for the input element, or the value from a checkbox, radio button, or select (I don't know what you have as a form element).

Because of this: if ($_POST['comType'] == "parseComment")

...which basically states: "If POST equals this text (or value), perform the operation.

Here is what I came up with to test my above-said, which will only test true if what seems to me would be the entered text "parseComment".

In the following example, it would return TRUE if $comtype = $_POST['comType']; was indeed set in a variable.

A form has been included in my example, since none was included in your question, therefore it makes it that much more harder to know whether or not the form's element is named or not, and/or whether there was a typo/lettercase.

Sidenote: parseComment and parsecomment <= with a lowercase c are not the same. So, if that is the case (no pun intended), then you need to double check everything.

<?php
if ($_POST['comType'] == "parseComment") {

    $comtype = $_POST['comType'];
    $name = $_POST['userName'];
    $location = $_POST['userLocation'];
    $comment = $_POST['userMsg'];

echo $comtype;
echo "<br>";
echo $name;
echo "<br>";
echo $location;
echo "<br>";
echo $comment;
echo "<hr>";
}
?>

<form action="" method="post">
ComType: 
<input type="text" name="comType">
<br />
Username: 
<input type="text" name="userName">
<br />
Location: 
<input type="text" name="userLocation">
<br />
Comment: 
<input type="text" name="userMsg">
<br />
<input type="submit" name="submit" value="Submit"><br />
</form>

A different method:

<?php
if ($_POST['comType'] == "parseComment") {

    $comtype = $_POST['comType'];
    $name = $_POST['userName'];
    $location = $_POST['userLocation'];
    $comment = $_POST['userMsg'];

    echo $comtype;
    echo "<br>";
    echo $name;
    echo "<br>";
    echo $location;
    echo "<br>";
    echo $comment;
    echo "<hr>";
}
    if(empty($_POST['comType'])){
    echo "ComType is either not set or is empty. Please enter a value.";
}
    if ($_POST['comType'] !== "parseComment") { // check if NOT equal to
    echo "That is not the comment I was looking for.";
}
?>

<form action="" method="post">
ComType: 
<input type="text" name="comType">
<br />

Username: 
<input type="text" name="userName">
<br />

Location: 
<input type="text" name="userLocation">
<br />

Comment: 
<input type="text" name="userMsg">
<br />

<input type="submit" name="submit" value="Submit"><br />
</form>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141