目标:允许用户单击页面上的按钮/链接(称为主机页面),将出现包含表单的模态。用户填写表单并单击提交按钮,数据将写入mysql。模式关闭,用户只能查看"主机页面"。
我有这个 2/3 工作。单击将创建加载到窗体的模式。添加到表单的数据将写入数据库。模式在提交后关闭。问题出在过程的最后。当用户提交时,模式将关闭,并向用户显示"表单"页面,而不是"主机"页面。此外,表单似乎将表单 datab 插入数据库两次。
问题我想做的事情是否可行,如果是,我将如何改变流程?
代码(主机.php)
主机页面
<div>
<h2>Test Section</h2>
<p> This <a id="target" href="http://localhost/dialogExperiments/TESTsingleformfeedback.php" title="Test Form">LINK</a> opens the feedback form.</p>
</div>
表单页 (表单.php)
<body>
<!-- Begin forms section --><div>
<h1> Form</h1>
<!-- First section is php script to process this particular form-->
<?php
$browser = get_browser(null, true);//get array of viewing browser information. Used in POST below.
//Address error handling
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~ E_NOTICE);
if (isset ($_POST['submit'])) {//Handle the form
if ($dbc = @mysql_connect ('localhost','root','root')) {
if (!@mysql_select_db ('feedback1')) {
die ('<p>Could not select the database because <b>' . mysql_error() . '</b></p>');
}
}else{
die('<p>Could not connect to MySQL because <b>' . mysql_error() . '</b></p>');
}
//Define the query. Note in this query we use the table "errors"
$query0 = "INSERT INTO errors (ID, words_omitted, jumbled_text, other, description, url, date_recorded, user, browser, operating_system)
VALUES (0, '{$_POST['words_omitted']}', '{$_POST['jumbled_text']}', '{$_POST['other']}', '{$_POST['description']}','{$_POST['url']}',NOW(),'{$_POST['user']}','{$_POST['browser']}','{$_POST['operating_system']}')";
//Execute the query
if (@mysql_query ($query0)) {
print '<p>The First form feedback has been recorded.</p>';
}else{
print "<p>Could not add entry because <b>" . mysql_error() . "</b> The query was $query0.</p>";
}
mysql_close();
}
?>
<!-- End process script and next display form-->
<!-- Begin Form 1 Errors--><div id="hideCategory1" class="formFrame">
<h2>Errors in the text</h2>
<p>Please check all that apply and add any description you can.</p>
<form action="TESTsingleformfeedback.php" method="post" name="errorInText">
<p><input name="words_omitted" type="checkbox" value="Words Missing" />Words Missing</p>
<p><input name="jumbled_text" type="checkbox" value="Jumbled Words" />Jumbled Text</p>
<p><input name="other" type="checkbox" value="Other" />Other - Please add details in the "Description" Box.</p>
<em>Description</em>
<p>Please add as much descripton as you can about the problem you noticed</p>
<textarea name="description" cols="40" rows="5"></textarea>
<p>Page Address:<input name="url" type="text" value="" id="targetURL" /></p>
<p>Date Recorded</p>
<p>User<input name="user" type="text" size="40" maxlength="100" /> </p>
<p>Browser<input name="browser" type="hidden" value="<?php echo $browser['browser'] ?>" /></p>
<p>Operating System<input name="operating_system" type="hidden" value="<?php echo $browser['platform'] ?>"/></p>
<input type="submit" name="submit" value="Add To Records" />
</form>
</div>
</div>
</body>
Javascript/jQuery 脚本是
$(document).ready(function() {
$('#target').each(function() {
var $link = $(this);
var $dialog = $('<div></div>')
.load($link.attr('href'))
.dialog({
autoOpen: false,
title: $link.attr('title'),
width: 600,
height: 500
});
$link.click(function() {
$dialog.dialog('open');
return false;
});
});
});
我将不胜感激任何有关如何更改过程的指示,以便我可以使用模态表单。
谢谢。
您在那里有许多错误,包括您的流程
-
$('#target').each(function() {
不应该有 each 函数,因为 ID 应该始终是唯一的,因此不应该超过一个 id="target"。 - 你得到两次的原因是因为你的MySQL没有得到正确的验证,你需要
if(isset($_POST)){ /* mysql thingy goes here */ }
这就是为什么你把它发布两次 -
请用mysql_real_escape_string($_POST['随便']);
-
祝你好运,你应该修改更多的:)
更新
if (isset ($_POST['submit']) && $_POST['submit']=='Add To Records') {
// check if the form was submitted
//do db thing here
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can''t use foo : ' . mysql_error());
}
//insert into db here using mysql_query();
//if there was no mysql_error(); then show the successfully message else error message
// please also check mysql_real_escape_string() on php.net its important so people don't hack ur
//db, n potentially the whole website itset.
//http://www.php.net/manual/en/function.mysql-real-escape-string.php
}else{
?>
<form>.....</form>
<?php
}
所以上面的代码,将检查表单,如果它已经提交,然后连接,选择数据库名称,然后插入到数据库中,其他明智的如果表单没有提交它将显示表单。
请记住,
您可以使用 $("#my_form_id").serialize() 来获取 $.ajax 调用的表单数据。
查看 http://api.jquery.com/serialize/