我需要將此表單發佈到數據庫並檢索新添加的信息,而無需使用XHR進行刷新。使用AJAX更新部分動態表單(無刷新) - 無法使其工作
頁面加載時可以編輯一些初始元素,並可以通過按按鈕動態添加更多「新」元素(新用戶需要這樣做,因爲他們沒有預先存在的任務,它會跟蹤誰你與session_id)。
我已經在這裏超過13個小時了,有點累了。
代碼:
的index.php
<?php
//Sets unique session for the current visitor and keeps track of information for use with database
$time = time();
$date = $today = date("Ymd");
$id = $time + $date;
$id = session_id();
if(empty($id)) session_start();
//echo "SID: ".SID."<br>session_id(): ".session_id()."<br>COOKIE: ".$_COOKIE["PHPSESSID"];
?>
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<title>Simple To-Do List</title>
<?php
// Create connection via my connect.php file
require 'connect.php';
// Create query
$query= "select * from checklist where SID = '".session_id()."'";
$result = mysql_query($query);
// Create requisite array for checklist
$checklistItems = array();
//Check to ensure query won't implode and is valid
if($result === FALSE) {
die(mysql_error());
}
// Calculates number of rows from query
$num=mysql_numrows($result);
mysql_close($con);
?>
<!-- javascript code to dynamically add new form fields as well as check\uncheck all boxes -->
<script src="//code.jquery.com/jquery-latest.min.js" language="javascript" type="text/javascript"></script>
<script src="addInput.js" language="Javascript" type="text/javascript"></script>
</head>
<body>
<h1>My To-Dos</h1>
<form name="checklist" id="checklist" class="checklist">
<?php // Loop through query results
while($row = mysql_fetch_array($result))
{
$entry = $row['Entry'];
$CID = $row['CID'];
$checked =$row['Checked'];
// echo $CID;
echo "<input type=\"text\" value=\"$entry\" name=\"textfield$CID;\" id=\"textfield$CID;\" onchange=\"showUser(this.value)\" />";
echo "<input type=\"checkbox\" name=\"checkbox$CID;\" id=\"checkbox$CID;\" value=\"$checked\"".(($checked == '1')? ' checked="checked"' : '')." />";
echo "<br>";
}
?>
<div id="dynamicInput"></div>
<input type="submit" id="checklistSubmit" name="checklistSubmit" class="checklist-submit"> <input type="button" id="CompleteAll" name="CompleteAll" value="Check All" onclick="javascript:checkAll('checklist', true);"><input type="button" id="UncheckAll" name="UncheckAll" value="Uncheck All" onclick="javascript:checkAll('checklist', false);">
<input type="button" value="Add another text input" onClick="addInput('dynamicInput');"></form>
</body>
</html>
connect.php
<?php
// Create connection
$con=mysql_connect("localhost","root","");
$selected = mysql_select_db("madmonk",$con);
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysql_connect_error();
}
?>
addInput.js
// Creates new dynamic elements within HTML body
var counter = 0;
var limit = 8;
function addInput(divName){
i=counter; i++;
if (counter == limit) {
alert("You have reached the limit of adding " + counter + " inputs");
}
else {
var newdiv = document.createElement('div');
newdiv.innerHTML = " <input type='text'name='myInputs["+i+"]'><input type='checkbox' name='myInputs["+i+"]'><br>";
document.getElementById(divName).appendChild(newdiv);
counter++;
}
}
//Checks\unchecks all checkboxes on the web page
function checkAll(formname, checktoggle)
{
var checkboxes = new Array();
checkboxes = document[formname].getElementsByTagName('input');
for (var i=0; i<checkboxes.length; i++) {
if (checkboxes[i].type == 'checkbox') {
checkboxes[i].checked = checktoggle;
}
}
}
//AJAX code to communicate with server without page refresh
$('checklistSubmit').click(function(e) {
$(e).stopPropagation();
$.post({
url: 'processor.php',
data: $('#checklist').serialize(),
dataType: 'html',
success: function(data, status, jqXHR) {
$('div.successmessage').html(data);
//success callback function
alert (success);
},
error: function() {
//error callback function
alert (failure);
}
});
});
我知道一個事實,我在上面的AJAX代碼有問題。這很關鍵,我無法得到這個工作。我從來沒有嘗試過像這樣做過,我決定嘗試並將我的想法付諸實踐並完成。啊。
processor.php
<?php
require 'connect.php';
$entry = $_POST['entry'];
$checked = $_POST['checked'];
$num_items = count($entry);
for ($i = 0; $i < $num_items; $i++)
{
$sql="INSERT INTO checklist (Entry, Checked, SID)
VALUES ($checked, $entry, session_id()) WHERE SID = '".session_id()."'";}
mysql_close($con);
?>
^這是粗糙的,完全未完成。
我如何通過MySQL和PHP動態域和循環通過他們管 到數據庫進行交互?
如何獲取它以更新index.php頁面,新值爲 新增無縫添加新項目?
如何獲得AJAX的工作?
請對我來說非常具體。我對使用AJAX非常新穎。
這對我來說還不夠,我無法單獨做任何事情。我在網上搜索這個程序,特別是使用動態生成的表單。 –