Ajax to PHP form.

Status
Not open for further replies.

Hender

King Tinkerer
Mar 3, 2016
304
122
Im coding a shoutbox for ModernMafia.
Whats wrong with my form?

My result:



Shoutbox code.
PHP:
<script>
$(document).on('keypress', '#message', function(event) {
 //var keycode = (event.keyCode ? event.keyCode : event.which);
    if(event.keyCode == 13) {
       event.preventDefault();
   console.log("enter click");
  //script
  var message = $("#message").val();
  // Returns successful data submission message when the entered information is stored in database.
  var dataString = 'message='+ message;
  // AJAX Code To Submit Form.
  $.ajax({
   type: "POST",
   url: "shoutboxpost.php",
   data: dataString,
   cache: false,
   success: function(result){
    console.log("worked");
    //console.log(result);
   }
  });
 }
});
</script>  
    <form role="form" id="message">
<input name="message" placeholder="Enter your message" type="text" class="textarea" style="width: 100%;margin-bottom: -3px;">
    </form>



Shoutboxpost.php
PHP:
<?php
include "configisheredontworryabouthefilenameok"; //Connects to the DB.

$username= mysql_real_escape_string($_SESSION["real_name"]); // gets the players name from session.
$comment = mysql_real_escape_string($_POST['message']); // Gets the message posted.
$time = gmdate('Y-m-d H:i:s'); // Date & Time format.
$realip= $_SERVER['REMOTE_ADDR']; // gets IP address.

mysql_query("INSERT INTO `chat` ( `id` , `username` , `message`, `time`, `ip` ) VALUES ( '', '$username', '$comment', '$time', `$realip`)"); // enters into chat table.
 
?>

Yes, im probably being lazy but, who cares. Roast me.
 

CosmoPeak

PeakRP.com
May 15, 2016
271
268
Is the record not being inserted into the DB? It could be an SQL issue if `id` is a primary key - You should just emit the `id` and '' if so.
 

Hender

King Tinkerer
Mar 3, 2016
304
122
It won't, you need to omit the ID column and it should work.
Thanks!
The website has been removed now anyway the original source was too insecure. Instead of being lazy and building upon cancerous code I'm making a new one from scratch.

Sent from my GT-I9505 using Tapatalk
 

MayoMayn

BestDev
Oct 18, 2016
1,423
683
Its not going into the DB no, but ID is set to auto increment. so it should work.
You arent supposed to put the values for the ID in the database if its auto incremented. Simply remove the `id` and '' inside the query.
EDIT:
Like so:
Code:
mysql_query("INSERT INTO `chat` (`username` ,`message`,`time`,`ip`) VALUES ('$username','$comment', '$time', '$realip')"); // enters into chat table.
A lot of errors in that query, but I've fixed it
Sent from my SM-G928F using Tapatalk
 

MayoMayn

BestDev
Oct 18, 2016
1,423
683
I sent him that fix on Skype before you all now shut up and let's not argue like a bunch of script kiddies.
Arguing like skids is claiming a fix is by writing a bunch of words pretending that its correct, when it was just some simple typos made by him.
@JMG @Bronson close the thread will ya?

Sent from my SM-G928F using Tapatalk
 

Brad

Well-Known Member
Jun 5, 2012
2,319
992
Don't have permissions for this section.
 
In future, if the problem has been solved and you want it closed. Report the thread instead of tagging. That way the appropriate moderators can handle it.
 
Status
Not open for further replies.

Users who are viewing this thread

Top