回显输出到控制台而不是HTML

时间:2019-02-26 03:41:41

标签: php sql database mysqli

我的问题是这些值已成功插入到我的数据库中,但是我对“成功”的回声不起作用。有人可以帮忙吗?

$conn =  new mysqli($db_host, $db_username, $db_pass, $db_name);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$pw = $_POST['pw'];
$cpw = $_POST['cpw'];

if($pw !== $cpw) {
die("Passwords do not match");
}


$stmt = $conn->prepare("INSERT INTO snpdata (Username, Password, Email, 
Gender) VALUES (?, ?, ?, ?)");

if(!$stmt->bind_param("ssss", $sname, $clean_pw, $clean_em, $gen)) {
    die("Error binding parameters");
} else {}

$gen = $_POST['gender'];
$sname = $_POST['sname'];
$em = $_POST['em'];

$clean_pw = password_hash($pw, PASSWORD_BCRYPT);
$clean_em = password_hash($em, PASSWORD_BCRYPT);

if(!$stmt->execute()) {
    die ('Error executing' . $stmt->error);
} else { 
    echo '<h3 color="green"> SUCCESS </h3>';
}

$stmt->close();
$conn->close();

我已经尝试研究如何回显html,但是产生的结果与我在这段代码中所得到的结果相同。

编辑:很抱歉,不清楚,问题是回声根本没有出现在页面上。

这是AJAX,它工作正常。

<script>
function formSubmit() {
$.ajax({
type:'POST',
url: 'snp.php',
data: $('#fm').serialize(),
success:function(response) {
$('#success').html(response);
}

});
var form = document.getElementById('fm').reset()
return false;
}
</script>

这是我的表单代码:

<form id='fm' action="/snp.php" method="post" onsubmit="return formSubmit()">
<div class='snpcontent'>
<h1 style='color: #6e727a; font-weight: 100'> Sign Up Here </h1>
<label style='font-weight: 900'>Username</label>
<input type='text' placeholder='Enter Username' name='sname' required>
<label style='font-weight: 900'>Password</label>
<input type='password' placeholder='Enter Password' name='pw' id="pw" required>
<button onmousedown="document.getElementById('pw').type = 'text' " onmouseup="document.getElementById('pw').type = 'password'" type='button' class="show">Show Password </button>
<label style='font-weight: 900'>Confirm PW</label>
<input type='password' placeholder='Enter Password' name='cpw' id="cpw" required>
<button onmousedown="document.getElementById('cpw').type = 'text' " onmouseup="document.getElementById('cpw').type = 'password'" type='button' class="show">Show Password </button>
<label style='font-weight: 900'> Email </label>
<input type='text' placeholder='Enter Email' name='em' required><br>
<input type='radio' name='gender' value='male' required> Male<br>
<input type='radio' name='gender' value='female' required> Female<br>
<input type='radio' name='gender' value='other' required> Other<br>
<button type='submit' style='background-color: #19ad11; border-color: #19ad11; color: white; font-family: Georgia, verdana; width: 50%; height: 50px; font-size: 20px'> Sign Up </button>
</div>
</form>

1 个答案:

答案 0 :(得分:0)

糟糕,我刚刚解决了我的问题。 在我的AJAX中,有一行代码表明

$('#success').html(response);

原来是我的

<h3> </h3>

ID为“成功”而不是“成功”,这是导致错误的原因。 感谢您的所有帮助!