我正在创建一个注册总线路由的应用程序,给它一次路由名称并动态创建一个文本框和一个选择框,以便用户在该路径上创建一个他想要的公交车站。 这是我的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> <p> </p></p>
<p> <p> </p></p>
<div id="div">
</div>
<p> </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> </p><p> </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> <div class='_25'><input type='textbox' id='textbox' name='stop"+counter+"' placeholder='Stop Name'></input></div> <div class='_25'><input type='textbox' id='textbox' name='timing"+counter+"' placeholder='Timing'></input></div> <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>
答案 0 :(得分: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);
}
}
?>