【发布时间】:2017-09-13 05:10:39
【问题描述】:
这是我的 ajax 代码:
$(document).ready (function() {
$("#send").click (function() {
var username = $(this).val();
alert (username);
$.ajax({
url : "sendpost.php",
type: 'POST',
async: false,
data: {username},
success: function(datat){}
})
});
});
这是我的 php 代码
include ('connection_socio.php');
if(isset($_POST['body']))
{
$body=mysqli_real_escape_string($db, $_POST['body']);
$date_added="123";
$added_by="123";
$username = mysqli_real_escape_string($db, $_POST['username']);
$check = "SELECT * FROM users";
$result_profile = mysqli_query($db, $check);
//check whether the no of rows in users table is more than 0
//get username from database
$getusername = mysqli_fetch_assoc($result_profile);
$user_posted_to = $username;
echo $user_posted_to;
$query_post = "INSERT INTO posts VALUES ('', '$user_poste_to',)";
mysqli_query($db, $query_post);
echo "POSTED SUUCESSFULLY";
}
这是我的 profile.php 代码:
if (isset($_GET['u']))
{
$username = mysqli_real_escape_string($db, $_GET['u']);
//checks and remove symbols like # , ' etc
if(ctype_alnum($username))
{
//check user existance
$check = "SELECT * FROM users WHERE username='$username'";
$result_profile = mysqli_query($db, $check);
//check whether the no of rows in users table is more than 0
if(mysqli_num_rows($result_profile) == 1)
{
//get username from database
$getusername = mysqli_fetch_assoc($result_profile);
$username = $getusername['username'];
}
else
{
echo "User dont exist";
exit();
}
}
}
我想要的是从profile.php 页面中获取变量$username 并将其分配给senpost.php 页面中的$user_posted_to 变量,然后使用javascript 插入到数据库中,如果有任何专家可以提供帮助,我会非常感激我尝试过@ 987654328@ 但那也不起作用 this.value 也不起作用 我无法从该页面获取用户名 谁能帮我解决这个问题 我要获取的用户名变量是我要分配给的用户配置文件的用户名$user_poste_to
【问题讨论】:
-
请在您点击“发送”的地方修改 HTML
-
好的,我一会儿贴出来
标签: javascript php mysql ajax fetch