Php 如何将表单中的数据导入mysql
目标:允许用户单击页面上的按钮/链接(称为主页),此时会出现包含表单的模式。用户填写表单并点击提交按钮,数据被写入mysql。模式关闭,用户只需查看“主机页” 我有三分之二的工作。单击创建与表单一起加载的模式。添加到表单中的数据将写入数据库。模式在提交后关闭。 问题就在这个过程的末尾。当用户提交时,模式关闭,用户将看到“表单”页面而不是“主机”页面。此外,表单似乎正在将表单数据库插入数据库中两次 问题 我想做的事情可能吗?如果可能,我将如何改变流程 代码(host.php) 主机页Php 如何将表单中的数据导入mysql,php,jquery,jquery-ui,forms,modal-dialog,Php,Jquery,Jquery Ui,Forms,Modal Dialog,目标:允许用户单击页面上的按钮/链接(称为主页),此时会出现包含表单的模式。用户填写表单并点击提交按钮,数据被写入mysql。模式关闭,用户只需查看“主机页” 我有三分之二的工作。单击创建与表单一起加载的模式。添加到表单中的数据将写入数据库。模式在提交后关闭。 问题就在这个过程的末尾。当用户提交时,模式关闭,用户将看到“表单”页面而不是“主机”页面。此外,表单似乎正在将表单数据库插入数据库中两次 问题 我想做的事情可能吗?如果可能,我将如何改变流程 代码(host.php) 主机页 试验段 这
试验段
这将打开反馈表单
表单页面(Form.php)
类型
你有很多错误,包括你的流程
$('#target')。每个(function(){
不应具有each函数,因为id应始终是唯一的,因此不应超过一个id=“target”
之所以要得到两次,是因为MySQL没有正确验证,您需要
if(isset($\u POST)){/*mysql thingy出现在这里*/}
这就是为什么你会两次发布它
请使用mysql\u real\u escape\u字符串($\u POST['which'])
祝你好运,你应该多复习:)
已更新
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
}
if(isset($\u POST['submit'])&&&$\u POST['submit']=='Add To Records')){
//检查表单是否已提交
//在这里做点什么
$link=mysql\u connect('localhost','mysql\u user','mysql\u password');
如果(!$link){
die('未连接:'.mysql_error());
}
//使foo成为当前db
$db\u selected=mysql\u select\u db('foo',$link);
如果(!$db_选中){
die('不能使用foo:'.mysql_error());
}
//在这里使用mysql_query()插入数据库;
//如果没有mysql_error();则显示成功消息else错误消息
//还请检查php.net上的mysql\u real\u escape\u string(),这很重要,这样人们就不会攻击你了
//db,n可能是整个网站的itset。
//http://www.php.net/manual/en/function.mysql-real-escape-string.php
}否则{
?>
.....
请记住,可以使用$(“#my_form_id”).serialize()获取$.ajax调用的表单数据
查看您可以使用按钮、链接或提交按钮触发jQuery并通过该按钮提交表单,然后在ajax关闭模式后提交表单。@arma:感谢您的反馈。是的,我总结/得知我的方法不正确,表单需要由jQuery处理。感谢您的回复和反馈。感谢您的回复和反馈确认:关于#1,我以jQuery UI对话框页面上引用的站点的脚本为基础。我的想法与您相同,但我认为作者必须知道自己在做什么,因为文档引用了作者。参见。关于#2,注意到。关于#3,注意到。关于#4,我需要它。但是,我怀疑我的技能也很差ide页面呈现的问题没有通过上述方式得到纠正,流程仍然需要修改-对该问题有什么想法吗?urlglobal分配给它的值在哪里?例如var urlglobal='blablabla';
对这一混乱表示歉意。我应该删除urlglobal。它与从ho传递值有关模式中的表单的st页。它与我询问的问题无关。已删除。谢谢,我感谢您的参考,这很有帮助。
<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>
$(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;
});
});
});
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
}