当我提交表单时,$_SESSION 总是变成 3,即使它不应该是。例如,如果我提交我的表单,下拉列表选择为 1,这应该使会话 = 1,但出于某种原因,它仍然返回 3。我不认为错误出在我的表单中,但我可能错了,我已经检查了我的 PHP 一千次,但找不到任何错误。此外,用户被插入到数据库中,但会话变量为 3,而不是实际的。我已经在 ajax 的表单文件中包含了 PHP 文件,也许这就是问题所在,但我不知道如何解决它。 P.S 抱歉,代码太长,我把它放进去是为了避免在评论中遇到不必要的问题。另外,这实际上是对本地主机的测试,所以我不关心 SQL 注入。
PHP
<?php
session_start();
$link = mysqli_connect("****", "****", "****", "****");
if(mysqli_connect_error()) {
die("Couldn't connect to the database. try again later.");
}
$query = "SELECT * FROM `users`";
if($result = mysqli_query($link, $query)) {
$row = mysqli_fetch_array($result);
}
$_SESSION['fa'] = "1";
if(isset($_POST['fa'])) {
$_SESSION['fa'] = "1";
}
if(isset($_POST['pa'])) {
$_SESSION['fa'] = "2";
}
if(isset($_POST['pra'])) {
$_SESSION['fa'] = "3";
}
$signupButton = "";
$username = "";
$password = "";
$termsandconditions = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(isset($_POST['username'])) {
$username = signupform_input($_POST["username"]); }
if(isset($_POST['password'])) {
$password = signupform_input($_POST["password"]); }
if(isset($_POST['signupButton'])) {
$signupButton = signupform_input($_POST["signupButton"]); }
if(isset($_POST['termsandconditions'])) {
$termsandconditions = signupform_input($_POST["termsandconditions"]); }
}
function signupform_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$usernameError = "";
$passwordError = "";
$termsandconditionsError = "";
$error = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["username"])) {
$usernameError = "Username is required.";
echo $usernameError;
} else {
$username = signupform_input($_POST["username"]);
}
if (empty($_POST["password"])) {
$passwordError = " Password is required.";
echo $passwordError;
} else {
$password = signupform_input($_POST["password"]);
}
if(isset($_POST['loginActive'])) {
if($_POST['loginActive'] == "0" && $usernameError == "" && $passwordError == "" && $termsandconditionsError == "") {
$query = "SELECT * FROM users WHERE username = '". mysqli_real_escape_string($link, $_POST['username'])."' LIMIT 1";
$result = mysqli_query($link, $query);
if(mysqli_num_rows($result) > 0) {
$error = "That username is already taken.";
echo $error;
} else {
echo "<p style='color: green'>Hi</p>";
$query = "INSERT INTO `users` (`username`, `password`, `plan`) VALUES ('". mysqli_real_escape_string($link, $_POST['username'])."', '". mysqli_real_escape_string($link, $_POST['password'])."', '". mysqli_real_escape_string($link, $_SESSION['fa'])."')";
mysqli_query($link, $query);
echo $query;
echo $_SESSION['fa'];
print_r($_POST);
}
}
}
}
?>
表单和 Ajax 和 Jquery
<h4>Account Type <i class="far fa-question-circle info" data-toggle="tooltip" data-placement="right" title="Whenever you have all free accounts checked, payment method should be hidden, but if not then click on any paid account and then click back!" height="16px"></i></h4>
<p>Visit <a href="pricing.php">Plan</a> for pricing details.</p>
<div class="input-group-prepend">
<button class="btn btn-outline-secondary dropdown-toggle dropdownB" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" name="playerAccount">Player Account</button>
<div class="dropdown-menu">
<input type="button" class="dropdown-item active dropdownI"value="Player: Free Account $0.00/Mo" class="FA" id="dfreePlayerAccount" name="fa">
<input type="button" class="dropdown-item dropdownI" value="Player: Pro Account $5.99/Mo" class="FA" id="dproPlayerAccount" name="pa">
<input class="dropdown-item dropdownI" value="Player: Premium Account $9.99/Mo" class="FA" id="dpremiumPlayerAccount" name="pra">
</div>
</div>
<br>
<input type="radio" id="freePlayerAccount" class="free playAccount readonly" name="account" value="1" checked>
<label for="freePlayerAccount">Player: Free Account $0.00/Mo</label><br>
<input type="radio" id="proPlayerAccount" class="paid playAccount readonly" name="account" value="2">
<label for="proPlayerAccount">Player: Pro Account $5.99/Mo</label><br>
<input type="radio" id="premiumPlayerAccount" class="paid playAccount readonly" name="account" value="3">
<label for="premiumPlayerAccount">Player: Premium Account $9.99/Mo</label><br>
<hr>
<div class="input-group-prepend">
<button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" name="dcreatorAccount">Creator Account(Optional)</button>
<div class="dropdown-menu">
<a class="dropdown-item" value="2" class="CA" name="pca" id="dproCreatorAccount">Creator: Pro Account $9.99/Mo</a>
<a class="dropdown-item" value="3" class="CA" id="dpremiumCreatorAccount" name="prca">Creator: Premium Account $14.99/Mo</a>
</div>
</div> <br>
<input type="radio" id="creatorProAccount" class="paid creatorAccount readonly" name="creatorAccount" value="5">
<label for="creatorProAccount">Creator: Pro Account $9.99/Mo</label><br>
<input type="radio" id="creatorPremiumAccount" class="paid creatorAccount readonly" name="creatorAccount" value="6">
<label for="creatorPremiumAccount">Creator: Premium Account $14.99/Mo</label><br>
<hr>
<div class="hiddenPaymentMethod"> <h5>Payment Method</h5>
<input type="radio" id="Paypal" name="payment" value="Paypal" class="payment">
<label for="Paypal">Paypal</label><br>
<input type="radio" id="creditCard" name="payment" value="CreditCard" class="payment">
<label for="creditCard">Credit Card</label><br>
<input type="text" style="display:none;" name="creditCardNumber" id="creditCardNumber" placeholder="Card Number">
<input type="radio" id="debitCard" name="payment" value="DebitCard" class="payment">
<label for="debitCard">Debit Card</label>
<input type="text" style="display:none;" name="debitCardNumber" id="debitCardNumber" placeholder="Card Number">
<br></div>
<br>
<input type="checkbox" id="termsAndConditions" class="conditions" name="termsandconditions" value="0">
<label for="termsAndConditions"> I have read and agreed to the Terms and Conditions <span data-toggle="modal" data-target="#exampleModal"><i class="far fa-question-circle questionMark"></i></span></label>
<p id="errors"></p>
<p id="tacError" style="color:red"></p>
<input type="button" class="btn btn-primary" name="signupButton" id="signUpButton" value="Submit">
<script type="text/javascript">
$("#signUpButton").click(function() {
$.ajax({
type: "POST",
url: "actionSignUp.php",
data: "username=" + $("#username").val() + "&password=" + $("#Password").val() + "&termsandconditions=" + $("#termsAndConditions").val() + $(".CA").val() + "&fa=" + $("#dfreePlayerAccount").val() + "&pa=" + $("#dproPlayerAccount").val() + "&pra=" + $("#dpremiumPlayerAccount").val() + "&pca=" + $("#dproCreatorAccount").val() + "&prca=" + $("#dpremiumCreatorAccount").val() + "&loginActive=" + $("#loginActive").val()
}).done(function(result) {
$("#errors").html(result);
}).fail(function(xhr, textStatus, errorThrown) {
alert("Error Requesting. Please Try Again Later.");
});
});
function myFunction(event) {
event.stopPropagation();
document.getElementById("dropdownI").classList.toggle("show");
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
document.getElementById("dropdownI").classList.remove("show");
}
$("#dproPlayerAccount").click(function(){
$("#dproPlayerAccount").css("background-color", "#157AF6");
$("#dproPlayerAccount").css("color", "white");
$("#dfreePlayerAccount").css("background-color", "white");
$("#dfreePlayerAccount").css("color", "black");
$("#dpremiumPlayerAccount").css("background-color", "white");
$("#dpremiumPlayerAccount").css("color", "black");
})
$("#dfreePlayerAccount").click(function(){
$("#dfreePlayerAccount").css("background-color", "#157AF6");
$("#dfreePlayerAccount").css("color", "white");
$("#dproPlayerAccount").css("background-color", "white");
$("#dproPlayerAccount").css("color", "black");
$("#dpremiumPlayerAccount").css("background-color", "white");
$("#dpremiumPlayerAccount").css("color", "black");
})
$("#dpremiumPlayerAccount").click(function(){
$("#dpremiumPlayerAccount").css("background-color", "#157AF6");
$("#dpremiumPlayerAccount").css("color", "white");
$("#dfreePlayerAccount").css("background-color", "white");
$("#dfreePlayerAccount").css("color", "black");
$("#dproPlayerAccount").css("background-color", "white");
$("#dproPlayerAccount").css("color", "black");
})
$("#dfreePlayerAccount").click(function(){
$("#freePlayerAccount").prop('checked', 'checked');
})
$("#dproPlayerAccount").click(function(){
$("#proPlayerAccount").prop('checked', 'checked');
})
$("#dpremiumPlayerAccount").click(function(){
$("#premiumPlayerAccount").prop('checked', 'checked');
})
$("#dfreeCreatorAccount").click(function(){
$("#creatorFreeAccount").prop('checked', 'checked');
})
$("#dproCreatorAccount").click(function(){
$("#creatorProAccount").prop('checked', 'checked');
})
$("#dpremiumCreatorAccount").click(function(){
$("#creatorPremiumAccount").prop('checked', 'checked');
})
$('input:checkbox').change(
function(){
if ($(this).is(':checked')) {
$(this).val("1");
} else {
$(this).val("0");
}
});
$("#signUpButton").click(function(){
if($(".conditions").val() == "0") {
$("#tacError").html("Terms and Conditions are required");
} else {
$("#tacError").html("");
}
})
$('#dproPlayerAccount').click(
function(){
$(".hiddenPaymentMethod").show();
$("#Paypal").attr('checked', 'checked');
});
$('#dpremiumPlayerAccount').click(
function(){
$(".hiddenPaymentMethod").show();
$("#Paypal").attr('checked', 'checked');
});
$('#dfreePlayerAccount').click(
function(){
$(".hiddenPaymentMethod").hide();
$("#Paypal").removeAttr('checked');
});
$('#dpremiumCreatorAccount').click(
function(){
$(".hiddenPaymentMethod").show();
$("#Paypal").attr('checked', 'checked');
});
$('#dproCreatorAccount').click(
function(){
$(".hiddenPaymentMethod").show();
$("#Paypal").attr('checked', 'checked');
});
$('input:radio[name="payment"]').change(
function(){
if ($(this).is(':checked') && $(this).val() == 'CreditCard') {
$("#creditCardNumber").show();
}
});
$('input:radio[name="payment"]').change(
function(){
if ($(this).is(':checked') && $(this).val() == 'Paypal' || $(this).val() == 'DebitCard') {
$("#creditCardNumber").hide();
}
});
$('input:radio[name="payment"]').change(
function(){
if ($(this).is(':checked') && $(this).val() == 'DebitCard') {
$("#debitCardNumber").show();
}
});
$('input:radio[name="payment"]').change(
function(){
if ($(this).is(':checked') && $(this).val() == 'Paypal' || $(this).val() == 'CreditCard') {
$("#debitCardNumber").hide();
}
});
$(function () {
$('[data-toggle="tooltip"]').tooltip();
})
</script>