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");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改