1

我正在创建一个应用程序来注册公共汽车的路线,给它一次路线名称,并为用户动态创建一个文本框和一个选择框,以便在该路线上尽可能多地创建一个公共汽车站。这是我的 CodeIgniter 的观点:在表单 div 中动态创建文本框和选择框的脚本。

我想将其输入的数据发布到我的数据库中,其字段如下:

route_number     stop_name  am_pm  timing 

你能帮我使用 CodeIgniter 模型控制器功能吗?视图如下:

<form method='post' action='<?php echo site_url('a3_bus_system/output')?>'>
<div class="_25">
<strong>Route Name/Number</strong>
     <br/>
<input type="text" name=""></input>
</div>
<p>&nbsp;<p>&nbsp;</p></p>
<p>&nbsp;<p>&nbsp;</p></p>
<div id="div">
</div>
<p>&nbsp;</p><div class="_25">
<p><input type="button" name="button" class="button red" id="button" value="Add"  onclick="generateRow() "/></a></p>
</div>
<input type='button' value='Remove Button' id='removeButton'>
<p>&nbsp;</p><p>&nbsp;</p></div>
 <input type="submit"  class="button blue" id="button" value="Register" />
/form>  
</div>
</div>
<div class="clear height-fix"></div>
</div></div> <!--! end of #main-content -->
</div> <!--! end of #main -->
<script>
var counter=1;
  function generateRow() {
var count="<font color='red'>"+counter+"</font>";
var temp ="<p>&nbsp;&nbsp;&nbsp;&nbsp;<div class='_25'><input type='textbox' id='textbox' name='stop"+counter+"' placeholder='Stop Name'></input></div>&nbsp;&nbsp;&nbsp;<div class='_25'><input type='textbox' id='textbox' name='timing"+counter+"' placeholder='Timing'></input></div>&nbsp;<div class='_25'><select id='ampm"+counter+"' name='ampm"+counter+"'><option>a.m</option><option>p.m</option></select>  </div>";
var newdiv = document.createElement('div');
newdiv.innerHTML = temp + count;
var yourDiv = document.getElementById('div');
yourDiv.appendChild(newdiv);
counter++;
}
</script>
4

1 回答 1

0

您需要将表单提交到同一个 PHP 文件,通过传递counter参数我们知道已经提交了多少组数据,然后我们可以将数据插入到您的数据库中。提交表单后,以下 php 代码会将数据插入到您的数据库中:

<?php
if(!empty($_POST["counter"]) && !empty($_POST["route"])){
    $counter = $_POST["counter"];
    $route   = $_POST["route"];
    $pdo     = new mysqli("host", "username", "password", "database");
    $stmt    = "INSERT INTO table_name(route_number, stop_name, am_pm, timing) VALUES(?, ?, ?, ?)";
    $insert  = $pdo->prepare($stmt);

    for($i = 1; $i <= $counter; $i++)
    {
        $stop   = $_POST["stop" . $i];
        $timing = $_POST["timing" . $i];
        $ampm   = $_POST["ampm" . $i];
        $insert->bind_param("isss", $route, $stop, $ampm, $timing);
        $insert->execute();
    }
    $stmt->close();
    $pdo->close();
}
?>


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Buss Routes</title>
<script type="text/javascript">
    var counter = 1;

    function generateRow() {
        var count = "<p align='center' ><font color='red'>" + counter + "</font></p></div>";
        var temp = "<div id='container" + counter + "'  class='_25' >"
            + "<input type='text' name='stop" + counter + "' placeholder='Stop Name' />"
            + "<input type='text' name='timing" + counter + "' placeholder='Timing' />"
            + "<select name='ampm" + counter + "'><option>a.m</option><option>p.m</option></select>"
            + "<input type='button' onClick='Javascript:this.parentNode.remove(); counter--;' value='Remove' />";
        var newdiv = document.createElement('div');
        newdiv.innerHTML = temp + count;
        document.getElementById('div').appendChild(newdiv);
        document.getElementById('counter').value = counter;
        counter++;
    }
</script>
</head>
<body>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
    <div class="_25">
        <p align="center"><strong>Route Name/Number</strong></p>
        <input type="text" name="route" placeholder="Route Number" />
    </div>
    <div id="div"></div>
    <div class="_25">
        <input type="button" class="button red" value="Add" onclick="generateRow()" />
        <input type="hidden" name="counter" id="counter" />
        <input type="submit" class="button blue" value="Register" />
    </div>
    </form>
<div class="clear height-fix" ></div>
</body>
</html>

这是一个演示,展示了 javascript 代码是如何工作的:jsfiddle

host, database, username,替换password为您的服务器的详细信息和table_name包含数据的表名。


对于 CodeIgniter,试试这个而不是上面的 php 代码(未经测试,我以前从未使用过 CodeIgniter,但通过检查 ellislab 网站上的文档,这应该可以工作):

<?php
if(!empty($_POST["counter"]) && !empty($_POST["route"])){
    $counter = $_POST["counter"];
    $route   = $_POST["route"];
    $this->load->database();

    for($i = 1; $i <= $counter; $i++)
    {
        $data = (
            'route_number' => $route,
            'stop_name'    => $_POST["stop" . $i],
            'am_pm'        => $_POST["ampm" . $i],
            'timing'       => $_POST["timing" . $i]
        );
        $this->db->insert('mytable', $data);
    }
}
?>
于 2013-04-08T19:12:48.913 回答