javascript - 提交时无法从 HTML 表单获取 jQuery 中的值
问题描述
我还在做一些事情,我几乎已经完成了。我遇到的唯一问题是在我的 jQuery 代码中它不会将值发送到 php 页面。例如:它应该放“?whatever=whatever&soon=soon”,但它没有。它留空。
我尝试过更改代码,例如更改 JS id,并完全更改代码。
$(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");
}
}
我希望它能够输入我的值(到我的 MySQL 表中),但它们都是空白的。
这是我的表格。(当点击 chatSend 时,它会执行 jQuery 函数。)
<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>
另外,这是我现在考虑的上传表单(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())");
解决方案
您需要像使用 chatsend 一样使用正确的标识符:
//# = ID
var $userName = $("#username");
var $text = $("#text");
var $title = $("#title");
推荐阅读
- python - python - 如何从 azurewebsites 访问 API
- html - 什么是数据电子邮件,它用作标签中的属性?
- php - PHP将当前时间与数组中的时间进行比较并返回相关值
- java - Microsoft Oauth2 访问令牌和刷新令牌的存储
- python - Python:如何删除其他数据框中的行?
- php - Yii 2 Kartik 输入验证消息仅在单击浏览按钮时出现
- shell - 在声明性管道内的 shell 脚本中传递 Jenkins 参数值时出现错误替换错误
- qt - 关于 QScreen::grabWindow() 崩溃
- qt - 在 QML 中使用画布绘制圆和连接圆的 3 条线
- java - 如何测试需要 PDF 多部分文件的 Post MVC 端点?