0

I created a form for a username and a text message but right now pressing submit doesn't actually submit anything despite it being close to the code for my login and registration form wich do properly work. the code:

<form id="myChatForm">
<input type= "text" placeholder= "Enter a name" name= "user_name" id= "user_name"><br>
<textarea placeholder= "type your message" name="message" id="message" cols="30" rows="3"></textarea><br>
<button type= "submit" name= "submit" value= "submit" class= "btn btn-succes btn-lg" id = "sendMessageBtn"> Send Message</button><br>
</form>
<?php
include("config.php");
if (isset($_POST['user_name']) && isset($_POST['message'])){
   if (isset($_POST['user_name'])) {
       $uname = ($_POST['user_name']);
    }
   if (isset($_POST['message'])) {
       $umessage = ($_POST['message']);
   }

   $query = "INSERT INTO chatroom (name, message) VALUES ('$uname', '$umessage')"; 
   $run = mysqli_query($con, $query);
   }
   ?>

For as far as I know, the problem lies with the form because before I had

if (isset($_POST['user_name']) && isset($_POST['message'])){

I would get errors saying that user_name and message were both undifined.

4 Answers4

1
<form id="myChatForm" method="POST" action="**??**">
...
</form

Try setting forms 'method' to POST, and setting the 'action' to where you want the form to POST the values to.

iCreateAwesome
  • 4,334
  • 1
  • 15
  • 6
1

It seems you have a misunderstanding on how HTML forms work.

The web page that contains the form and the web page that gets the form input and acts on it are not the same page/file. Your form web page points to your form action web page by using the action attribute.

You can put them both on the same HTML file, but I suggest you gain a good understanding on how HTML forms work with the classic "two separate files" design first before complicating things further.

SunKnight0
  • 3,331
  • 1
  • 10
  • 8
1

By default, the form's method is a GET. Either you access your fields using $_GET or change your form's method to POST

<form id="myChatForm" method="POST">
MrByte11
  • 300
  • 2
  • 9
0

Add method to your form method="post", you search vars in $_POST[""], but sends by default - GET, so write:

<form id="myChatForm" method="post">
Alexey Usachov
  • 1,364
  • 2
  • 8
  • 15