如何从模式中的表单获取数据到mysql

时间:2011-05-10 05:57:10

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

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

我有2/3工作。单击将创建使用表单加载的模式。添加到表单的数据将写入数据库。提交后模式关闭。 问题出在这个过程的最后。当用户提交时,模式关闭,用户将显示“表单”页面以代替“主机”页面。此外,表单似乎是将表单数据库两次插入数据库。

问题 我想做的是什么,如果是这样,我将如何改变这个过程?

代码(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;
        });
    });
});

我很欣赏有关如何更改流程的任何指示,以便我可以使用模态表单。

感谢。

2 个答案:

答案 0 :(得分:0)

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

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

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

  5. <强>已更新

    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
    }
    

    所以上面的代码,将检查表单,如果它已经先提交,然后连接,选择db name,然后插入db,其他明智的如果表单未提交则会显示表单。

答案 1 :(得分:0)

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

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