doubu1853 2019-01-27 03:49
浏览 135
已采纳

无法在提交时从HTML表单获取jQuery中的值

I'm still working on things and I've almost got it done. The only issue I've got is in my jQuery code it don't send values to a php page. Ex: it should put "?whatever=whatever&soon=soon", but it don't. It leaves it blank.

I've tried changing code such as changing JS ids, and changing the code completely.

$(document).ready(function() {
var $userName = $("username");
var $text = $("text");
var $title = $("title");
var $chatSend = $("#chatSend");

function secret() {
    var userNameString = $userName.val();
    var textString = $text.val();
    var titleString = $title.val();

    $.get("./upload.php", {
        username: userNameString,
        text: textString,
        title: titleString
    });

    window.location.replace("success.php");

    }
}  

I expected it to put in my values (to my MySQL table), but instead they're all blank.

Here's my form. (When clicked on chatSend, it does the jQuery functions.)

<div>
<input type="text" id="text" name="text">
                <input type="text" id="title" name="title">
                <input type="text" id="username" name="username" hidden value="<?php echo htmlspecialchars($_SESSION['username']); ?>">
                <button id="chatSend">Send</button>
</div>

Also, here's the upload form now that I think about it (upload.php):

$user = $_GET['username'];

$title = $_GET['title'];

$text = $_GET['text'];

$query = mysqli_query($con, "INSERT INTO `blog` (username, title, text, view) VALUES ('$user', '$title', '$text', UUID())");
  • 写回答

1条回答 默认 最新

  • dslkchyv673627 2019-01-27 04:03
    关注

    You need to use the proper identifier like you have with chatsend:

    //# = ID
    var $userName = $("#username");
    var $text = $("#text");
    var $title = $("#title");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?