将变量从javascript通过ajax传递给php

时间:2017-04-15 04:58:10

标签: php ajax post

我是jquery和ajax的新手。我在这里有一个脚本,我需要将变量传递给php文件。那个php将被编码为div#chat-body。我试图通过POST将接收器变量传递给load-messages.php,但是我收到以下错误:“未定义的索引:第8行的xxxx / scripts / load_messages.php中的接收器”。我认为我的语法有问题或者我完全错了。

的script.js

$('input#send-message').on('click', function(){
alert("test");
var message = $('input#input-message').val();
var sender= $('input#sender').val();
var receiver= $('input#receiver').val();

    if($.trim(message)!=''){
        $.post('scripts/messaging.php', {message: message, sender: sender, receiver:receiver}, function(data){
        //output after sending message  

        });     
        //load message to  chat-body div

        $.ajax({
        url: 'scripts/load_messages.php',
        type: "POST",
        data: {receiver: receiver},
        success: function(data){
            $('#chat-body').html(data);
            //$('#chat-body').scrollTop($('#chat-body')[0].scrollHeight); 
        }
    });
    }});

负载messages.php

 <?php 
    session_start();
    require('config.php');
    require('chat_functions.php');


$messages = get_msg($_SESSION['user_id'], $_POST['receiver']);

foreach($messages as $message){
    if($message['sender'] == $_SESSION['user_id'])  {
        ?><div id = "you_message">

        <?php echo '<strong> You: </strong><br />';
        echo $message['message'].'<br /><br />';?>
        </div><!--you_message-->
        <?php   
    }

    else{
        ?><div id="recipient_message">
        <?php echo '<strong>'.get_name($_POST['receiver']).'</strong><br />';
        echo $message['message'].'<br /><br />';?>
        </div> <!--recipient_message -->
        <?php
        }
    }

    ?>

3 个答案:

答案 0 :(得分:0)

您传递的是对象,而不是JSON字符串:

$.ajax({
  type: 'POST',
  url: 'scripts/messaging.php',
  data: JSON.stringify ({receiver: receiver}),
  success: function(data) { alert('data: ' + data); },
  contentType: "application/json",
  dataType: 'json'
});

答案 1 :(得分:0)

你可以试试这个然后根据你的需要调整它,因为我只是把它变得更“基本”:

你的表格:

<form action="#" id="form" method="post">
<input type="text" id="sender" name="sender" />
<input type="text" id="receiver" name="receiver" />
<input type="text" id="input-message" name="input-message" />
<input type="submit" id="send-message" value="Post" />
</form>

<div id="chat-body" class="regular"></div>

jQuery部分:

$(document).ready(function(){

$("#send-message").click(function(e){
e.preventDefault(); /* to prevent form default action */

var message = $('#input-message').val();
var sender = $('#sender').val();
var receiver = $('#receiver').val();

    $.ajax({
    url: "load_messages.php",
    method: "POST",
    data: { message: message, sender: sender, receiver:receiver },
    success: function(html){
        alert(html); /* checking response */
        $('#chat-body').html(html); /* add to div chat */
    }
  });
 });
});

然后,load_message.php

<?php

error_reporting(E_ALL); ini_set('display_errors', 1);

$sender = $_POST['sender'];
$receiver = $_POST['receiver'];
$message = $_POST['message'];

echo"[ $sender / $receiver / $message ]"; /* here, you can only echo $message for instance, 
then use response to append message to chat window */

?>

答案 2 :(得分:0)

通过AJAX调用将值传递给php文件非常简单。 更改您的AJAX调用,如下所示

var message = $('#input-message').val();
var sender= $('#sender').val();
var receiver= $('#receiver').val(); 
$.ajax({
    url: "scripts/load_messages.php", 
    method: "post", 
    //data: { "message":$('#input-message').val(),"sender":$('#sender').val(),"receiver":$('#receiver').val()},you can pass the values directly like this or else you can store it in variables and can pass
    data: { "message":message,"sender":sender,"receiver":receiver},
    success: function(data){
    $('#chat-body').html(data);
    },
     error: function() {
    alert('Not OKay');
    } 
   });

并且你的load-messages.php可能就像这样。

$receiver = $_POST['receiver'];
echo $receiver;