這是我的Ajax代碼從另一個頁面獲取$ username變量:我想用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
並將其分配給$user_posted_to
變量在senpost.php
頁面,並插入到數據庫使用JavaScript,如果任何專家可以幫助將真正感激它,我已經嘗試this.attr()
但那不工作也也無法正常工作我無法從該頁面獲取用戶名可以任何人都可以幫助我這個我想要獲取的用戶名變量是我想要分配的用戶個人資料的用戶名$user_poste_to
請點擊「發送」的psot HTML – mplungjan
Ok I將在一段時間後發佈它 –