我有一个简单的页面,允许用户向项目添加语言 ID。该页面通过执行 ajax 调用来加载记录列表。所有记录都作为列表加载到 div 中。
每个列表项都是一个表单,因此当用户输入 lang id 时,他们按下 update 并且 ajax 调用关闭,更新数据库并使用记录重新加载列表,减去刚刚更新的记录。
jquery 调用适用于页面加载,但如果您提交表单项,它会转到 php 脚本,而不是在您提交的页面中重新加载。
任何想法为什么初始调用加载“页面内”但表单提交没有。使用每条记录的表单也是一个好主意,或者是否有更好的方法来使用 jquery。
执行 ajax 调用的原始 php 脚本 (sort_language.php)
<script id="source" language="javascript" type="text/javascript">
function run(){
// get form data
var eng = $('#rec #eng').val();
var cym = $('#rec #cym').val();
// put form data in a JSON
var data_json = {'eng':eng, 'cym':cym};
//post to php script to update database and return html list
$.ajax({
type: 'post',
url: 'ajax/lang_api.php',
data: data_json,
beforeSend: function() {
// before send the request, display a "Loading..." message
$('#records').html('Loading...');
},
timeout: 10000, // sets timeout (10 seconds)
error: function(xhr, status, error) { alert('Error: '+ xhr.status+ ' - '+ error); },
success: function(response) { $('#records').html(response); }
});
return false; //this should stop page reload
}
$(document).ready(function()
{
$("#rec").submit(function()
{
run()
});
run()
});
</script>
</head>
<body>
<div id="record_wrapper">
<h1>Records by title</h1>
<div id="records">
Waiting for records to load....
</div>
这是创建列表并更新数据库的 php 脚本。
//get lang post variables if they exist
$eng_id = $_POST['eng'];
$cym_id = $_POST['cym'];
//debug - check post values are correct visually
echo 'eng' . $eng_id . ' cym ' . $cym_id;
function create_list() {
//generate html list
$html .=' <ul>';
//grab records from DB which have not been updated with language value to display
if (!$query = mysql_query("SELECT `id`, `title-245` FROM `records` WHERE `catalog_lang-040` = 'eng' ORDER BY `title-245`")) {
echo mysql_error();
}
//generate each item in list as a form
//set form id to rec
while($row = mysql_fetch_array($query))
{
$html .= ' <li><form action="ajax/lang_api.php" method="post" id="rec">' . $row['title-245'] . ' <input type="text" name="cym" size="3" /><input type="hidden" name="eng" id="eng" value="' . $row['id'] . '" /> <input type="submit" value="submit" /></form></li>
';
}
$html .= ' </ul>';
//return the html to be used
return $html;
}
//if eng and cym ids are submitted then update DB, otherwise just return the html required.
//check value is set using isset
if (isset($eng_id) && isset($cym_id)) {
if (!is_numeric($eng_id)) { $rehtml= 'Invalid English ID'; }
if (!is_numeric($cym_id)) { $rehtml= 'Invalid Welsh ID'; }
//sql to go here to update db ith language value
//and then get new list to output.
echo create_list();
//post undefined so just return list (for first page load/refresh)
}else{
// output the html
echo create_list();
}
可能遗漏了一些明显的东西,但第一次玩 jquery/js。
这是由 lang_api.php 创建的 HTML 列表,它加载到记录 div 中,替换 sort_language.php 中的“等待记录加载”
eng417 cym 21
<ul>
<li><form action="ajax/lang_api.php" method="post" id="rec">18th - 19th century ballads collection , 18th - 19th century, <input type="text" name="cym" size="3" /><input type="hidden" name="eng" id="eng" value="417" /> <input type="submit" value="submit" /></form></li>
<li><form action="ajax/lang_api.php" method="post" id="rec">1st The Queen's Dragoon Guards Collections , 1685 -, <input type="text" name="cym" size="3" /><input type="hidden" name="eng" id="eng" value="1001" /> <input type="submit" value="submit" /></form></li>
<li><form action="ajax/lang_api.php" method="post" id="rec">Aberconway Library , 20th century -, <input type="text" name="cym" size="3" /><input type="hidden" name="eng" id="eng" value="101" /> <input type="submit" value="submit" /></form></li>
<li><form action="ajax/lang_api.php" method="post" id="rec">Aberfan Disaster Archive , Mainly 1966 -, <input type="text" name="cym" size="3" /><input type="hidden" name="eng" id="eng" value="303" /> <input type="submit" value="submit" /></form></li>
<li><form action="ajax/lang_api.php" method="post" id="rec">Abergavenny Local History Collection , Prehistory to the present day, <input type="text" name="cym" size="3" /><input type="hidden" name="eng" id="eng" value="1030" /> <input type="submit" value="submit" /></form></li>
<li><form action="ajax/lang_api.php" method="post" id="rec">Alister Hardy Religious Experience Archive , 1924 -, <input type="text" name="cym" size="3" /><input type="hidden" name="eng" id="eng" value="1042" /> <input type="submit" value="submit" /></form></li>
<li><form action="ajax/lang_api.php" method="post" id="rec">Almanacs Collection , 1700 - 1850, <input type="text" name="cym" size="3" /><input type="hidden" name="eng" id="eng" value="740" /> <input type="submit" value="submit" /></form></li>
<li><form action="ajax/lang_api.php" method="post" id="rec">Ammanford Local Studies Collection , 1880s -, <input type="text" name="cym" size="3" /><input type="hidden" name="eng" id="eng" value="939" /> <input type="submit" value="submit" /></form></li>
<li><form action="ajax/lang_api.php" method="post" id="rec">Anderson Collection , 17th - 20th century, <input type="text" name="cym" size="3" /><input type="hidden" name="eng" id="eng" value="407" /> <input type="submit" value="submit" /></form></li>
</ul>