1

I know there are tons of questions like this one, but I still can't get this to work properly. Even a comment of what I should do to make this correct is more than enough since I understand like 80% of the jQuery..

<form action="meddelanden.php" id="fromen2" method="post">
    <input type="text" name="message" id="type" autocomplete="off" placeholder="type your chat message">
    <input class="lg" type="submit" name="submit" value="Send">
</form>

Meddelanden.php

<?php
    session_start();
    $meddelanden = $_POST['message'];
    $username = $_SESSION['user'];
    include ("connect.php");
    $sql = $con->prepare('INSERT INTO messages (message,username) VALUES (?,?)');
    $sql->bind_param("ss",$meddelanden,$username);
    $sql->execute(); 
    $sql->close();
    $con->close(); 
?>

Scripts (which mess things up for my head)

$('#fromen2').submit(function(){

    $.ajax({
        type: 'POST',
        url: meddelanded.php,
        data: { 
            user: username,      // <-- is this what i should write in data?!
            message: message     // <-- and this?!
        },
        success: function(msg){                        
            alert('Message Sent');
        }
    });

    return false;
});

So, my problem is what I should write in the data:, and I have no clue what I'm supposed to type there! Can anybody help me, or is it something else that makes it not work?

  • Try putting your variables $meddelanden and $username directly into VALUES ('$meddelanden','$username'), leave out bind_param() – Brian Smith Feb 06 '17 at 16:32
  • @BrianSmith no then its possible to do mysql injections –  Feb 06 '17 at 16:33
  • You need to change your button so that it doesn't submit after the click: `` then wire up your ajax to it's click event. – freedomn-m Feb 06 '17 at 16:42
  • and how am i supposed to do that? @freedomn-m –  Feb 06 '17 at 16:45
  • use `e.preventDefault();` at the top of your function. – BizzyBob Feb 06 '17 at 16:54
  • add `e` as a pram: `$('#fromen2').submit(function(e)` – BizzyBob Feb 06 '17 at 16:54
  • tried that too but then the form is not getting submitted –  Feb 06 '17 at 16:58
  • Change `` to ` – freedomn-m Feb 06 '17 at 17:09
  • so the data is the same? o.o –  Feb 06 '17 at 17:11
  • @freedomn-m didn't work, when i submit im still getting redirected to meddelanden.php –  Feb 06 '17 at 17:13
  • @DolanDuck I think you are going to need to do some granular debugging. In your js, start adding console.log after each statement and see which ones appear in dev tools. Add additional callbacks for "done" and "fail" and place console.log statements in them. In your php, echo some text to return to the success function in js and then console.log it. Turn on error_reporting at the top of your .php script (error_reporting(E_ALL); ini_set('display_errors',1);) and see if an error gets returned to js. – tjfo Feb 06 '17 at 17:30

2 Answers2

2

Try something like this. You might have to mess around with it a bit, though. I first prevented the form from automatically submitting when you pressed the submit button, then I use the value entered by the user into message, and input the value for messageInput in the data. Hope this helps.

$('#fromen2').submit(function(e){
 e.preventDefault();
 var userMessage = $('#messageInput').val();

 $.ajax({
  type: 'POST',
  url: meddelanded.php,
  data: { 
    //You dont need to send user data becaues you are setting the user variable with $_SESSION in php file
    message: userMessage     
  },
   success: function(msg){


   alert('Message Sent');

      }
   });

return false;
});

HTML

<form id="fromen2" method="post">
        <input type="text" id = "messageInput" autocomplete="off" placeholder="type your chat message">
        <input class = "lg" type="submit" name="submit" value="Send">
    </form>

Alternatively, instead of using the .submit() as the event, you could add a function to an onclick event that will retrieve the data from the form and post the data with AJAX to your PHP script.

$('#submitButton').on('click', function(e){
e.preventDefault();
var userMessage = $('#messageInput').val();

$.ajax({
 type: 'POST',
 url: meddelanded.php,
 data: { 
   //You dont need to send user data becaues you are setting the user    variable with $_SESSION in php file
   message: userMessage     
 },
  success: function(msg){


  alert('Message Sent');

     }
  });
});

HTML

<form id="fromen2">
    <input type="text" id = "messageInput" autocomplete="off" placeholder="type your chat message">
    <input class = "lg" id="submitButton" name="submit" value="Send">
</form>
Brian Smith
  • 165
  • 1
  • 2
  • 14
  • nope didn't work, the message wont get submitted, even if i corrected that url typo –  Feb 06 '17 at 17:06
0

Just serialize form data using jQuery serialize:

$('#fromen2').submit(function(){

    $.ajax({
        type: 'POST',
        url: meddelanded.php,
        data: $(this).serialize()
        success: function(msg){

           alert('Message Sent');
        }
    });

    return false;
})

;

Bartek Fryzowicz
  • 6,464
  • 18
  • 27
  • that didn't work for me the form is still heading to meddelanded.php after submit –  Feb 06 '17 at 16:38
  • Please maker sure that your javascript code is added when form element is already in DOM . You can add `console.log(this)` inside submit callback to check if this code is executed at all when submit button is clicked. If for example you've put this js code in head it will not work – Bartek Fryzowicz Feb 06 '17 at 18:22