gpt4 book ai didi

Javascript 不向 php 发布信息

转载 作者:行者123 更新时间:2023-12-01 02:24:26 25 4
gpt4 key购买 nike

美好的一天

请帮助我,我正在努力找出为什么这个脚本不起作用,除了 accountM 之外的所有项目都通过

PHP 首页片段:

<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 第二页片段:

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();
}
}

客户.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);

最佳答案

经过上述所有评论,似乎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);
});
});
});

关于Javascript 不向 php 发布信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48924375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com