-1

I been trying to make a chat system but I keep getting errors, this is the last error I have is unexpected if on line 7

line 7:

if(isset($_POST['text']) && isset($_POST['Name']))

code

<?php
//
$db = new PDO('mysql:host=127.0.0.1;dbname=chat', 'root', '')

//secure the chat

if(isset($_POST['text']) && isset($_POST['Name']))
{
    $text = strip_tags(stripslashes($_POST['text']))
    if(!empty($text) && !empty($name))
    {
        $insert = $conn->prepare("INSERT INTO messages VALUES('', '".$name"', '".$text"')");
        $insert->execute();

        echo "<li class='cm'><b>".ucwords($name)."</b> - ".$text."</li>";
    }
}
?>

and in case my html form is incorrect

<?php

//Get username
$user = $_GET['Name'];
require 'getmessages.php';

?>
<!DOCTYPE html>
<html>
<head>
    <title>Private system</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="javscript/functions.js"></script>
</head>
<body>
<div class="chatContainer">
  <div class="chatHeader">
     <h3>Welcome <?php echo ucwords($user); ?></h3>
   </div>
   <div class="chatMessages"></div>
   <div class="chatBottom"></div>
      <form action="connect.php" id="chatForm">
        <input type="hidden" id="Name" value="<?php echo $user;?>" />
        <input type="text" name="text" id="text" value="" placeholder="Type your Message" />
        <input type='submit' name="submit" id="submit" value="Send"/>
      </form>
</div>        
<body>
</html>

thanks for anyone who helps :)

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
eric
  • 3
  • 1

1 Answers1

0

You are missing the semicolon on following lines:

$db = new PDO('mysql:host=127.0.0.1;dbname=chat', 'root', '')

And

$text = strip_tags(stripslashes($_POST['text']))

Second issue is that you didn't define the form method POST in <form>

<form action="connect.php" id="chatForm">

This should be:

<form action="connect.php" id="chatForm" method="post">

Third issue is that you are missing name attribute in name input and try to get $_POST["name"] this will return nothing:

<input type="hidden" id="Name" value="<?php echo $user;?>" />

This should be:

<input type="hidden" id="Name" value="<?php echo $user;?>" name="Name" />

Side note:

Don't know about this $user = $_GET['Name']; if you are getting it from URL than you can use it as:

$user = isset($_GET['Name']) ? $_GET['Name'] : ""; 
devpro
  • 16,184
  • 3
  • 27
  • 38