我需要将此表单发布到数据库并检索新添加的信息,而无需使用 XHR 刷新。
该页面加载了一些可以编辑的初始元素,并且可以通过按下按钮动态添加更多“新”元素(新用户需要这样做,因为他们没有预先存在的任务。它使用 session_id 跟踪您的身份)。
我已经在这玩了13个多小时了,有点累。
代码:
索引.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>
连接.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 代码时遇到了问题。这很关键,我无法让它发挥作用。我以前从未尝试过做这样的事情,我决定尝试并全神贯注地完成它。啊。
处理器.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 非常非常陌生。