2013-11-28 146 views
2

我试图从jQuery的动态表单插入数据到MySQL。为了记录,我正在使用Twitter Bootstrap。 我实际上实现了插入“行”但没有数据。我现在发现的最好的帮助是这个​​用于插入到MySQL和here到jQuery脚本。从动态jQuery表单向数据添加数据

提前感谢您花时间为此。

文件:add.php

<form action="../reglas/insert.php" method="post" class="form-horizontal"> 
<div class="container"> 
<div id="content"> 
<div class="row"> 

<a class="add_line btn btn-small" href="#" style="margin-left:5px">+</a> 
</div> 

<br> 
</div> 



</div> 

</div> 
</div> 

<div style="margin:20px auto; width:80px;"> 

<input class="btn btn-success" type="submit" value="Guardar"> 
</div> 

</form> 


<script> 
$("#content").delegate(".add_line", "click", function() { 
var content = '<div class="row">' + 

        '<div class="span3"><div class="input-append"><input type="text" class="span2 text-right" placeholder="0.00" id="enganche" name="enganche" value="" /><span class="add-on">%</span></div></div>' + 
        '<div class="span3"><div class="input-append"><input type="text" class="span2 text-right" placeholder="0.00" id="comision" name="comision" value="" /><span class="add-on">%</span></div></div>' + 
        '<div class="span3"><div class="input-append"><input type="text" class="span2 text-right" placeholder="0.00" id="r1" name="r1[]" value="" /><span class="add-on">%</span></div></div>' + 

        '<div class="span2"><div class="input-append"><input type="text" class="span1 text-right" placeholder="0.00" id="r2" name="r2[]" value="" /><span class="add-on">%</span></div></div>'+ 

        '<a class="del_line btn btn-small" href="#" style="margin-left:5px">-</a>&nbsp;' + 
        '<br><br>'+ 

        '</div>'; 
$("#content").append(content); 
return false; 
}); 

$("#content").delegate(".del_line", "click", function() { 
$(this).parent().remove(); 
return false; 
}); 
</script> 

文件:insert.php

的连接:

$con=mysqli_connect("$host","$username","$password","$db_name"); 
mysqli_autocommit($con, false); 

$flag=true; 

查询:

$query=" 
INSERT INTO $tbl_name(
the_input, 
) 
VALUES (
'$the_input', 
)"; 

的 “for”:

for ($i = 0; $i < count($_POST['the_input']); $i++) { 
$the_input = $_POST['the_input'][$i]; 


$result = mysqli_query($con, $query); 
if (!$result) { 
$flag = false; 
echo "Error details: " . mysqli_error($con). ". "; 
} 
} 
if ($flag) { 
mysqli_commit($con); 
echo "Done!"; 
} 

mysqli_close($con); 

?> 
+0

谢谢@leemo,你能更具体吗?我很欣赏这里的一些指导。 – Horacio

+0

我会猜测你试图发送哪些数据,并且试着嘲笑你的答案 – hammus

+0

谢谢@leemo。这是实际窗体在浏览器中的外观。 (https://www.dropbox.com/s/61hxtuls9kwa959/Screen%20Shot%202013-11-27%20at%208.19.37%20PM.png) – Horacio

回答

0

欧凯dokey。

首先第一件事情:

$.delegate()已被弃用所以,除非你正在使用的版本是< jQuery的1.7(这是许多年前的版本),你应该迁移到$.on()事件粘合剂/委托功能。这将确保你的代码是面向未来的

其次(至少要等到他们再次更改API!):

我假设你已经在表单标签包裹你的数据和值实际上是越来越发送到这里的接收页面是我该怎么做:

if(isset($_POST['the_input'])) 
{ 
    //create connection object; 
    $mysqli = new mysqli($host,$username,$password,$db_name); 

    $query = "INSERT INTO `the_input` VALUES ("; 

    //construct the query. Get the the_input data and paramater string (required for stmt binding) 
    foreach($_POST['the_input'] as $value) 
    { 
     $query .= $value . ","; 
    } 

    //remove trailing comma 
    $query = substr($query, 0, strlen($query)-1); 
    $query .= ")"; 

    //this is just so you can see what the loop did and test the output of the query, remove this when you're confident 
    echo "The constructed query string is: " . $query 

    if($mysqli->query($query) == false) 
    { 
     trigger_error($mysqli->error); 
    } else { 
     echo "Success! Check the DB!"; 
    } 

} else { 
    Echo "No POST data was recieved"; 
} 
+0

非常感谢@leemo的建议和答案。让我们试试吧! – Horacio

+0

@HcH我的代码中有一个错误,我现在已经纠正了......丢失的括号。 – hammus

+0

谢谢@leemo我有这个错误。我正在使用Adobe Dw。 https://www.dropbox.com/s/bgp5ukcw4ihom8o/Screenshot%202013-11-27%2021.01.42.png – Horacio