我尝试通过javascript和xmlhttprequest验证表单,但它无法正常工作。 我的onload功能应该弹出一个警报,有时它会工作,警报会弹出,有时不会。我真的不明白我的错误。
document.getElementById("button").addEventListener("click", function(){
validateForm(document.myForm);
});
function validateForm(form) {
if (form.FirstName.value.match(/^([A-Za-z ]+)$/) &&
form.Surname.value.match(/^([A-Za-z ]+)$/) &&
form.Verein.value.match(/^([A-Za-z ]+)$/) &&
form.Headcoach.value.match(/^([A-Za-z ]+)$/) &&
form.Assistantcoach.value.match(/^([A-Za-z ]+)$/) &&
form.Rueckennummer.value.match(/^([0-9])$/) &&
form.Rueckennummer.value > 3 &&
form.Rueckennummer.value < 16 &&
(document.getElementById("Aktiv1").checked || document.getElementById("Aktiv2").checked) &&
(new Date(document.getElementById("idDate").value).getFullYear() <= new Date().getFullYear()) ) {
senden(form);
}else{
alert("Einige Eingaben sind fehlerhaft. Bitte ueberpruefen Sie ihre
Eingaben.");
}
}
function senden(form){
var formData = new FormData(form);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://188.166.165.74:13337/api/players', true);
xhr.responseType = 'json';
xhr.onload = function() {
alert("Daten wurden erfolgreich uebermittelt")
};
xhr.send(formData);
}
答案 0 :(得分:1)
尝试使用 <?php
// database connection info
$conn = @mysql_connect('localhost','root','') or die(mysql_error());
$db = mysql_select_db('friends',$conn) or die(mysql_error());
// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM user";
$result = mysql_query($sql, $conn) or die(mysql_error());
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 1;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
// get the info from the db
$sql = "SELECT name, surname, score FROM user LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or die(mysql_error());
?>
<TABLE BORDER="3" CELLPADDING="10" CELLSPACING="10">
<TD>
<table border="0" cellpadding="4">
<tr>
<td bgcolor="#CCCCCC"><strong>Name</strong></td>
<td bgcolor="#CCCCCC"><strong>Surname</strong></td>
<td bgcolor="#CCCCCC"><strong>score</strong></td>
</tr>
<?php
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['surname']."</td>";
echo "<td>".$row['score']."</td>";
echo "</tr>";
}
?>
</table>
<?php
/****** build the pagination links ******/
// range of num links to show
$range = 1;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage' class=previous>Previous</a> ";
$prev_button = (isset($prev_id) && $prev_id>0)?'<a href="#" data-id="'.$prev_id.'" class="get_pic"><img src="prev.png" border="0" /></a>':'';
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage' class=next>next</a> ";
// echo forward link for lastpage
} // end if
/****** end build pagination links ******/
?>
代替onreadystatechange
...
onload