【问题标题】:How to get data from form in a modal into mysql如何从模态表单中获取数据到mysql
【发布时间】:2011-05-10 05:57:09
【问题描述】:

目标:允许用户点击页面上的按钮/链接(称为主机页面),出现一个包含表单的模式。用户填写表单并点击提交按钮,数据写入mysql。模式关闭,用户只能查看“主机页面”。

我有这 2/3 的工作。单击创建与表单一起加载的模式。添加到表单的数据被写入数据库。模式在提交后关闭。 问题出现在流程的最后。当用户提交时,模式关闭,用户看到的是“表单”页面,而不是“主机”页面。此外,表单似乎将表单 datab 两次插入数据库。

问题 我想做的事情是否可行,如果可以,我将如何改变流程?

代码(host.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>

表单页面(form.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;
        });
    });
});

如果有任何关于如何更改流程以便我可以使用模态表单的建议,我将不胜感激。

谢谢。

【问题讨论】:

  • 您可以使用按钮或链接或提交按钮来触发 jQuery .ajax 并通过它提交表单,然后在 ajax 关闭模式之后。
  • @arma:感谢您的反馈。是的,我得出结论/了解到我的方法不正确,表单需要由 jquery 处理。

标签: php jquery jquery-ui forms modal-dialog


【解决方案1】:

你有很多错误,包括你的流程

  1. $('#target').each(function() { 不应具有 each 函数,因为 ID 应始终是唯一的,因此不应超过一个 id="target"。
  2. 你得到它两次的原因是因为你的 MySQL 没有正确验证,你需要 if(isset($_POST)){ /* mysql thingy goes here */ } 这就是为什么你会发布两次
  3. 请请,请使用 mysql_real_escape_string($_POST['whatever']);

  4. 祝你好运,你应该多修改:)

更新

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
}

所以上面的代码,会检查表单,如果已经提交,则连接,选择数据库名称,然后插入数据库,否则如果表单没有提交,它将显示表单。

【讨论】:

  • 感谢您的回复和反馈。
  • 感谢您的回复和反馈。关于#1,我从 jQuery UI 对话框页面上引用的站点中获取了脚本的基础。我和你有同样的想法,但认为作者必须知道他在做什么,因为文档引用了作者。见blog.nemikor.com/2009/04/18/loading-a-page-into-a-dialog。关于#2 已注明。关于#3 指出。关于#4我需要它。但是,我怀疑我的技能很差,除了页面演示的问题并没有被上述纠正,并且该过程仍然需要修改 - 对这个问题有什么想法吗?
  • urlglobal 分配给它的值在哪里?例如var urlglobal = 'blabla';
  • 为那段混乱道歉。我应该删除 urlglobal。它与将值从主机页面传递到模式中的表单有关。这与我要问的问题无关。已删除。
【解决方案2】:

请记住,您可以使用 $("#my_form_id").serialize() 来获取您的表单数据以进行 $.ajax 调用。

查看http://api.jquery.com/serialize/

【讨论】:

  • 谢谢,感谢您的参考,这很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 2017-11-30
相关资源
最近更新 更多