美好的一天
请帮助我,我正在努力弄清楚为什么这个脚本不起作用,所有项目都通过除了accountM
PHP First Page Snippet:
<body>
<div class="form">
<form name="registration" action="" id="register_form" method="post" autocomplete="new-password">
<input type="text" name="Name" placeholder="Name" id="Name" autocomplete="new-password" required />
<input type="text" name="Surname" placeholder="Surname" id="Surname" autocomplete="new-password" required />
<input type="email" name="Email" placeholder="Email" id="Email" autocomplete="new-password" required />
<!-- The text and password here are to prevent FF from auto filling my login credentials because it ignores autocomplete="off" -->
<input type="text" style="display:none"/>
<input type="password" style="display:none"/>
<input type="password" name="Password" placeholder="Password" id="Password" autocomplete="new-password" required />
<div class="select">
<select name="Server" id="Server" onchange="server(this.value)">
<option value="">Select Server</option>
<option value="1">ZA02</option>
<option value="2">ZA04</option>
<option value="3">ZA05</option>
<?php
$getServer = $con->query("SELECT `Server`, `Name` FROM users.customers where `Server ID` = '0';");
$gsResult = mysqli_fetch_all($getServer);
for ($i = 0; $i < mysqli_num_rows($getServer); $i++) {
echo "
<option value=".$gsResult[$i][0].">".$gsResult[$i][1]."</option>
";
} ?>
</select>
<div class="select_arrow"></div>
</div>
<div class="select">
<select id='customer' name='Company'>
<option value=''>Select Customer</option>
</select>
<div class="select_arrow"></div>
</div>
<div class="select">
<select id="accountM" name="accountM">
<option value="">Select AM</option>
<option value="Tyron Lecki">Tyron Lecki</option>
<option value="Kaamilah Achmat">Kaamilah Achmat</option>
<option value="Siddharth Bawa">Siddharth Bawa</option>
</select>
<div class="select_arrow"></div>
</div>
<br>
<br>
<div syle="padding-bottom: 10px;">
<label for="cbox" style="color: #2f2f2f; float: left; margin-top: 10px; padding-right: 10px" >Premium</label>
<input id="cbox" type="checkbox" name="premium" id="Premium" value="yes"/>
</div>
<input type="button" id="submit" name="submit" value="Register" />
<br>
<div id="loading">
<p></p>
<p style="margin-left: 15px">Please Wait...</p>
<img src="images/hrs2.gif"/>
</div>
</form>
</div>
使用Javascript:
$(document).ready(function() {
$("#submit").click(function() {
// To Display progress bar
$("#loading").show();
var name = $("#Name").val();
var surname = $("#Surname").val();
var email = $("#Email").val();
var password = $("#Password").val();
var company = $("#customer").val();
var server = $("#Server").val();
var premium = $("#Premium").val();
var accountM = $("#accountM").val();
// Transfering form information to different page without page refresh
$.post("register.php", {
name: name,
surname: surname,
email: email,
password: password,
company: company,
server: server,
accountM: accountM,
premium: premium
}, function(status) {
$("#loading").hide(); // To Hide progress bar
alert(status);
});
});
});
PHP Second Page Snippet:
if (($_POST['name']=='')
||($_POST['surname']=='')
||($_POST['email']=='')
||($_POST['password']=='')
||($_POST['company']=='')
||($_POST['server']=='')
||($_POST['accountM']=='')) {
echo "Please fill in all fields";
} else {
// some other code
}
使用此功能时,我不断收到“请填写所有字段”,我发现只有accountM变量没有传递给第二个脚本
编辑:
这是AJAX脚本:
function server(str) {
if (str == "") {
document.getElementById("customer").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("customer").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","customer.php?q="+str,true);
xmlhttp.send();
}
}
customer.php:
<?php
include("db.php");
$q = intval($_GET['q']);
$result = $con->query("SELECT * from users.customers where `Server ID` = '".$q."' order by Name");
echo "<option value=''>Select Customer</option>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . htmlentities($row['Name'], ENT_QUOTES) . "'>"
. htmlentities($row['Name'], ENT_QUOTES)
. "</option>";
}
mysqli_close($con);
答案 0 :(得分:1)
完成上述所有评论后,似乎server()
功能会损坏DOM,然后您的<select id='customer'>
标记不再有效且无法发送。尝试使用htmlentities()
保护PHP发送的HTML:
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . htmlentities($row['Name'], ENT_QUOTES) . "'>"
. htmlentities($row['Name'], ENT_QUOTES)
. "</option>";
}
试试这个:
$(document).ready(function() {
$("#submit").click(function() {
// To Display progress bar
$("#loading").show();
var name = $("#Name").val();
var surname = $("#Surname").val();
var email = $("#Email").val();
var password = $("#Password").val();
var company = $("#customer").val();
var server = $("#Server").val();
var premium = $("#Premium").val();
var accountM = $("#accountM").val();
var postobject = {
name: name,
surname: surname,
email: email,
password: password,
company: company,
server: server,
accountM: accountM,
premium: premium
};
console.log(postobject);
// Transfering form information to different page without page refresh
$.post("register.php", postobject, function(status) {
$("#loading").hide(); // To Hide progress bar
alert(status);
});
});
});
答案 1 :(得分:1)
症状表明选项值是导致问题的原因,因为它们没有被引用和/或没有转换为html实体,这导致了你的dom。我认为有两个地方会出现问题:
以您的形式:
<option value="">Select Server</option>
<option value="1">ZA02</option>
<option value="2">ZA04</option>
<option value="3">ZA05</option>
<?php
if($result=$con->query("SELECT `Server`,`Name` FROM users.customers WHERE `Server ID`=0;")){
while($row=$result->fetch_assoc()){
echo "<option value=\"",htmlentities($row['Server'],ENT_COMPAT),"\">",htmlentities($row['Name'],ENT_COMPAT),"</option>";
}
}else{
echo "<option>ERROR</option>"; // just for the sake of debugging
}
?>
在您的customer.php文件中:(如果value
与text
相同,请忽略选项标记中的value属性 - 它是多余的)
$q = intval($_GET['q']);
if($result=$con->query("SELECT `Name` from users.customers WHERE `Server ID`=$q ORDER BY `Name`"){
echo "<option value=''>Select Customer</option>";
while($row=$result->fetch_assoc()){
echo "<option>",htmlentities($row['Name'],ENT_COMPAT),"</option>";
}
}else{
echo "<option>ERROR</option>"; // just for the sake of debugging
}
我完全未经测试的建议
@your form
<body>
<div class="form">
<form id="register" autocomplete="new-password">
<input type="text" name="Name" placeholder="Name" id="Name" autocomplete="new-password" required />
<input type="text" name="Surname" placeholder="Surname" id="Surname" autocomplete="new-password" required />
<input type="email" name="Email" placeholder="Email" id="Email" autocomplete="new-password" required />
<!-- The text and password here are to prevent FF from auto filling my login credentials because it ignores autocomplete="off" -->
<input type="text" style="display:none"/>
<input type="password" style="display:none"/>
<input type="password" name="Password" placeholder="Password" id="Password" autocomplete="new-password" required />
<div class="select">
<select name="Server" id="Server">
<option value="">Select Server</option>
<option value="1">ZA02</option>
<option value="2">ZA04</option>
<option value="3">ZA05</option>
<?php
if($result=$con->query("SELECT `Server`,`Name` FROM users.customers WHERE `Server ID`=0;")){
while($row=$result->fetch_assoc()){
echo "<option value=\"",htmlentities($row['Server'],ENT_COMPAT),"\">",htmlentities($row['Name'],ENT_COMPAT),"</option>";
}
}else{
echo "<option>ERROR</option>"; // just for the sake of debugging
}
?>
</select>
<div class="select_arrow"></div>
</div>
<div class="select">
<select id="Company" name="Company">
<option value=''>Select Customer</option>
</select>
<div class="select_arrow"></div>
</div>
<div class="select">
<select id="AccountM" name="AccountM">
<option value="">Select AM</option>
<option value="Tyron Lecki">Tyron Lecki</option>
<option value="Kaamilah Achmat">Kaamilah Achmat</option>
<option value="Siddharth Bawa">Siddharth Bawa</option>
</select>
<div class="select_arrow"></div>
</div>
<br><br>
<div syle="padding-bottom: 10px;">
<label for="cbox" style="color: #2f2f2f; float: left; margin-top: 10px; padding-right: 10px" >Premium</label>
<input id="cbox" type="checkbox" name="Premium" id="Premium" value="yes"/>
</div>
<input type="button" id="Submit" value="Register" />
<br>
<div id="status"></div>
</form>
</div>
</body>
@ your.js文件
$(document).ready(function(){
$("#Server").change(function(){
$("#status").html("<p></p><p style=\"margin-left:15px\">Please Wait...</p><img src=\"images/hrs2.gif\"/>");
$.get("customer.php", {q:this.value}, function(response){
$("#Company").html(response);
$("#status").html("");
});
});
$("#Submit").click(function(){
e.preventDefault();
$("#status").html("<p></p><p style=\"margin-left:15px\">Please Wait...</p><img src=\"images/hrs2.gif\"/>");
$.post('register.php', $(this).serialize(), function(response){
$("#status").html(response);
});
});
});
@ customer.php
<?php
include("db.php");
if(empty($_GET['q'])){
echo "<option value=''>Select Population Error</option>";
}else{
if($result=$con->query("SELECT `Name` from users.customers WHERE `Server ID`=".intval($_GET['q'])." ORDER BY `Name`"){
echo "<option value=''>Select Customer</option>";
while($row=$result->fetch_assoc()){
echo "<option>",htmlentities($row['Name'],ENT_COMPAT),"</option>";
}
}
?>
@ register.php
<?php
echo "<pre>";
var_export($_POST);
echo "</pre>";
if(empty($_POST['Name']) || empty($_POST['Surname']) || empty($_POST['Email']) || empty($_POST['Password']) || empty($_POST['Server']) || empty($_POST['Company']) || empty($_POST['AccountM']) || empty($_POST['Premium'])){
echo "<div>Whoops! Something's missing!</div>";
}else{
echo "<div>FINALLY!</div>";
}