javascript - 为什么这个post方法没有被检索到?

标签 javascript php

目前正在为我的网站开发消息系统。我创建了一个 JavaScript 函数来发送发布数据,并创建了一个相应的 PHP 文件来插入数据。但是,数据并未发送到数据库。我不确定错误是在 JavaScript 还是 PHP 文件中,因为没有创建错误日志。

HTML:

<form action="javascript:sendPM();" name="pmForm" id="pmForm" method="post">
   <input name="pm_send_id" id="pm_send_id" type="hidden" value="<?php echo $_SESSION['userID']; ?>" />
   <input name="pm_send_name" id="pm_send_name" type="hidden" value="<?php echo $_SESSION['userName']; ?>" />
   <input name="pm_receive_id" id="pm_receive_id" type="hidden" value="<?php echo $row['userID']; ?>" />
   <input name="pm_receive_name" id="pm_receive_name" type="hidden" value="<?php echo $row['userName']; ?>" />               
   <a href="#close" title="Close" class="close">X</a>
   <h4>Send to <?php echo $row['userName']; ?></h4>
   <div class="sectionheader"></div>
   <div id="interaction"></div>
   <br>
   <p>Comment:</p>
   <textarea name="pmTextArea" id="pmTextArea"></textarea>
   <p>Select Video:</p>
   <input name="pmSubmit" type="submit" value="Submit" />
</form>

JavaScript:

$('#pmForm').submit(function(){$('input[type=submit]', this).attr('disabled','disabled');});
  function sendPM(){
    var pmTextArea = $("pmTextArea");
    var sendName = $("pm_send_name");
    var sendID = $("pm_send_id");
    var receiveName = $("pm_receive_name");
    var receiveID = $("pm_receive_id");
    var url = "messages.php";
    if (pmTextArea.val() == ""){
      $("#interaction").html('Comment field is empty.').show().fadeOut(5000);
    }
    else {
    $.post(url,{ message: pmTextArea.val(), sendername: sendName.val(), senderid: sendID.val(), recname: receiveName.val(), recID: receiveID.val() },    function(data){
      $("#interaction").html(data).show().fadeOut(5000);
      document.pmForm.pmTextArea value='';
      });
    }
  }

PHP:

<?php
session_start();
require_once 'class.channel.php';
require_once 'dbconfig.php';
$user_message = new USER();

if (isset($_POST['message'])) {
  $to = ($_POST['recID']);
  $from = ($_POST['senderid']);
  $toName = ($_POST['sendername']);
  $fromName = ($_POST['recname']);
  $msg = htmlspecialchars($_POST['message']);

  $stmt = $user_message->runQuery("INSERT INTO inbox(send_id, receive_id, timesent, comment) VALUES(?, ?, ?, ?)");
  $stmt->bindValue(1,$from);
  $stmt->bindValue(2,$to);
  $stmt->bindValue(3,now());
  $stmt->bindValue(4,$msg);
  $stmt->execute();
}
?>

最佳答案

您需要使用 # 来使用您缺少的 id 来检索值。

var pmTextArea = $("pmTextArea");

应该是

var pmTextArea = $("#pmTextArea");

是的,你需要纠正,@RyanHame 指出

document.pmForm.pmTextArea value='';

document.pmForm.pmTextArea.value=''; 

关于javascript - 为什么这个post方法没有被检索到?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46312680/

相关文章:

php - 将桶中的项目(或具有属性)均匀分布到属性后的另一个桶(或数组)

php - 模型映射器,抽象公共(public)代码的最佳方式是什么,即帮助器、装饰器

php - 复选框不会在插件设置页面上保持选中状态

php - IE9L自动将文档模式更改为IE7

javascript - 如何将小数点移动到整数的 100 位?

javascript - 如何在 Android 的 React Native 中以编程方式打开 Tez 应用程序(印度的 Google Pay 应用程序)?

php - Jquery 通过 .load 传递数据

javascript - 在什么情况下 Enumerator 对象 (IE) 和 Iterator 对象 (Fx) 真正有用?

javascript - 使用javascript计算组合框中的所有值

javascript - Highchart - 仅更改一个 x 轴标签的颜色