我想發佈foreach函數的多個值,因爲我有多個動態文本框,那麼我應該如何發送數據庫中的值? 怎麼寫的foreach功能DAT .. 下方顯示2多個文本框代碼但沒有在DATABSE提交值動態添加輸入字段並提交到數據庫與jQuery和PHP
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript">
var counter = 0;
$(function(){
$('p#add_field').click(function(){
counter += 1;
$('#container').append(
'<strong>Hobby No. ' + counter + '</strong><br />'
+ '<input id="field_' + counter + '" name="dynfields[]' + '" type="text" /><br />'
+'<strong>HolidayReason ' + counter + '</strong> '
+ '<input id="holidayreason_' + counter + '" name="holireason[]' + '" type="text" />'
);
});
});
</script>
<body>
<?php
if (isset($_POST['submit_val'])) {
if (($_POST['dynfields'])&& ($_POST['holireason'])) {
//$aaa=array($_POST['dynfields']);
foreach ($_POST['dynfields'] as $key=>$value)
{
$values = mysql_real_escape_string($value);
//$holireasons = mysql_real_escape_string($holireason);
$query = mysql_query("INSERT INTO my_hobbies (hobbies) VALUES ('$values')");
}
}
echo "<i><h2><strong>" . count($_POST['dynfields']) . "</strong> Hobbies Added</h2></i>";
mysql_close();
}
?>
<?php if (!isset($_POST['submit_val'])) { ?>
<h1>Add your Hobbies</h1>
<form method="post" action="">
<div id="container">
<p id="add_field"><a href="#"><span>Click To Add Hobbies</span></a></p>
</div>
<input type="submit" name="submit_val" value="Submit" />
</form>
<?php } ?>
</body>
</html>
你嘗試在你的foreach循環中打印'$ values'嗎? – mesutozer
不,我想在db中添加值作爲單行 – user3359065
你可以參考這個:http://stackoverflow.com/questions/20607405/how-to-insert-dynamically-added-text-boxes-into-mysql-database或檢查這個小提琴: http://jsfiddle.net/ehx6M/1/ –