嘿嘿嘿!
我遇到了这个问题,由于某种原因,它不会将数据添加到我的MYSQL数据库中,我不知道为什么。我在StackOverflow上看了很多其他帖子,但似乎找不到对我有帮助的帖子:)
这是我的代码:
<script>
$('.alert-saved-changes-success2').hide();
$(".save-tradelink-profile").click(function() {
$tradelinkvalue = document.getElementById("tradelink").value;
if ($.trim($('#tradelink').val()) == '') {
alert('Tradelink can not be blank');
} else if ($tradelinkvalue.indexOf("https://steamcommunity.com/tradeoffer/new") >= 0) {
<?php //TRYING TO UPDATE DATA
$TradelinkValue = $_POST['GetTradelinkValue'];
mysql_query("UPDATE item-jackpot-users SET tradelink=$TradelinkValue WHERE steam_id=$steamid") or die(mysql_error());
?>
$("#alert-saved-changes-success").slideDown("slow");
} else {
alert('Tradelink has to be valid');
}
});
$(".logout-button-profile").click(function(){
window.location.href = "steamauth/logout.php";
});
</script>
php标签似乎比其他标签先加载,因为
$('.alert-saved-changes-success2').hide();
未加载。它发现了错误,然后就扼杀了其他一切。
考虑到php是在你的网络浏览器接收到js文本之前执行的,你可以使用一个表单,将你的更新信息发送到post中(比如使用表单标签),重新加载页面,并让页面用你的post数据执行更新。您也可以使用ajax。
<form name="login" class="form-horizontal" method="post" action="refer to this file" >
<div class="form-group">
<label class="col-sm-3 control-label">
Username
</label>
<div class="col-sm-2">
<input class="pull-right" type="text" name="username" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">
Password
</label>
<div class="col-sm-2">
<input class="pull-right" type="password" name="password" />
</div>
</div>
<div class="form-group">
<div class=" col-sm-offset-3 col-sm-2">
<button type="submit" name="submitbtn" class="btn btn-default pull-right">
Sign in
</button>
</div>
</div>
</form>
<?php
$user = $_POST['username'];
$pwd = $_POST['password'];
//DO SOME SANITIZATION!
//Store $user and $pwd
?>
当然,您可以在文件的其他地方包括php部分,比如在脚本标记中。