无法在提交时从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())");
我还在处理事情,我差不多完成了。 我唯一的问题是在我的jQuery代码中它不会将值发送到php页面。 例如:它应该放“不管怎么样?很快就会很快”,但事实并非如此。 它留空了。 p>
我尝试更改代码,例如更改JS ID,以及完全更改代码。 p>
$( 文件).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”,{
用户名:userNameString,
text:textString,
title:titleString
});
window.location.replace (“success.php”);
}
}
code> pre>
我希望它放入我的值(到我的MySQL表),而是 它们都是空白的。 p>
这是我的表格。(当点击chatSend时,它会执行jQuery函数。) p>
&lt; div&gt;
&lt; input type =“text”id =“text”name =“text”&gt;
&lt; input type =“text”id =“title”name =“title”&gt;
&lt; input type =“text”id =“username”nam e =“username”hidden value =“&lt;?php echo htmlspecialchars($ _ SESSION ['username']); ?&gt;“&gt;
&lt; button id =”chatSend“&gt;发送&lt; /按钮&gt;
&lt; / div&gt;
code> pre>
此外,这是 现在上传表格我想到了它(upload.php): p>
$ user = $ _GET ['username'];
$ title = $ _GET [' title'];
$ text = $ _GET ['text'];
$ query = mysqli_query($ con,“INSERT INTO`blog`(用户名,标题,文字,视图)VALUES('$ user','$ title','$ text',UUID())“);
code> pre>
div>
You need to use the proper identifier like you have with chatsend:
//# = ID
var $userName = $("#username");
var $text = $("#text");
var $title = $("#title");